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 


Arithmetic on a nil value

 
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: 84

PostPosted: Thu Aug 13, 2020 9:04 am    Post subject: Arithmetic on a nil value Reply with quote

I have a timer-hotkey script that performs teleportation,it checks whether a key is pressed or not every 10 milliseconds and if it is pressed then it calls a function boost.The boost function reads 3 values and stores those values into 3 local variables namely x,y,z.Now it performs some calculations on these 3 local variables and stores the result into the same memory locations(from which it read) only after checking whether a key is pressed or not.The problem is that when I press the key that performs the reading and then when I press the key that stores the values of x,y,z back to the memory location,it shows an error like "performing arithmetic on a nil value global z".

I dont know how z is global and how it is nil?

Here is the lua script:
Code:

{$lua}

if syntaxcheck then return end
[ENABLE]

local function boost()

         if isKeyPressed(VK_NUMPAD6) then

                   local   x=readFloat("[loc]")
                     local  y=readFloat("[loc]+4")
                      local z=readFloat("[loc]+8")
         end
         if isKeyPressed(VK_NUMPAD2) then
                      writeFloat("[loc]",x)
                      writeFloat("[loc]+4",y)
                      writeFloat("[loc]+8",z+2)
                      end
                      end
            t=createTimer(nil)
            timer_setInterval(t,10)
            timer_onTimer(t,boost)
            timer_setEnabled(t,true)

[DISABLE]
timer_setEnabled(t,false)
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 62

Joined: 01 Oct 2008
Posts: 958

PostPosted: Thu Aug 13, 2020 10:07 am    Post subject: Reply with quote

This [then] local x ... [end] form a scope block, said B, environment outside this block cannot see this [local x] value.
There may be other x declaration outside this block,
but not referenced the intended x,y,z declaration within the block B.

Move the local x,y,z declaration (only declaration, no need also the assgnemnt) should fix it,ie.
Code:

local x,y,z
local function boost()
--  local x,y,z  --> move outside again
  if ... then
    x,y,z = read ...
  end
  if VK_NUMPAD2... and x and y and z then -- make sure x,y,z is not nil
    write..x,y,z
  end
  ...
end -- boost


ADDED:
ok, so x,y,z should still be nil in if-(VK_NUMPAD2)-then-block if VK_NUMPAD6 is not pressed, because x,y,z only live within this function.

Either use global x,y,z (bad idea) or again move [local x,y,z] outsize the function block.

Also you should check x,y,z before writing their x,y,z values


ADDED:
use createHotkey function should no need the timer, ie.
Code:

-- reset globally defined hotkey handler
if HK_teleport_save then HK_teleport_save = nil,HK_teleport_save.Destroy()end
if HK_teleport_load then HK_teleport_load = nil,HK_teleport_load.Destroy()end

local xsave,ysave,zsave
HK_teleport_save = CreateHotkey(function()
  xsave, ysave, zsave = readFloat'[loc]',readFloat'[loc]+4',readFloat'[loc]+8'
end,VK_NUMPAD6)
HK_teleport_load = CreateHotkey(function()
  if xsave and ysave and zsave then
    writeFloat('[loc]',xsave)
    writeFloat('[loc]+4',ysave)
    writeFloat('[loc]+8',zsave)
  end
end,VK_NUMPAD2)


_________________
- Retarded.
Back to top
View user's profile Send private message
sgsgwv$6263
Advanced Cheater
Reputation: 0

Joined: 05 Aug 2020
Posts: 84

PostPosted: Thu Aug 13, 2020 11:08 am    Post subject: Reply with quote

Thanks a lot dude.
I was so frustrated with the problem that I implemented this teleport hack in assembly.
But your solution really helped me out.

Why did you do the declaration twice (in the first code)?

I was aware of the CreateHotkey() but never tried it because I didn't know how to use it.
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 62

Joined: 01 Oct 2008
Posts: 958

PostPosted: Thu Aug 13, 2020 12:33 pm    Post subject: Reply with quote

Quote:
declaration twice

The two minus in lua begin a line comment

_________________
- Retarded.
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