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 


Script To MemoryRecord
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
dl748
Advanced Cheater
Reputation: 0

Joined: 05 Mar 2016
Posts: 75

PostPosted: Sun Jan 01, 2017 5:08 pm    Post subject: Script To MemoryRecord Reply with quote

This may have been asked before but I can't seem to find a post about it.

How do you get the associated MemoryRecord (or table entry) to the script that was just activated or deactivated without using getMemoryRecordByDescription?

I tried looking for a variable or function that could give this. And it doesn't look like the call to autoassemble() passes this information in when called from TMemoryRecord.setActive().
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Sun Jan 01, 2017 5:40 pm    Post subject: Reply with quote

There is no "this" variable inside of the script to identify the currently running record.
You can, however, set a record's OnActivate and OnDeactive events to perform additional logic.
Code:
local record = getAddressList().getMemoryRecordByID(0)

record.OnActivate = function(record, before, currentstate)
  print("OnActivate"..(before and "Before " or "After ")..(currentstate and "true" or "false"))
  return true
end

record.OnDeactivate = function(record, before, currentstate)
  print("OnDeactivate"..(before and "Before " or "After ")..(currentstate and "true" or "false"))
  return true
end

I suppose you could loop through every record in your table and overwrite their OnActivate functions.
Have them all set a global "last_record" variable to the record and use that global variable in your scripts.
Code:
local records = getAddressList()
local activate = function(record, before, currentstate)
  last_record = record
  return true
end
local deactivate = function(record, before, currentstate)
  last_record = record
  return true
end
last_record = records[0]
for i = 0, records.Count - 1 do
  records[i].OnActivate = activate
  records[i].OnDeactivate = deactivate
end
Back to top
View user's profile Send private message
dl748
Advanced Cheater
Reputation: 0

Joined: 05 Mar 2016
Posts: 75

PostPosted: Sun Jan 01, 2017 6:32 pm    Post subject: Reply with quote

The only problem with this, is that its not set when you restart cheat engine. You have to "check/uncheck/check" in order to get this to work. I even thought about using onMemRecPreExecute, but cheat engine keeps alerting me and allows me to say "No", in which case, it never fires.
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Sun Jan 01, 2017 6:38 pm    Post subject: Reply with quote

onMemRecPostExecute might be better than OnActivate / OnDeactivate since it works globally and isn't specific to any single memory record.
Code:
function onMemRecPostExecute(memrec, newState, succeeded)
  if memrec.Type == vtAutoAssembler and succeeded then
    lastExecuted = memrec
  end
end

Save the script as a .lua file to the autorun folder in the main CE directory if you want it to run whenever CE is run.

_________________
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
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Sun Jan 01, 2017 7:24 pm    Post subject: Reply with quote

Nothing wrong with calling getAddressList().getMemoryRecordByID(0) inside the script.
There a reason you don't like that approach? And what are you using the MemoryRecord object for anyway?
Also, you could have a parent "Activate This First" script to initialize all this stuff.
That way the user is forced to execute the Lua.
Back to top
View user's profile Send private message
dl748
Advanced Cheater
Reputation: 0

Joined: 05 Mar 2016
Posts: 75

PostPosted: Sun Jan 01, 2017 7:50 pm    Post subject: Reply with quote

Zanzer wrote:
Nothing wrong with calling getAddressList().getMemoryRecordByID(0) inside the script.
There a reason you don't like that approach? And what are you using the MemoryRecord object for anyway?
Also, you could have a parent "Activate This First" script to initialize all this stuff.
That way the user is forced to execute the Lua.


Because its not accurate in the least. i.e. running that code in my entry returns nil. In another script, it returns the first script, not the script i'm working with.

ParkourPenguin, thats what I was thinking but it asks me if i want to run the script. And putting it in the location you said, prevents me from giving the script out to anyone. I was kind of hoping there was an easier way.
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Sun Jan 01, 2017 9:03 pm    Post subject: Reply with quote

The exact feature you're looking for isn't a part of CE. It's necessary to execute some Lua code to add this functionality.

Given that the prompt to execute the Lua script is a problem for you, I'm assuming all the Lua code you have is embedded inside AA scripts.

I'd recommend trusting that other people will execute the Lua script on their own. If they don't execute the script, they shouldn't expect the table to work completely: the Lua script is a part of the table.

Some alternatives:
  • Release it as a trainer. It doesn't give users the option of whether they want to execute the Lua script or not.
  • Put all the memory records under a single master script, hide those records when the master script is disabled, and have the master script execute the required Lua code. Users can still get around this if they know what they're doing, but it's less likely.
  • Copy and paste the code that creates the new functionality to the beginning of every single AA script that uses it. You may need to check if that code has already run, and if so, don't do it again.

_________________
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
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Sun Jan 01, 2017 9:08 pm    Post subject: Reply with quote

dl748 wrote:
Because its not accurate in the least. i.e. running that code in my entry returns nil. In another script, it returns the first script, not the script i'm working with.

Sounds like you're doing something wrong.
When you retrieve by ID, it will always return the same entry.
Once added, an entry's ID will never change within the table.
Perhaps you can show us how you're using two scripts and we can help you fix it.
Back to top
View user's profile Send private message
dl748
Advanced Cheater
Reputation: 0

Joined: 05 Mar 2016
Posts: 75

PostPosted: Sun Jan 01, 2017 11:10 pm    Post subject: Reply with quote

ParkourPenguin wrote:
The exact feature you're looking for isn't a part of CE. It's necessary to execute some Lua code to add this functionality.


Not exactly, some pascal code, yes, but not Lua. All it needs to do is in the TMemoryRecord.setActive() function is to add/update a symbol in the Lua symbol table BEFORE it runs autoassemble(). ENABLE and DISABLE are quite like event handlers. So in theory anyway, you should be able to access WHAT the event is firing on.

ParkourPenguin wrote:
Given that the prompt to execute the Lua script is a problem for you, I'm assuming all the Lua code you have is embedded inside AA scripts.

I'd recommend trusting that other people will execute the Lua script on their own. If they don't execute the script, they shouldn't expect the table to work completely: the Lua script is a part of the table.


No I understand, I just thought there would be an easier way. Thanks for the info, I've been racking my brain through the pascal code trying to find something.

Zanzer wrote:
Sounds like you're doing something wrong.
When you retrieve by ID, it will always return the same entry.
Once added, an entry's ID will never change within the table.
Perhaps you can show us how you're using two scripts and we can help you fix it.


No.
Yes, unless the entry changes or is removed.
Added is in fact changing the entry.
There is no need, there is nothing to "fix". getMemoryRecordByID() is no safer or more dangerous than getMemoryRecordByDescription().

Now it would be nice to either have a variable called myMemoryRecord, or a function called getMyMemoryRecord(), or even at least something like myMemoryRecordID, available at the time the script is fired for Enabled/Disabled (not for other events).
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Sun Jan 01, 2017 11:37 pm    Post subject: Reply with quote

Not sure what you're trying to accomplish.
You can easily get the MemoryRecord of the script with one line of code.
You can move it around, you can change the type, you can modify the script. The record ID never changes.
I would argue ID is indeed safer than Description, since you can easily duplicate the description.
Anyway, best of luck on your future endeavors.
Code:
myMemoryRecord = getAddressList().getMemoryRecordByID(#)
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 54

Joined: 01 Oct 2008
Posts: 941

PostPosted: Mon Jan 02, 2017 12:18 am    Post subject: Reply with quote

onMemRecPostExecute and onMemRecPreExecute should be the right way,
preExecute run before the MR (script) start, the MR object and its executing state can be record here (eg. into a Global Variable myMemoryRecord).
And postExecute may clear the Global Variable so that it will not confuse the script run by Lua autoAssemble.

Here is an example
http://forum.cheatengine.org/viewtopic.php?t=587056
From menu/Table/ the lua file entry/ save as file,
you can check the lua file.
It is also an example to load Lua script (when clicking an MR entry) from table file.

btw, I agree the MR object or its ID should be feed into AA script like syntaxcheck, the current pre/postExecute may not work for async executing MR in possible future version.

bye~

_________________
- Retarded.
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 457

Joined: 09 May 2003
Posts: 25262
Location: The netherlands

PostPosted: Mon Jan 02, 2017 5:26 am    Post subject: Reply with quote

the ID will only change when it gets copied/pasted to another addresslist and that exact ID is already in the list. (editing/adding new entries/deleting entries will never change it)


Anyhow, for next version i've added the 'memrec' object to the execution of the lua code, but be aware that memrec will be nil during the initial syntax check of "assign to cheat table", and when you just click execute in an auto assembler script, as those do not have a memory record assigned to them at that time (so check for nil and act accordingly)

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
panraven
Grandmaster Cheater
Reputation: 54

Joined: 01 Oct 2008
Posts: 941

PostPosted: Mon Jan 02, 2017 11:07 am    Post subject: Reply with quote

Hi, Dark Byte.

Can CE also add an global event handler like onMemRecPreExecute for Clicking 'Value', like the moment when if a dropdown list defined, the dropdown list will show.

With an event handler (may be double clicking? to differential current interaction), it is possible to spawn a custom ui for input of multiple named flags (eg. name of buffs) to set the value as bit flag integer values on memory, or dynamically show an dictionary data struct which are valid key choice at that moment.

The event handler may make possible to cancel showing of the default ui.

Thank you~

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

Joined: 05 Mar 2016
Posts: 75

PostPosted: Tue Jan 03, 2017 1:08 pm    Post subject: Reply with quote

Dark Byte wrote:
the ID will only change when it gets copied/pasted to another addresslist and that exact ID is already in the list. (editing/adding new entries/deleting entries will never change it)


Unfortunately, I do, do this a lot. I have a few scripts I use as a template. A script is usually the last thing I do, and its not easy to get the id of a table entry. For example a script i'm working on is Index 22 (Original index ~200), ID 7243. I have to write a script to list all the IDs, and the go through the hundreds of entries to find that id. i.e. of a template I use for dynamic entries.

Code:
{$lua}
[ENABLE]
local eng = GetLuaEngine()
eng.mOutput.Lines.Clear() -- Clear debug output on startup
eng.cbShowOnPrint.Checked = false

eng.Show()  -- Call show for startup and not on every print

local mr = getAddressList().getMemoryRecordByID(#)

[DISABLE]
local mr = getAddressList().getMemoryRecordByID(#)
for i=0,mr.Count-1 do
  mr.Child[i].delete()
end


It just would be nice(and cleaner) to not have it figure out what table entry is associated with the script.

Also I could get rid of some extra debug code if there was a table entry option of "Show Lua Engine on status change" rather than the "View -> Show on Print". Reason is because if you use a timer and have print's, Cheat Engine steals the focus for every print when you are in the game.
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Tue Jan 03, 2017 6:17 pm    Post subject: Reply with quote

Click on your memory record, press Ctrl+C to copy.
Open NotePad, press Ctrl+V to paste.
Code:
<ID>0</ID>
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