Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Stack Overflow Error

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
++METHOS
I post too much
Reputation: 92

Joined: 29 Oct 2010
Posts: 4197

PostPosted: Wed Jan 26, 2022 11:07 am    Post subject: Stack Overflow Error Reply with quote

I have this segment of code in my script:

Code:
    if (isKeyPressed(22535)) or (isKeyPressed(38)) then
        writeBytes('higher_input', 1);


Sometimes, CE is unable to determine was 'higher_input' is, and I receive a stack overflow error that spams CE to the point that I cannot do anything else and have to force end process.

Is there a way to check if 'higher_input' is nil or similar to avoid this error?

This is the entire lua:

Code:
[ENABLE]

{$lua}
_G._hotkeyTimer = createTimer();
_G._hotkeyTimer.Interval = 10;
_G._hotkeyTimer.OnTimer = function (t)
    if (isKeyPressed(22535)) or (isKeyPressed(38)) then
        writeBytes('higher_input', 1);
    else
        writeBytes('higher_input', 0);
    end
    if (isKeyPressed(22534)) or (isKeyPressed(40)) then
        writeBytes('lower_input', 1);
    else
        writeBytes('lower_input', 0);
    end
    if (isKeyPressed(22560)) or (isKeyPressed(87)) then
        writeBytes('north_input', 1);
    else
        writeBytes('north_input', 0);
    end
    if (isKeyPressed(22561)) or (isKeyPressed(83)) then
        writeBytes('south_input', 1);
    else
        writeBytes('south_input', 0);
    end
    if (isKeyPressed(22562)) or (isKeyPressed(68)) then
        writeBytes('east_input', 1);
    else
        writeBytes('east_input', 0);
    end
    if (isKeyPressed(22563)) or (isKeyPressed(65)) then
        writeBytes('west_input', 1);
    else
        writeBytes('west_input', 0);
    end
end

{$asm}



Code:
[DISABLE]

{$lua}
if (_G._hotkeyTimer ~= nil) then
    _G._hotkeyTimer.destroy();
    _G._hotkeyTimer = nil;
end

{$asm}





Thanks.



2022-01-26_11-09-45.png
 Description:
 Filesize:  15.91 KB
 Viewed:  1700 Time(s)

2022-01-26_11-09-45.png



2022-01-26_11-09-37.png
 Description:
 Filesize:  42.49 KB
 Viewed:  1700 Time(s)

2022-01-26_11-09-37.png


Back to top
View user's profile Send private message
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1053
Location: 0x90

PostPosted: Wed Jan 26, 2022 11:13 am    Post subject: Reply with quote

Something like this.
Code:

         if getAddress('higher_input') ~= nil then
            writeBytes('higher_input', 1);
         end
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 54

Joined: 01 Oct 2008
Posts: 941

PostPosted: Wed Jan 26, 2022 11:57 am    Post subject: This post has 2 review(s) Reply with quote

<Never mind, please ignore. I missed read again, sorry>

The logic of the series 'if key1 or key2 then else end' to writing the same address is tricky.
No matter what previous key is checked, the end result will only be the last 'if then else end'
May be you what a single check that if any of the said key pressed set to 1, else set to 0?

Not sure the cause of the stack overflow, but if it is cause by writing unreadable memory, then if can be avoid by checking if readable first,ie.
Code:

local readable = readSmallInteger'higher_input' -- may be readBytes work better, but older version readBytes will spawn error instead of return nil
if readable then
  if key1Pressed or key2Pressed or key3Pressed or key4Pressed or ... then
    write('higher_input',1)
  else
    write('higher_input',0)
  end
end

_________________
- Retarded.
Back to top
View user's profile Send private message
++METHOS
I post too much
Reputation: 92

Joined: 29 Oct 2010
Posts: 4197

PostPosted: Wed Jan 26, 2022 12:27 pm    Post subject: Reply with quote

LeFiXER wrote:
Something like this.
Code:

         if getAddress('higher_input') ~= nil then
            writeBytes('higher_input', 1);
         end
-Thanks. However, this did not solve the problem.

panraven wrote:

Code:

local readable = readSmallInteger'higher_input' -- may be readBytes work better, but older version readBytes will spawn error instead of return nil
if readable then
  if key1Pressed or key2Pressed or key3Pressed or key4Pressed or ... then
    write('higher_input',1)
  else
    write('higher_input',0)
  end
end
-So far, this seems to be working. Thanks.

I really appreciate everyone's help.
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Wed Jan 26, 2022 1:27 pm    Post subject: Reply with quote

The easiest way to solve this is to use errorOnLookupFailure. e.g. run this before createTimer:
Code:
errorOnLookupFailure(false)


getAddress will give an error on symbol lookup failure the same way as writeBytes. getAddressSafe won't.

I'd use getAddressSafe and use the return value from that. And put that duplicated code in a function.
Code:
local function writeBoolOnKeysPressed(address, ...)
  local a = getAddressSafe(address)
  if not a then return false end

  for _,v in ipairs{...} do
    if isKeyPressed(v) then
      return writeBytes(a, 1) == 1
    end
  end

  return writeBytes(a, 0) == 1
end

_G._hotkeyTimer.OnTimer = function (t)
  writeBoolOnKeysPressed('higher_input', 22535, 38)
  writeBoolOnKeysPressed('lower_input', 22534, 40)
  writeBoolOnKeysPressed('north_input', 22560, 87)
  writeBoolOnKeysPressed('south_input', 22561, 83)
  writeBoolOnKeysPressed('east_input', 22562, 68)
  writeBoolOnKeysPressed('west_input', 22563, 65)
end

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
++METHOS
I post too much
Reputation: 92

Joined: 29 Oct 2010
Posts: 4197

PostPosted: Wed Jan 26, 2022 1:34 pm    Post subject: Reply with quote

ParkourPenguin wrote:
I'd use getAddressSafe
-Thanks. I will revisit this if I experience any more errors. So far, so good. I appreciate your help.
Back to top
View user's profile Send private message
Frouk
Master Cheater
Reputation: 5

Joined: 22 Jun 2021
Posts: 489
Location: mov dword ptr [Ukraine]

PostPosted: Thu Jan 27, 2022 3:36 am    Post subject: Reply with quote

I had this issue because i haven't known lua yet
One more solution is check if opened process id equals to existing one
Code:
function IsAttached() --returns true if attached to game process else it will return false
return getOpenedProcessID() == getProcessIDFromProcessName("game.exe")
end
--[[
--Another function tho it might be used
function IsGameProcessExists()
return getProcessIDFromProcessName("game.exe") ~= nil
end
--]]

In timer insert line in first line of timer function
Code:
if not IsAttached() then return end

it might give some errors tho

_________________
void(__cdecl *Haxing)(HWND hGameWindow)
Back to top
View user's profile Send private message
++METHOS
I post too much
Reputation: 92

Joined: 29 Oct 2010
Posts: 4197

PostPosted: Thu Jan 27, 2022 3:53 am    Post subject: Reply with quote

Frouk wrote:
One more solution
-Thanks. I will try all other suggestions if I have any more problems. So far, panraven's solution is thankfully working without issues.

Thanks.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites