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 


Conditional If's in a Hotkey Toggle?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine
View previous topic :: View next topic  
Author Message
Hyperglyph
Newbie cheater
Reputation: 0

Joined: 12 Jan 2017
Posts: 12

PostPosted: Fri Jan 13, 2017 7:18 pm    Post subject: Conditional If's in a Hotkey Toggle? Reply with quote

I've been trying to assemble my first Trainer. Using the built-in generator I created a simple one... but now I'd like to have the same hotkey change values based on another addresses current value.

This is what I've added so far to the Lua script the generator made.
Code:

addresslist=getAddressList()

function onHotkey0(Hotkey)
  --Executed before the hotkey is handled
  local MatchType
  for i=0,addresslist.Count-1 do
   local CurrentMemRec=addresslist.getMemoryRecord(i);
   if (CurrentMemRec.Description=="ZZZ") then
       MatchType=CurrentMemRec.Value;
   end
  end
  for i=0,addresslist.Count-1 do
      if (MatchType==0) then
           if (CurrentMemRec.Description=="A") then
                  CurrentMemRec.Value=256;
          elseif (CurrentMemRec.Description=="B") then
                  CurrentMemRec.Value=128;
           end
      elseif (MatchType==3) then
           if (CurrentMemRec.Description=="A") then
                  CurrentMemRec.Value=64;
          elseif (CurrentMemRec.Description=="B") then
                  CurrentMemRec.Value=32;
           end
      end
  end
end


For whatever reason, this isn't working. Only the first value ("A") is ever changed. Plus, the second MatchType (==3) will never run... even if I explicitly declare MatchType=3; before the second loop starts.

I'm new to Lua and have no idea what I'm doing wrong here. Printing the value of MatchType returns the correct value (although I don't know if it's a string or a number). I've tried using memoryrecord_getValue/setValue. I've tried comparison checks for strings (=="3") and tonumber to convert a potential string to a decimal.

Nothing's working and I'm starting to wonder why.

Anyone have any thoughts/suggestions?

Edit: Oops, just realized I posted this in general instead of Lua.
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Fri Jan 13, 2017 7:25 pm    Post subject: Reply with quote

You can already retrieve a memory record by its description:
Code:
local MatchType = addresslist.getMemoryRecordByDescription("ZZZ").Value

Your second loop is missing the most important part Smile
Code:
local CurrentMemRec=addresslist.getMemoryRecord(i);
Back to top
View user's profile Send private message
Hyperglyph
Newbie cheater
Reputation: 0

Joined: 12 Jan 2017
Posts: 12

PostPosted: Fri Jan 13, 2017 7:55 pm    Post subject: Reply with quote

Code:

function onHotkey0(Hotkey)
  --Executed before the hotkey is handled
 local MatchType =addresslist.getMemoryRecordByDescription("ZZZ").Value
  for i=0,addresslist.Count-1 do
      local CurrentMemRec=addresslist.getMemoryRecord(i);
      if (MatchType==0) then
           if (CurrentMemRec.Description=="A") then
                  CurrentMemRec.Value=256;
           elseif (CurrentMemRec.Description=="B") then
                  CurrentMemRec.Value=128;
           end
      elseif (MatchType==3) then
           if (CurrentMemRec.Description=="A") then
                  CurrentMemRec.Value=64;
           elseif (CurrentMemRec.Description=="B") then
                  CurrentMemRec.Value=32;
           end
      end
end
end


That missing local CurrentMemRec was a copy&paste error when I posted. (This is a condensed version, actual code has about 30 =="Letter" statements in each MatchType block). Ironically, I HAD actually forgotten to include it earlier, so I double checked and yeah, it's already in there.

I replaced the first loop with your recommendation. I'm still making a mistake somewhere, though. The MatchType==3 block still doesn't run (even when it's explicitly set) and no values after "A" are ever changed.

It's like the conditionals are stopping after the first statement, or something. I'm continuing to test.

Edit:
Code:

elseif (MatchType==3) then
print(MatchType)


fails to print anything when the script is executed. So I know for sure now that the block isn't running at all.

... and yes, I've checked the addresses value. It IS still 3 as I'm staring at it right now with the game paused. Also, it's a static address.

Edit2:
Further print tests show that the (MatchType==0) block isn't printing anything either (which it shouldn't) and printing MatchType inside the loop and before the conditionals still prints the correct value of 3. No idea why that 3 isn't the same as (MatchType==3).
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4289

PostPosted: Fri Jan 13, 2017 8:21 pm    Post subject: Reply with quote

The value property is a string. The literal 3 is a number.
Code:
"3" == 3   -- false
"3" == tostring(3)  -- true
tonumber("3") == 3  -- true

_________________
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
Hyperglyph
Newbie cheater
Reputation: 0

Joined: 12 Jan 2017
Posts: 12

PostPosted: Fri Jan 13, 2017 8:30 pm    Post subject: Reply with quote

Yes, I read that in the engine's documentation just a moment ago. So, the (MatchType==3) block now works with
Code:

 local MatchType = tonumber(addresslist.getMemoryRecordByDescription("ZZZ").Value)

instead of
Code:
 local MatchType =addresslist.getMemoryRecordByDescription("ZZZ").Value


So, that's one issue solved. I still haven't figured out why the address values aren't being written properly. I know "A" was working at one point, but it doesn't appear to be working now (at least, the one in the ==3 block isn't).

I just did a print test and the address IS being found (if... =="A" then print("A") end). It just isn't writing the value for some reason.

Could this have something to do with literals vs strings again?

Edit:
Thought I was being an idiot since I just realized I've been writing a literal to a .Value property, again... but then I tried with
Code:
memoryrecord_setValue(CurrentMemRec,64)
and that didn't work either....

Edit2:
A print test of print(CurrentMemRec.Value) immediately after setting it returns the correct value of 64. I guess this means it's somehow getting reset to its original value by my code? It can't be the game, since the process is paused AND I know I can manually changed it without it reverting back when the it resumes.
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4289

PostPosted: Fri Jan 13, 2017 8:46 pm    Post subject: Reply with quote

If this line of code is being executed:
Code:
CurrentMemRec.Value=64

Then the value 64 is getting written to that memory record just as if you changed it yourself. It doesn't matter if it's a string or a number in this case.

If you're absolutely certain the game isn't changing anything, then something else you're doing is changing it.

Don't use those classname_method functions unless you see them documented in main.lua. You're still writing the literal 64 to CurrentMemRec (in a less readable way).

_________________
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
Hyperglyph
Newbie cheater
Reputation: 0

Joined: 12 Jan 2017
Posts: 12

PostPosted: Fri Jan 13, 2017 8:51 pm    Post subject: Reply with quote

Gotcha. I reverted to the original .Value writing.

Further tests have cleared up a lot of what's happening. For some reason, in the event of a 3, if (MatchType==3) DOES run... and all of the letter blocks do change their respective values. However, the address from "A" is then inexplicably set to the value from the (MatchType==0) "A" block (Note, still not it's original value) sometime after the function terminates. This sounds like a typo somewhere, and I'm looking for it.

Edit:
Yea, print tests confirm the value is getting set to the first block's "A" value about halfway through the loop... which is weird since the tests also confirm that the first block (MatchType==0) never runs when the MatchType != 0.

I'm thinking there's some holdover from my very first efforts when I used the trainer generator to create a hotkey that simply set "A" to the MatchType==0 value. That was before I added a second conditional to the generated script.

Edit2:
Yea, that was the issue. I guess the settings in the generator override whatever the Lua script is. I changed the values from the generator hotkeys so now they all have an address and "Set Value to" but no actual value to write there. It seems to work, the script-written values aren't being overwritten anymore.
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 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