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 


How to access (read/write) to variable from a function?

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

Joined: 19 Jul 2013
Posts: 23
Location: Cayman Islands

PostPosted: Mon Sep 23, 2013 7:16 pm    Post subject: How to access (read/write) to variable from a function? Reply with quote

Hey guys Smile


I am writing another cheat trainer in Lua script with cheat engine's Generate trainer from table, & manually create trainer... instead of the usual trainer maker thingy.

My troubles seem to be with a variable that is written at the beginning of the Lua script that is not in a function. It has a value of "TEST", but when I access it from a function (button click), I can read "TEST" from the variable, but not write, or otherwise alter it.

Is there some way to set it up so it's accessible?

Maybe there's a way to call it properly so it can be edited?
I tried it with another variable that has a numeric value also at the beginning of the script outside of any function, & i can alter it as expected, so I'm confused.

Any help is awesome as usual Smile


EDIT:
Code:
--TRAINERGENERATORSTART--
--This is autogenerated code. Changing code in this block will
--get erased and rewritten if you regenerate the trainer code

--Uncomment the following line if this is a Cheat Table format trainer and you don't want CE to show (Tip, save as .CETRAINER alternatively)
--hideAllCEWindows()

RequiredCEVersion=6.3
if (getCEVersion==nil) or (getCEVersion()<RequiredCEVersion) then
  messageDialog('Please install Cheat Engine '..RequiredCEVersion, mtError, mbOK)
  closeCE()
end

TestVarNum = 1
TestVarString = "TEST"


function ButtonClick(sender)

CBO = component_findComponentByName(CETrainer,'CEComboBox1')
TestVarNum = getProperty(CBO,'ItemIndex')
messageDialog( "The combobox index is " .. TestVarNum .. ".", 4, 2 );

TestVarString = "AnotherTEST"
messageDialog( "The TestVarString value is " .. TestVarString .. ".", 4, 2 );

end



The above code is basically equivalent to my problem... TestVarNum works fine, but TestVarString is still set to what is was originally set to at the beginning of the script. Doesn't change.


I guess what I'm asking is, is it possible to alter a global variable's value from a function's local code block? If so, example?

_________________
Hitler... When all else fails, you'll be in the right state of mind. Jesus Saves.
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
DaSpamer
Grandmaster Cheater Supreme
Reputation: 52

Joined: 13 Sep 2011
Posts: 1578

PostPosted: Mon Sep 23, 2013 8:38 pm    Post subject: Reply with quote

Code:
TestVarNum = -1 -- The default is -1.
TestVarString = "TEST"


function ButtonClick(sender)
-- This is just extra work...
--[[
CBO = component_findComponentByName(CETrainer,'CEComboBox1')
TestVarNum = getProperty(CBO,'ItemIndex')
--]]
TestVarNum = CETrainer.CEComboBox1.ItemIndex -- Returns combo box index.
messageDialog( "The combobox index is " .. TestVarNum .. ".", 4, 2 );

TestVarString = "AnotherTEST"
messageDialog( "The TestVarString value is " .. TestVarString .. ".", 4, 2 );

end

Example of local and global.
Code:
function example1()
   local code = 1
   print(code); --> 1
end
function example2()
   code = 2
   print(code); --> 2
end

example1()
print(type(code)) --> nil because it was local
example2()
print(type(code)) --> 2, it's global

You shouldn't have any problems changing the var value.

_________________
HEY Hitler
Do you get lazy when making trainers?
Well no more!
My CETrainer will generate it for you in seconds, so you won't get lazy! Very Happy

http://forum.cheatengine.org/viewtopic.php?t=564919
Back to top
View user's profile Send private message
DTeCH
Newbie cheater
Reputation: 0

Joined: 19 Jul 2013
Posts: 23
Location: Cayman Islands

PostPosted: Mon Sep 23, 2013 9:17 pm    Post subject: Reply with quote

yea... that's the problem I have. Your example works for me, but it's numeric variables. This is why I'm confused. The problem shows up with string variables for me.

I think I'm gonna have to eye-ball the code, & go line by line to see if that variable is being re-written back to "TEST" some how, or something like that because this behavior is unusual.


Thanks for the info on the Extra Work bit Smile

ps: Is there a difference between single quote ' & double quote "?

_________________
Hitler... When all else fails, you'll be in the right state of mind. Jesus Saves.
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
DaSpamer
Grandmaster Cheater Supreme
Reputation: 52

Joined: 13 Sep 2011
Posts: 1578

PostPosted: Mon Sep 23, 2013 9:58 pm    Post subject: Reply with quote

Code:
function example1()
   local code = "1"
   print(code); --> 1 (String)
end
function example2()
   code = "2"
   print(code); --> 2 (String)
end

example1()
print(type(code)) --> nil because it was local string
example2()
print(type(code)) --> 2 (String), it's global


There's no difference between "" '' and [[ ]]
But for example to show this message
Code:
print("Hey there it's a string with ' char");

using ' instead of " will break it (because the it's word has the ').
Vice versa..

Code:
print('One day a guy said "LOL"');

using " here will break it too.

To avoid these two. just use this.
Code:
print([[Look I can do this " and that ' togther '' "" """ '' '' '' ']]);

But it does not parse for example new lines (\n) like other does.

_________________
HEY Hitler
Do you get lazy when making trainers?
Well no more!
My CETrainer will generate it for you in seconds, so you won't get lazy! Very Happy

http://forum.cheatengine.org/viewtopic.php?t=564919
Back to top
View user's profile Send private message
DTeCH
Newbie cheater
Reputation: 0

Joined: 19 Jul 2013
Posts: 23
Location: Cayman Islands

PostPosted: Mon Sep 23, 2013 11:43 pm    Post subject: Reply with quote

Well... I looked it over, & have to say that it's not being overwritten in any way. I have been eye-balling the code line by line from 8 AM this morning, & it's 11:30 PM. There is NO reason for this other than possibly corruption of cheat engine somehow in memory, or in it's DLLs. I really don't want to re-install, or whatever only to find out that there's a simple thing I've overlooked.

Here is a simple test i did, & it's still not working:

Code:
BUG = "nothing"
THIS = 2 --Let's say THIS = 2

function bugTest(sender)
    if THIS == '1' then
        BUG = "somestring" --Works if this is the first instance of BUG
    end
    if THIS == '2' then
        BUG = "something else"
    end
messageDialog( "The chosen broser is " .. BUG .. ".", 4, 2 );
--BUG is still = "nothing"  :(
end

_________________
Hitler... When all else fails, you'll be in the right state of mind. Jesus Saves.
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
DaSpamer
Grandmaster Cheater Supreme
Reputation: 52

Joined: 13 Sep 2011
Posts: 1578

PostPosted: Tue Sep 24, 2013 1:07 am    Post subject: Reply with quote

Code:
BUG = "nothing" --> String
THIS = 2 --> Number

function bugTest(sender)
    -- if THIS == '1' then --> Compare number with string
   if (THIS == 1) then -- If This equals to 1 then it'll define BUG as "somestring" and continue to the next if, if not, will continue to the next if without touching BUG.
        BUG = "somestring" -- Changes BUG
      print(BUG);
    end
    -- if THIS == '2' then --> Compare number with string
   if (THIS == 2) then  -- If This equals to 2 then it'll define BUG as "somestring" and continue to the next if, if not, will continue to the next if without touching BUG.
        BUG = "something else" -- Changes BUG
      print(BUG);
    end
   messageDialog( "The chosen broser is " .. BUG .. ".", 4, 2 );
   
end

_________________
HEY Hitler
Do you get lazy when making trainers?
Well no more!
My CETrainer will generate it for you in seconds, so you won't get lazy! Very Happy

http://forum.cheatengine.org/viewtopic.php?t=564919
Back to top
View user's profile Send private message
DTeCH
Newbie cheater
Reputation: 0

Joined: 19 Jul 2013
Posts: 23
Location: Cayman Islands

PostPosted: Tue Sep 24, 2013 1:21 am    Post subject: Reply with quote

That was the problem Smile

Trying to cmp Integer with String without some other function to convert String to Integer first.

The other languages I use auto-catches this, & converts it automatically, but LUA script seems strict. Smile

Thanks DaSpamer... You restored my sanity Very Happy

_________________
Hitler... When all else fails, you'll be in the right state of mind. Jesus Saves.
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
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