Post subject: Print when value is written (lua)?
Joined: 3/18/2006
Posts: 971
Location: Great Britain
I want to print the value at 0x80000048 everytime it is written to. Anyone know how this can be done? I only know that I'll have to use this function, but I'm unsure how to have it print only when the value is written. (so it doesn't print anything when the value isn't changing)
print(memory.readdword(0x80000048))
Thanks if anyone can help with this. edit: or an alternative way would be to print the value every 3 seconds! adelikat: Moved this topic from OT to Labratory
adelikat
He/Him
Emulator Coder, Site Developer, Site Owner, Expert player (3576)
Joined: 11/3/2004
Posts: 4754
Location: Tennessee
memory.registerread() is what you are looking for (assuming the emulator you are using has it implemented). What this does is register a lua function that you write to the read event of a memory address. so for example you would do a function like this: function PrintValue() print(memory.readdword(0x80000048)) end memory.registerread(PrintValue)
It's hard to look this good. My TAS projects
Joined: 7/2/2007
Posts: 3960
Wouldn't memory.registerread need to know the address of the memory that you want to register to? That is, aren't you missing an argument to it? I admit I haven't worked in lua in ages, but this seems somewhat underdefined otherwise.
Pyrel - an open-source rewrite of the Angband roguelike game in Python.
upthorn
He/Him
Emulator Coder, Active player (392)
Joined: 3/24/2006
Posts: 1802
actually, you'll want registerwrite, and yeah, you'll want to say memory.registerwrite(address,(size,)function) where size is an optional argument that defines a range to watch, instead of a single address. For your case, it looks like you'll want memory.registerwrite(0x80000048,4,print(memory.readdword(0x80000048)))
How fleeting are all human passions compared with the massive continuity of ducks.
Brandon
He/Him
Editor, Player (191)
Joined: 11/21/2010
Posts: 914
Location: Tennessee
If your emulator doesn't have a registerwrite function, you can always just do this manually by reading the value at that address every frame and comparing it with the previous value, only printing the value when they differ. memory.registerwrite is almost definitely more efficient, if available, though.
All the best, Brandon Evans
Former player
Joined: 5/4/2005
Posts: 502
Location: Onett, Eagleland
That wouldn't help if he needs to know when the value is written to though. For instance, what if a game writes the value once when a button is pressed but never wipes it from memory, and the next frame the same button is pressed and the same number is written. I don't think there is anyway to do this in lua, you'd probably need to set breakpoints if the emu has a debugger.
I think.....therefore I am not Barry Burton
Patashu
He/Him
Joined: 10/2/2005
Posts: 4045
Pasky13 wrote:
That wouldn't help if he needs to know when the value is written to though. For instance, what if a game writes the value once when a button is pressed but never wipes it from memory, and the next frame the same button is pressed and the same number is written. I don't think there is anyway to do this in lua, you'd probably need to set breakpoints if the emu has a debugger.
If you only want to know when the value changes at a frame boundary, you can remember what the value from the previous frame was, compare, print if it differs and set it to the new value.
My Chiptune music, made in Famitracker: http://soundcloud.com/patashu My twitch. I stream mostly shmups & rhythm games http://twitch.tv/patashu My youtube, again shmups and rhythm games and misc stuff: http://youtube.com/user/patashu
Post subject: how to print every 60 frames (lua)?
Joined: 3/18/2006
Posts: 971
Location: Great Britain
ok, so I think I've made this more complicated than necessary. All I actually need is something that does print(memory.readdword(0x80000048)) every second (60 frames). Anyone can help with this?
Joined: 3/4/2012
Posts: 74
like this? local timer = 0 while true emu.frameadvance() timer = timer + 1 if timer == 60 then print(memory.readdword(0x80000048)) timer = 0 end end
Editor, Player (54)
Joined: 12/25/2004
Posts: 634
Location: Aguascalientes, Mexico
I like using modulo best
local timer = 0
while true
  timer = (timer + 1) % 60
  emu.frameadvance()

  if timer == 0 then
    print(memory.readdword(0x80000048))
  end

end
I'm not sure, but normally don't you place the frame advance at the end of the 'while' statement?
I'm the best in the Universe! Remember that!
Site Admin, Skilled player (1255)
Joined: 4/17/2010
Posts: 11495
Location: Lake Char­gogg­a­gogg­man­chaugg­a­gogg­chau­bun­a­gung­a­maugg
I like that one better:
Language: lua

last_value = 0 function check() value = memory.readbyte(0x80000048) if value ~= last_value then print(value.." for frame "..movie.framecount()) end last_value = value end emu.registerafter(check);
Warning: When making decisions, I try to collect as much data as possible before actually deciding. I try to abstract away and see the principles behind real world events and people's opinions. I try to generalize them and turn into something clear and reusable. I hate depending on unpredictable and having to make lottery guesses. Any problem can be solved by systems thinking and acting.
Post subject: using a clock in lua to determine seconds?
Joined: 3/18/2006
Posts: 971
Location: Great Britain
samurai goroh wrote:
I like using modulo best
local timer = 0
while true do
  timer = (timer + 1) % 60
  emu.frameadvance()

  if timer == 0 then
    print(memory.readdword(0x80000048))
  end

end
This is what I need except can it be modified so that it bases 'seconds' on something else? Based on any type of clock, including the OS if necessary. (emu.frameadvance() doesn't work properly in this emulator and just floods the lua dialog with values as fast as it can, and crashes.)
Editor, Player (54)
Joined: 12/25/2004
Posts: 634
Location: Aguascalientes, Mexico
So, which emulator are we talking about? If emu.frameadvance() isn't recognized (like that) then it won't run the WHILE statement once per frame. So, if you remove it, it will run indefinitely when you open the script & that's likely the reason it floods the dialog & crashes...
I'm the best in the Universe! Remember that!
Former player
Joined: 5/4/2005
Posts: 502
Location: Onett, Eagleland
Patashu wrote:
Pasky13 wrote:
That wouldn't help if he needs to know when the value is written to though. For instance, what if a game writes the value once when a button is pressed but never wipes it from memory, and the next frame the same button is pressed and the same number is written. I don't think there is anyway to do this in lua, you'd probably need to set breakpoints if the emu has a debugger.
If you only want to know when the value changes at a frame boundary, you can remember what the value from the previous frame was, compare, print if it differs and set it to the new value.
That was my point, what if an enemy spitting a fireball wrote a value of '5' to memory, but the game never resets it to '0' when it doesn't throw a fireball but it did the same thing on the following frame. Since you're looking for a change, there would be no way to tell the enemy did it twice in a row. It'd be nice to have some sort of feature that can detect when an address is written to similar to a 'break on write'. Sorry if off topic a bit.
I think.....therefore I am not Barry Burton
Joined: 3/18/2006
Posts: 971
Location: Great Britain
Dolphin emulator. Hmm, that's too bad then.
samurai goroh wrote:
So, which emulator are we talking about? If emu.frameadvance() isn't recognized (like that) then it won't run the WHILE statement once per frame. So, if you remove it, it will run indefinitely when you open the script & that's likely the reason it floods the dialog & crashes...
Emulator Coder, Site Developer, Former player
Joined: 11/6/2004
Posts: 833
Alternative form:
while true do
  for _unusedvar=1,60 do
    emu.frameadvance()
  end

  print(memory.readword(0x80000048))
end
So many people forget that you can call emu.frameadvance however the heck you want. Doesn't have to be done once per infinite loop.
Post subject: lua help: memory.registerwrite ?
Joined: 3/18/2006
Posts: 971
Location: Great Britain
i managed to fix emu.frameadvance() function. so it now works! However, I'm getting false positives. Could I use memory.registerwrite to make sure I only get the value printed (or even better: written to txt file) when it changes? I don't want to get the same value twice or more in a row. I used this:
-- File to write to
local outfile = assert(io.open("value_mem.txt", "w"));


memory.registerwrite(0x00c7500f,outfile:(memory.readbyte(0x00c7500f)))



end

assert(outfile:close());
But I get this error:
\register.lua:5: '<name>' expected near '('
Emulator Coder, Site Developer, Former player
Joined: 11/6/2004
Posts: 833
The colon: is for calling a function, not making a function. You probably want something more like this:
Language: lua

-- File to write to local outfile = assert(io.open("value_mem.txt", "w")); local function onMemoryWrite() outfile:write(memory.readbyte(0x00c7500f) .. "\n") end memory.registerwrite(0x00c7500f,onMemoryWrite)
Post subject: help with this simple lua script workaround?
Joined: 3/18/2006
Posts: 971
Location: Great Britain
^With this, I get: "script returned but is still running registered functions". And the txt file is empty.