Difference between revisions of "Lua:errorOnLookupFailure"

From Cheat Engine
Jump to navigation Jump to search
(Started article using help text from CE's built-in help)
 
m (Added related function template.)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
'''function''' errorOnLookupFailure(''state'')
+
[[Category:Lua]]
 +
{{CodeBox|'''function''' errorOnLookupFailure(''state'') ''':''' void}}
  
If set to true (default) address lookups in string form will raise an error if it can not be looked up. This includes symbol names that are not defined and pointers that are bad. If set to false it will return 0 in those cases (Useful for pointers that don't work 100% of the time)
+
Controls whether address and symbol lookup failures raise an error.
  
6.4+:Returns the original state
+
If enabled, address lookups in string form will raise an error when the address cannot be resolved. This includes undefined symbols and invalid pointers.
 +
 
 +
If disabled, failed lookups return 0 instead. This is useful for pointers or symbols that may not be valid all the time.
 +
 
 +
By default, lookup failures raise an error.
  
 
===Function Parameters===
 
===Function Parameters===
{|width="85%" cellpadding="10%" cellpadding="5%" cellspacing="0" border="0"
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 
!align="left"|Parameter
 
!align="left"|Parameter
 
!align="left"|Type
 
!align="left"|Type
Line 13: Line 18:
 
|state
 
|state
 
|Boolean
 
|Boolean
|Whether to raise errors on address lookup failures
+
|If true, lookup failures raise an error. If false, lookup failures return 0.
 
|}
 
|}
  
== See also ==
+
===Returns===
* [[readBytesLocal]]
+
This function does not return a value.
* [[Lua]]
+
 
 +
===Examples===
 +
 
 +
====Disable lookup errors====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
errorOnLookupFailure(false)
 +
</syntaxhighlight>
 +
 
 +
====Enable lookup errors====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
errorOnLookupFailure(true)
 +
</syntaxhighlight>
 +
 
 +
====Use with an unstable pointer====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
errorOnLookupFailure(false)
 +
 
 +
local address = getAddress("[[[game.exe+1234]+10]+20]+30")
 +
 
 +
if address == 0 then
 +
  print("Pointer could not be resolved")
 +
else
 +
  print(string.format("%X", address))
 +
end
 +
</syntaxhighlight>
 +
 
 +
====Restore the default behavior after a safe lookup====
 +
<syntaxhighlight lang="lua" line highlight="1,10">
 +
errorOnLookupFailure(false)
 +
 
 +
local address = getAddress("SomeOptionalSymbol")
 +
 
 +
if address == 0 then
 +
  print("Symbol not available")
 +
end
 +
 
 +
-- Restore default behavior
 +
errorOnLookupFailure(true)
 +
</syntaxhighlight>
 +
 
 +
====Use getAddressSafe instead of changing the global behavior====
 +
<syntaxhighlight lang="lua" line>
 +
local address = getAddressSafe("SomeOptionalSymbol")
 +
 
 +
if address == nil then
 +
  print("Symbol not available")
 +
end
 +
</syntaxhighlight>
 +
 
 +
====Temporarily suppress lookup errors====
 +
<syntaxhighlight lang="lua" line highlight="2">
 +
local function lookupOptionalAddress(symbol)
 +
  errorOnLookupFailure(false)
 +
 
 +
  local address = getAddress(symbol)
 +
 
 +
  errorOnLookupFailure(true)
 +
 
 +
  if address == 0 then
 +
    return nil
 +
  end
 +
 
 +
  return address
 +
end
 +
</syntaxhighlight>
 +
 
 +
====Handle optional symbols====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
errorOnLookupFailure(false)
 +
 
 +
local symbols = {
 +
  "OptionalSymbolA",
 +
  "OptionalSymbolB",
 +
  "OptionalSymbolC"
 +
}
 +
 
 +
for i, symbol in ipairs(symbols) do
 +
  local address = getAddress(symbol)
 +
 
 +
  if address ~= 0 then
 +
    print(symbol .. " = " .. string.format("%X", address))
 +
  end
 +
end
 +
 
 +
errorOnLookupFailure(true)
 +
</syntaxhighlight>
 +
 
 +
====Avoid pointer lookup crashes in repeated checks====
 +
<syntaxhighlight lang="lua" line highlight="1,13">
 +
errorOnLookupFailure(false)
 +
 
 +
local function checkPointer()
 +
  local address = getAddress("[[[game.exe+1234]+10]+20]+30")
 +
 
 +
  if address == 0 then
 +
    return nil
 +
  end
 +
 
 +
  return readInteger(address)
 +
end
 +
 
 +
errorOnLookupFailure(true)
 +
</syntaxhighlight>
 +
 
 +
{{LuaSeeAlso}}
 +
 
 +
{{Address}}

Latest revision as of 22:57, 26 June 2026

<> Lua API Reference

function errorOnLookupFailure(state) : void

Controls whether address and symbol lookup failures raise an error.

If enabled, address lookups in string form will raise an error when the address cannot be resolved. This includes undefined symbols and invalid pointers.

If disabled, failed lookups return 0 instead. This is useful for pointers or symbols that may not be valid all the time.

By default, lookup failures raise an error.

Function Parameters[edit]

Parameter Type Description
state Boolean If true, lookup failures raise an error. If false, lookup failures return 0.

Returns[edit]

This function does not return a value.

Examples[edit]

Disable lookup errors[edit]

1 errorOnLookupFailure(false)

Enable lookup errors[edit]

1 errorOnLookupFailure(true)

Use with an unstable pointer[edit]

1 errorOnLookupFailure(false)
2 
3 local address = getAddress("[[[game.exe+1234]+10]+20]+30")
4 
5 if address == 0 then
6   print("Pointer could not be resolved")
7 else
8   print(string.format("%X", address))
9 end

Restore the default behavior after a safe lookup[edit]

 1 errorOnLookupFailure(false)
 2 
 3 local address = getAddress("SomeOptionalSymbol")
 4 
 5 if address == 0 then
 6   print("Symbol not available")
 7 end
 8 
 9 -- Restore default behavior
10 errorOnLookupFailure(true)

Use getAddressSafe instead of changing the global behavior[edit]

1 local address = getAddressSafe("SomeOptionalSymbol")
2 
3 if address == nil then
4   print("Symbol not available")
5 end

Temporarily suppress lookup errors[edit]

 1 local function lookupOptionalAddress(symbol)
 2   errorOnLookupFailure(false)
 3 
 4   local address = getAddress(symbol)
 5 
 6   errorOnLookupFailure(true)
 7 
 8   if address == 0 then
 9     return nil
10   end
11 
12   return address
13 end

Handle optional symbols[edit]

 1 errorOnLookupFailure(false)
 2 
 3 local symbols = {
 4   "OptionalSymbolA",
 5   "OptionalSymbolB",
 6   "OptionalSymbolC"
 7 }
 8 
 9 for i, symbol in ipairs(symbols) do
10   local address = getAddress(symbol)
11 
12   if address ~= 0 then
13     print(symbol .. " = " .. string.format("%X", address))
14   end
15 end
16 
17 errorOnLookupFailure(true)

Avoid pointer lookup crashes in repeated checks[edit]

 1 errorOnLookupFailure(false)
 2 
 3 local function checkPointer()
 4   local address = getAddress("[[[game.exe+1234]+10]+20]+30")
 5 
 6   if address == 0 then
 7     return nil
 8   end
 9 
10   return readInteger(address)
11 end
12 
13 errorOnLookupFailure(true)

Main Pages

Core Lua documentation entry points

Lua
Script Engine

Symbol Related Lua Functions

Symbol lookup, address resolution, module checks, and lookup callbacks