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 


Possible to have too many if statements?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
microsoftv
Advanced Cheater
Reputation: 0

Joined: 07 Apr 2016
Posts: 82
Location: U.S

PostPosted: Fri Jul 21, 2017 7:14 pm    Post subject: Possible to have too many if statements? Reply with quote

So, the code I'm trying to run is over 1k lines, and I've just run into a problem regarding adding a new if statement that assigns a nil variable a new value, but when trying to use that variable it is still nil, as though the if statement that assigns the value doesn't even exist. The whole script is assigned to the table, which requires it to be in an assembly script. Haven't experienced something like this till now. Another note: everything before the error still works correctly.
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 137

Joined: 25 Jan 2006
Posts: 7284
Location: 127.0.0.1

PostPosted: Fri Jul 21, 2017 9:19 pm    Post subject: Reply with quote

You can hit a stack limit if you nest too much but if Lua did not explode and complain about hitting that limit you should be fine. Double check your code and make sure you didn't overlook something or if one of the checks are out of order or has a typo.
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
microsoftv
Advanced Cheater
Reputation: 0

Joined: 07 Apr 2016
Posts: 82
Location: U.S

PostPosted: Fri Jul 21, 2017 9:41 pm    Post subject: Reply with quote

atom0s wrote:
You can hit a stack limit if you nest too much but if Lua did not explode and complain about hitting that limit you should be fine. Double check your code and make sure you didn't overlook something or if one of the checks are out of order or has a typo.

Right now I have 2 if statements, one that assigns the value to the variable, and another that uses the variable after it's assigned the value, before their requirements are met, everything works correctly(things before and after). When their requirements are met, the if using the variable reports an error of a nil variable. When adding a return to the if assigning(includes an elseif), the return never occurs (even elseif), even though requirements are met 100%.
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 137

Joined: 25 Jan 2006
Posts: 7284
Location: 127.0.0.1

PostPosted: Fri Jul 21, 2017 9:47 pm    Post subject: Reply with quote

Without seeing the actual code it is going to be a bit rough for anyone else to help much with this.
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
microsoftv
Advanced Cheater
Reputation: 0

Joined: 07 Apr 2016
Posts: 82
Location: U.S

PostPosted: Fri Jul 21, 2017 10:04 pm    Post subject: Reply with quote

atom0s wrote:
Without seeing the actual code it is going to be a bit rough for anyone else to help much with this.

Shortened for ease of reading:
Code:
[ENABLE]
{$lua}

if(t ~= nil) then -- if script is already running then
    timer_setEnabled(t, false) -- stop the script
    object_destroy(t)
    t = nil
end

tickrate = 1 -- 50ms is every 3 frames

function main ()
      ball_rise = nil
      ball_run = nil
      ball_run2 = nil
      stage_xmax = nil
      stage_xmin = nil
      stage_ymax = nil
      stage_ymin = nil
      dxpred = nil
      dypred = nil

--Mountain
      s0_xmin = 2162688
      s0_ymin = 2162688
      s0_xmax = 81723392
      s0_ymax = 33423360
end

t = createTimer(nil, false)  -- create a Timer object and assign it to variable t
timer_onTimer(t, main)   -- When the timer ticks, call the function main
timer_setInterval(t, tickrate) -- Sets the tickrate of the timer in milliseconds
timer_setEnabled(t, true) -- Turns the timer on -- Lua code here

setGlobalKeyPollInterval(1)
setGlobalDelayBetweenHotkeyActivation(1)

if hk
   then hk.destroy()
        end

local oldT = getTickCount()  -- resolution isn't great, but good enough
local hk = createHotkey(function()
local nowT = getTickCount()
  oldT = nowT

--excluded assigning of local variables

--Starting the meat of the problem:

       if stage == 0
          then stage_xmax = s0_xmax
               stage_xmin = s0_xmin
               stage_ymax = s0_ymax
               stage_ymin = s0_ymin
               ball_rise = (stage_ymax-bally)
               ball_run = (18350080-((524288)^((bally-2548097)/908096)))
               ball_run2 = (18350080-524288)
               end

--Begin main if statement(assigner if statement in question)

       if requirements == met
          then dxpred = ((ballx+ball_run)-p1x)
                   dypred = (p1y-(bally+ball_rise))
       elseif requirements == met
                  then nil_variable1 = ((ballx+ball_run2)-p1x)
                       nil_variable2 = (p1y-(bally+ball_rise))
                       end

--Begin use of variables(user if statement in question)

       if math.abs(dypred) == met
--gives error: "bad argument #1 to 'abs' (number expected, got nil)"
              then keyDown(VK_Z)
                   sleep(1)
                   keyUp(VK_Z)
                   end
       end, VK_D)

hk.DelayBetweenActivate = 1

{$asm}
//Start of Assembly Script
[DISABLE]
//End of  Assembly Script
Back to top
View user's profile Send private message
FreeER
Master Cheater
Reputation: 10

Joined: 09 Aug 2013
Posts: 326

PostPosted: Sat Jul 22, 2017 10:03 am    Post subject: Reply with quote

well based on what you've shown you're still trying to get the absolute value even if the requirements weren't met, which would mean the value is still nil because you only set it when the requirements are met. If the requirements aren't met and there's nothing else you can do then just return from the function, otherwise you have to nest the code in if statements checking that they've been met or that the variables are not nil etc. (but I find a simple "if not ableToWork then return end" cleaner)
Back to top
View user's profile Send private message
Prehistoricman
Advanced Cheater
Reputation: 0

Joined: 02 Aug 2016
Posts: 53

PostPosted: Sat Jul 22, 2017 12:45 pm    Post subject: Reply with quote

Jesus, your indentation reminds me of an escalator.

Anyway... from the sounds of it, it's likely going to be a localisation error (you've made a variable local, and therefore you can't access it later) or it an issue with planning your variables. Is the code that's using the variable making sure that the variable exists?

In times like these, I find it useful to spam prints for a good sanity check so you know exactly what is and what is not working.

_________________
Er, hi
Back to top
View user's profile Send private message
microsoftv
Advanced Cheater
Reputation: 0

Joined: 07 Apr 2016
Posts: 82
Location: U.S

PostPosted: Sat Jul 22, 2017 2:29 pm    Post subject: Reply with quote

FreeER wrote:
well based on what you've shown you're still trying to get the absolute value even if the requirements weren't met, which would mean the value is still nil because you only set it when the requirements are met. If the requirements aren't met and there's nothing else you can do then just return from the function, otherwise you have to nest the code in if statements checking that they've been met or that the variables are not nil etc. (but I find a simple "if not ableToWork then return end" cleaner)

I've already said that even if they are met, the variable continues to be nil, no matter if global or locally assigned as nil first.
Prehistoricman wrote:
Anyway... from the sounds of it, it's likely going to be a localisation error (you've made a variable local, and therefore you can't access it later) or it an issue with planning your variables. Is the code that's using the variable making sure that the variable exists?

In times like these, I find it useful to spam prints for a good sanity check so you know exactly what is and what is not working.

The variables assigned at the same place (i.e everything that is assigned = nil at the top) is useable by if statements. The first if statement works correctly:
Code:
if stage == 0
then stage_xmax = s0_xmax
stage_xmin = s0_xmin
stage_ymax = s0_ymax
stage_ymin = s0_ymin
ball_rise = (stage_ymax-bally)
ball_run = (18350080-((524288)^((bally-2548097)/908096)))
ball_run2 = (18350080-524288)
end
, assigns everything correctly (I checked by printing them), and leads into the next if statement which is supposed assign things, but doesn't (even adding returns or prints don't work, and i'm 100% sure the requirements are met):
Code:

if requirements == met
then dxpred = ((ballx+ball_run)-p1x)
dypred = (p1y-(bally+ball_rise))
elseif requirements == met
then dxpred = ((ballx+ball_run2)-p1x)
dypred = (p1y-(bally+ball_rise))
end
Back to top
View user's profile Send private message
mgr.inz.Player
I post too much
Reputation: 148

Joined: 07 Nov 2008
Posts: 4186
Location: W kraju nad Wisla. UTC+01:00

PostPosted: Sat Jul 22, 2017 3:09 pm    Post subject: Reply with quote

You have a timer which constantly sets dypred to nil. You have to use a temporary variable, e.g. dypredTmp:
Code:
local dypredTmp

if requirements == met then
  dxpred = ((ballx+ball_run)-p1x)
  dypredTmp = (p1y-(bally+ball_rise))
  dypred = dypredTmp

elseif requirements == met then
  dxpred = ((ballx+ball_run2)-p1x)
  dypred = (p1y-(bally+ball_rise))
end

if math.abs(dypredTmp) == met then
  keyDown(VK_Z)
  sleep(1)
  keyUp(VK_Z)
end




Why you are using a timer anyway?

_________________
Back to top
View user's profile Send private message MSN Messenger
microsoftv
Advanced Cheater
Reputation: 0

Joined: 07 Apr 2016
Posts: 82
Location: U.S

PostPosted: Sat Jul 22, 2017 4:05 pm    Post subject: Reply with quote

mgr.inz.Player wrote:
You have a timer which constantly sets dypred to nil. You have to use a temporary variable, e.g. dypredTmp. Why you are using a timer anyway?

The other variables I set to nil in the same place as dypred when I had a timer all could be changed and would seem to stay those values, since they were printed as such. I just deleted the timer, but even when setting a local dypredTmp inside the hotkey function, it continues to say it is nil at the math.abs if statment, this is what I changed the script to:
Code:

[ENABLE]
{$lua}

p1h = nil
p1w = nil
p2h = nil
p2w = nil
p3h = nil
p3w = nil
p4h = nil
p4w = nil
ball_rise = nil
ball_run = nil
ball_run2 = nil
stage_xmax = nil
stage_xmin = nil
stage_ymax = nil
stage_ymin = nil
--Used as a placeholders to be later replaced
s0_xmin = 2162688
s0_ymin = 2162688
s0_xmax = 81723392
s0_ymax = 33423360

setGlobalKeyPollInterval(1)
setGlobalDelayBetweenHotkeyActivation(1)

if hk
   then hk.destroy()
        end

local hk = createHotkey(function()

       local stage = readInteger(pointer)
       local dxpredTmp
       local dypredTmp
--Beginning Start of problem
if stage == 0
          then stage_xmax = s0_xmax
               stage_xmin = s0_xmin
               stage_ymax = s0_ymax
               stage_ymin = s0_ymin
               ball_rise = (stage_ymax-bally)
               ball_run = (18350080-((524288)^((bally-2548097)/908096)))
               ball_run2 = (18350080-524288)
end
if requirements == met
   then dxpredTmp = ((ballx+ball_run)-p1x)
          dypredTmp = (p1y-(bally+ball_rise))
elseif requirements == met
        then dxpredTmp = ((ballx+ball_run2)-p1x)
                dypredTmp = (p1y-(bally+ball_rise))
end
       if requirements == met
          and math.abs(dypredTmp) <= p1h
--Gives error here, dypredTmp is seen as nil
          and math.abs(dxpredTmp) <= p1w
              then keyDown(VK_Z)
                   sleep(1)
                   keyUp(VK_Z)
                   end

       end, VK_D)

-- If you don't want to affect all other hotkeys, just change this hotkey:
hk.DelayBetweenActivate = 1

{$asm}
//Start of Assembly Script
[DISABLE]
//End of  Assembly Script
Back to top
View user's profile Send private message
mgr.inz.Player
I post too much
Reputation: 148

Joined: 07 Nov 2008
Posts: 4186
Location: W kraju nad Wisla. UTC+01:00

PostPosted: Sun Jul 23, 2017 1:54 am    Post subject: Reply with quote

Try this:
Code:
       if (requirements == met)
          and (dypredTmp ~= nil)
          and (dxpredTmp ~= nil)
          and (math.abs(dypredTmp) <= p1h)
          and (math.abs(dxpredTmp) <= p1w)
              then keyDown(VK_Z)
                   sleep(1)
                   keyUp(VK_Z)
                   end

_________________
Back to top
View user's profile Send private message MSN Messenger
Prehistoricman
Advanced Cheater
Reputation: 0

Joined: 02 Aug 2016
Posts: 53

PostPosted: Sun Jul 23, 2017 10:12 am    Post subject: Reply with quote

microsoftv wrote:

Prehistoricman wrote:
Anyway... from the sounds of it, it's likely going to be a localisation error (you've made a variable local, and therefore you can't access it later) or it an issue with planning your variables. Is the code that's using the variable making sure that the variable exists?

In times like these, I find it useful to spam prints for a good sanity check so you know exactly what is and what is not working.

The variables assigned at the same place (i.e everything that is assigned = nil at the top) is useable by if statements. The first if statement works correctly:
Code:
if stage == 0
then stage_xmax = s0_xmax
stage_xmin = s0_xmin
stage_ymax = s0_ymax
stage_ymin = s0_ymin
ball_rise = (stage_ymax-bally)
ball_run = (18350080-((524288)^((bally-2548097)/908096)))
ball_run2 = (18350080-524288)
end
, assigns everything correctly (I checked by printing them), and leads into the next if statement which is supposed assign things, but doesn't (even adding returns or prints don't work, and i'm 100% sure the requirements are met):
Code:

if requirements == met
then dxpred = ((ballx+ball_run)-p1x)
dypred = (p1y-(bally+ball_rise))
elseif requirements == met
then dxpred = ((ballx+ball_run2)-p1x)
dypred = (p1y-(bally+ball_rise))
end


Print out requirements and met. And their types too, as 2 does not equal "2", for example.
And if you find that the if statement works correctly, print dxpred throughout your program and see where you lose it

_________________
Er, hi
Back to top
View user's profile Send private message
microsoftv
Advanced Cheater
Reputation: 0

Joined: 07 Apr 2016
Posts: 82
Location: U.S

PostPosted: Sun Jul 23, 2017 2:26 pm    Post subject: Reply with quote

mgr.inz.Player wrote:
Try this:
Code:
       if (requirements == met)
          and (dypredTmp ~= nil)
          and (dxpredTmp ~= nil)
          and (math.abs(dypredTmp) <= p1h)
          and (math.abs(dxpredTmp) <= p1w)
              then keyDown(VK_Z)
                   sleep(1)
                   keyUp(VK_Z)
                   end

Now it simply does nothing, as it probably stops at dypred ~= nil.
Quote:
Print out requirements and met. And their types too, as 2 does not equal "2", for example.
And if you find that the if statement works correctly, print dxpred throughout your program and see where you lose it

I have other if statements that use the same requirements, and they work correctly, even having same met values. Printing dxpred or dypred says nil no matter where it's set.

EDIT: Seems like it's reading requirements as a 4byte instead of a byte, re-checking if the statements work again
EDIT: They do, can't believe such a simple thing got me again...
Back to top
View user's profile Send private message
dl748
Advanced Cheater
Reputation: 0

Joined: 05 Mar 2016
Posts: 54

PostPosted: Sun Jul 23, 2017 11:59 pm    Post subject: Reply with quote

atom0s wrote:
You can hit a stack limit if you nest too much but if Lua did not explode and complain about hitting that limit you should be fine. Double check your code and make sure you didn't overlook something or if one of the checks are out of order or has a typo.


There is a stack limit on IF statements? Sounds very silly. I could see a stack limit on function calls, but not if statements.
Back to top
View user's profile Send private message
Prehistoricman
Advanced Cheater
Reputation: 0

Joined: 02 Aug 2016
Posts: 53

PostPosted: Mon Jul 24, 2017 9:27 am    Post subject: Reply with quote

dl748 wrote:
atom0s wrote:
You can hit a stack limit if you nest too much but if Lua did not explode and complain about hitting that limit you should be fine. Double check your code and make sure you didn't overlook something or if one of the checks are out of order or has a typo.


There is a stack limit on IF statements? Sounds very silly. I could see a stack limit on function calls, but not if statements.


I just stacked 240 if statements and I got the error "chunk has too many syntax levels". You see, each if statement has its own scope. Try this:

Code:

if true then
    local a = 1
end
print(a)


It outputs nil.

There's also a limit on the number of local variables. The limit applies to your whole program, not a single scope. Upon exceeding 200 total local variables, you will get the error:

main function has more than 200 local variables

_________________
Er, hi
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
Goto page 1, 2  Next
Page 1 of 2

 
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