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 


lua code works only when I use print()

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
sgsgwv$6263
Advanced Cheater
Reputation: 0

Joined: 05 Aug 2020
Posts: 82

PostPosted: Tue Jan 14, 2025 2:38 am    Post subject: lua code works only when I use print() Reply with quote

Here is my code:
Code:

{$lua}


[ENABLE]

local mov_speed=25
local jumpSpeed=20

x=getAddressList().getMemoryRecordByDescription("player x speed")
y=getAddressList().getMemoryRecordByDescription("player y speed")
z=getAddressList().getMemoryRecordByDescription("player z speed")
xd=getAddressList().getMemoryRecordByDescription("x speed down")
yd=getAddressList().getMemoryRecordByDescription("y speed down")
zd=getAddressList().getMemoryRecordByDescription("z speed down")

local upSpeed=jumpSpeed

function getUpSpeed(val,direction)
if direction == "up" then
   if val>0 then
      val=val*2
   else
       if val>-1 and val<0 then
          val=1
       else
           val=val/2
       end
   end
else
    if val<0 then
       val=val*2
    else
        if val<1 then
           val=-1
        else
           val=val/2
        end
    end
end
return val
end


hk=createHotkey(function()
r=(((x.Value)^2)+((y.Value)^2))^0.5
if r==0 then return end
ratio=mov_speed/r
x.Value=x.Value*ratio
y.Value=y.Value*ratio
end,0x26,0x10) -- up and shift at the same time


jump=createHotkey(function()
while not isKeyPressed(0x53) do
  local xval=tonumber(x.Value)
  local yval=tonumber(y.Value)
  local zval=tonumber(z.Value)
  local xdval=tonumber(xd.Value)
  local ydval=tonumber(yd.Value)
  local zdval=tonumber(zd.Value)
  if tonumber(xval)==nil or tonumber(yval)==nil or tonumber(zval)==nil then
     break
  end


  r=(((xval)^2)+((yval)^2))^0.5
  if r==0 then return end
  ratio=mov_speed/r
  x.Value=xval*ratio
  y.Value=yval*ratio
  z.Value=upSpeed

  -- below if works only when I insert print(xdval) here
  if xdval~=nil then
     local r=(((xdval)^2)+((ydval)^2))^0.5
     if r==0 then return end
     ratio=mov_speed/r
     xd.Value=xdval*ratio
     yd.Value=ydval*ratio
     zd.Value=upSpeed
  end

  if isKeyPressed(0x55) then -- U
     upSpeed=getUpSpeed(upSpeed,"up")
     keyUp(0x55)
  end
  if isKeyPressed(0x44) then -- D
     upSpeed=getUpSpeed(upSpeed,"down")
     keyUp(0x44)
  end
end
upSpeed=jumpSpeed
x.Value=0
y.Value=0
end,0x26,0x20) -- up and space at the same time



[DISABLE]
hk.destroy()
jump.destroy()


Here, it looks like `if xdval~=nil then` doesnot get executed at all. But surprisingly when I insert `print(xdval)` before that line, the same if condition gets executed perfecltly. This is wierd. I am not sure why is it happening? Can you some help me on this?
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 34

Joined: 16 Feb 2017
Posts: 1442

PostPosted: Wed Jan 15, 2025 8:16 am    Post subject: Reply with quote

Try this.
I also found it odd that you assigned the "r" below but used it independently above. Smile

Code:
  if xdval==nil then
     print("'xdval' not found!")
     print("'xdval' redefined!")
     xdval=tonumber(xd.Value)
  end
     local r=(((xdval)^2)+((ydval)^2))^0.5
     if r==0 then return end
     ratio=mov_speed/r
     xd.Value=xdval*ratio
     yd.Value=ydval*ratio
     zd.Value=upSpeed
  --end

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
sgsgwv$6263
Advanced Cheater
Reputation: 0

Joined: 05 Aug 2020
Posts: 82

PostPosted: Sat Jan 18, 2025 12:18 am    Post subject: Reply with quote

Sorry, I forgot about this question.

Unfortunately that didn't solve the problem. Its hard to idemtify the issue because when you are trying to debug only then its working lol. I have faced this problem before in reactjs where request promise completed only when i debug it turns out the server sent the request first then did what it was supposed to do. It might be concurrency related issue but still at some point that xd.Value should be numeric string. I can tell you that it gets modifed but to a value that is unexpected.


Also r doesnot matter too much here.

I kinda solved the problem by using timers instead of loops inside hotkeys. That is my lesson from now on. Don't do long running tasks on hotkeys. Better to create timers and use hotkeys to enable or disable the timer.
Back to top
View user's profile Send private message
daspamer
Grandmaster Cheater Supreme
Reputation: 54

Joined: 13 Sep 2011
Posts: 1584

PostPosted: Tue Jan 21, 2025 7:27 pm    Post subject: Reply with quote

Can you upload a table?
_________________
I'm rusty and getting older, help me re-learn lua.
Back to top
View user's profile Send private message Visit poster's website
sgsgwv$6263
Advanced Cheater
Reputation: 0

Joined: 05 Aug 2020
Posts: 82

PostPosted: Wed Jan 22, 2025 7:04 am    Post subject: Reply with quote

Hi daspamer, this is the cheat table.

Last edited by sgsgwv$6263 on Thu Jan 23, 2025 6:35 am; edited 1 time in total
Back to top
View user's profile Send private message
daspamer
Grandmaster Cheater Supreme
Reputation: 54

Joined: 13 Sep 2011
Posts: 1584

PostPosted: Wed Jan 22, 2025 3:13 pm    Post subject: Reply with quote

xdval is unknown.
Add local xdval above, or define xdval as local or with anything like nil or some value.
Did not test, but can remove the url.

_________________
I'm rusty and getting older, help me re-learn lua.
Back to top
View user's profile Send private message Visit poster's website
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