1 2
13 14
Darth_Marios
He/Him
Joined: 5/11/2015
Posts: 111
YoshiRulz wrote:
Darth_Marios wrote:
By the way, how to "convert" a code that start with 10xxxxxx? The cheat type says: 10aaaaaa dddd ;-16bit Increment [aaaaaa]=[aaaaaa]+dddd but i dont have any idea how a lua script would look about that...
Just based on what you've said, memory.write_u16_le(a, memory.read_u16_le(a) + d).
Thanks! (I'm still quite a noob with lua)
Editor, Expert player (2352)
Joined: 5/15/2007
Posts: 3946
Location: Germany
I'm trying to read cpu registers for Game Boy (A, B, C, D, E, H, L). The Bizhawk Lua Functions page says to use
emu.getregisters()
to get the register names, but it just returns an empty table. On Bizhawk 2.8.
YoshiRulz
Any
Editor, Emulator Coder
Joined: 8/30/2020
Posts: 127
Location: Sydney, Australia
MUGG wrote:
I'm trying to read cpu registers for Game Boy (A, B, C, D, E, H, L). The Bizhawk Lua Functions page says to use
emu.getregisters()
to get the register names, but it just returns an empty table. On Bizhawk 2.8.
CNR, all 3 cores implement registers. Do you have a rom loaded?
I contribute to BizHawk as Linux/cross-platform lead, testing and automation lead, and UI designer. This year, I'm experimenting with streaming BizHawk development on Twitch. nope Links to find me elsewhere and to some of my side projects are on my personal site. I will respond on Discord faster than to PMs on this site.
Hey look buddy, I'm an engineer. That means I solve problems. Not problems like "What is software," because that would fall within the purview of your conundrums of philosophy. I solve practical problems. For instance, how am I gonna stop some high-wattage thread-ripping monster of a CPU dead in its tracks? The answer: use code. And if that don't work? Use more code.
Editor, Expert player (2352)
Joined: 5/15/2007
Posts: 3946
Location: Germany
I would like to update my old luascript so it can run on newer Bizhawk.
while true do

--...

    if client.ispaused() then
		gui.DrawFinish()
		emu.yield()
	else
		emu.frameadvance()
	end
end
The emulator prints that gui.DrawFinish() is deprecated. This line was needed to update the screen while the emulator is paused. I couldn't find a replacement for this function. How should I change the code to achieve the desired effect (updating the screen while the emulator is paused)?
Emulator Coder, Judge, Experienced player (849)
Joined: 2/26/2020
Posts: 820
Location: California
Just don't include the line, drawing will still occur regardless.
Editor, Expert player (2352)
Joined: 5/15/2007
Posts: 3946
Location: Germany
The script in question is my Kirby & The Amazing Mirror luascript and I'm testing in Bizhawk 2.9.1 on the (U) or (J rev 1.1) version. First, I replaced all instances of table.getn(mytable) with #mytable to make it run in the first place. While the emulator is paused, I'm expecting to be able to navigate the little menu I have drawn on screen via my script by mouse-click. After removing the line gui.DrawFinish, this is not possible anymore, even on Bizhawk 2.9.1. The line works fine under Bizhawk 2.9.1, but the emulator would keep printing in the console that it's deprecated, slowing performance. I would be grateful for a replacement function or to be able to disable the console messages. I might look into other ways, such as lua canvas. Thank you.
Emulator Coder, Judge, Experienced player (849)
Joined: 2/26/2020
Posts: 820
Location: California
gui.drawFinish just does nothing in modern (since 2.5 iirc?) BizHawk versions (besides putting the deprecated message, which you can get rid of by just not calling that function). Drawing a surface "starts" whenever a draw commands happens in a new frame and "ends" once that frame ends and there was a start to begin with, more or less (the exact logic is complicated and also wouldn't work with multiple scripts). The latest dev builds anyways have simplified the behavior and might end up solving your issue?
Editor, Expert player (2352)
Joined: 5/15/2007
Posts: 3946
Location: Germany
Hmm, this is odd. I tested again with the removed line and now it works, mysteriously... Ok, the problem is solved. Thank you and sorry for the confusion.
Editor, Expert player (2352)
Joined: 5/15/2007
Posts: 3946
Location: Germany
I have run into a different issue. When event.onloadstate() is called, it seems client extra padding set via client.SetGameExtraPadding() resets to 0. I have found nothing in my script that could be causing this. As a work-around I refresh the extra padding, but it is annoying to see the window resize every time I load a state. The script in question: https://tasvideos.org/Forum/Topics/2095?CurrentPage=9&Highlight=530445#530445
Emulator Coder, Judge, Experienced player (849)
Joined: 2/26/2020
Posts: 820
Location: California
As far as I can tell there's nothing in BizHawk that could even be causing such behavior. Is the same behavior present in dev builds?
Editor, Expert player (2352)
Joined: 5/15/2007
Posts: 3946
Location: Germany
CasualPokePlayer wrote:
As far as I can tell there's nothing in BizHawk that could even be causing such behavior. Is the same behavior present in dev builds?
I'm not sure what to say. Because I reopened the emulator and the issue "fixed itself", once again. Maybe something is wrong about reloading luascripts. When making changes to a luascript, it seems the emulator has to be reopened and the script run from a fresh start, in order to fix obscure issues.
Site Admin, Skilled player (1265)
Joined: 4/17/2010
Posts: 11597
Location: Lake Char­gogg­a­gogg­man­chaugg­a­gogg­chau­bun­a­gung­a­maugg
Clearing registered functions would have the same effect as closing the emu (right-click menu on lua console).
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.
Editor, Expert player (2352)
Joined: 5/15/2007
Posts: 3946
Location: Germany
How can I let a movie play back in TAStudio while applying input on the go via lua?
Language: Lua

-- this code isn't working myjoypad = joypad.getwithmovie() if myjoypad.Left then local random_num = math.random() local apply_change = false if random_num < 0.1 then myjoypad.Right = true apply_change = true elseif random_num > 0.9 then myjoypad.Up = true apply_change = true end if apply_change then tastudio.setrecording(true) joypad.set(myjoypad) tastudio.applyinputchanges() tastudio.setrecording(false) end end
EDIT: Nvm, here is working code I figured out.
Language: Lua

myjoypad = joypad.getwithmovie() if myjoypad.Left then local random_num = math.random() if random_num < 0.1 then tastudio.submitinputchange(emu.framecount(), "Right", true) tastudio.applyinputchanges() elseif random_num > 0.9 then tastudio.submitinputchange(emu.framecount(), "Down", true) tastudio.applyinputchanges() end end
Emulator Coder, Judge, Experienced player (849)
Joined: 2/26/2020
Posts: 820
Location: California
joypad.set only works while recording mode is enabled (as it mimics native input, not painting inputs and such).
Editor, Expert player (2352)
Joined: 5/15/2007
Posts: 3946
Location: Germany
How can I make my script stop? I have not found a function for this. Only functions that close the rom or close the emulator. When I close the ROM, my luascript spits out errors in the console and I want to avoid that. Another question. I noticed the lua console said "Message Cap reached, suppressing output" because of a long string that was outputted. I don't think this happened on older Bizhawk. Is there a way to disable this?
Editor, Expert player (2123)
Joined: 6/15/2005
Posts: 3292
MUGG wrote:
I noticed the lua console said "Message Cap reached, suppressing output" because of a long string that was outputted.
From my experience, that can definitely happen if you do too many console.write operations without emu.frameadvance(). The Lua console is not a good way of holding output long-term. Try output to file, something like:
local f = io.open("results.txt","a")
    f:write( ... )
f:close()
(replace "..." with whatever string you want to output) Edit: The error happens if you use console.write 100 or more times in a row. You can make your output string first (use ".." (two dots) to concatenate the strings all together), then at the end use console.write on that string. Of course, if you are storing output that you want to view long-term, better to output to file as shown above.
YoshiRulz
Any
Editor, Emulator Coder
Joined: 8/30/2020
Posts: 127
Location: Sydney, Australia
MUGG wrote:
How can I make my script stop?
return; from the top level (or break; out of your infinite loop and let it run to EOF).
I contribute to BizHawk as Linux/cross-platform lead, testing and automation lead, and UI designer. This year, I'm experimenting with streaming BizHawk development on Twitch. nope Links to find me elsewhere and to some of my side projects are on my personal site. I will respond on Discord faster than to PMs on this site.
Hey look buddy, I'm an engineer. That means I solve problems. Not problems like "What is software," because that would fall within the purview of your conundrums of philosophy. I solve practical problems. For instance, how am I gonna stop some high-wattage thread-ripping monster of a CPU dead in its tracks? The answer: use code. And if that don't work? Use more code.
Darth_Marios
He/Him
Joined: 5/11/2015
Posts: 111
Since i'm tasing Tekken games for fun, im looking for a script to automate some "moves", a combination, but i dont know how would it looks like. I should use emu.framecount with increments to insert a button sequence each frame? Example, assuming a wanna make script for wave dash (f,N,d,d/f), its a 4 frame sequence: 1f = 1 frame [1f] f [1f] N (neutral, mean no buttons or directions are pressed) [1f] d [1f] d/f i need emu.framecount and joypad.set? Maybe combining framecount with a 'for i=1, 4 do'?
YoshiRulz
Any
Editor, Emulator Coder
Joined: 8/30/2020
Posts: 127
Location: Sydney, Australia
For something simple like that you could use the macros feature, but yes you can do Download macro.lua
Language: lua

-- ... if trigger then joypad.set({ ["B"] = true }); emu.frameadvance(); emu.frameadvance(); joypad.set({ ["Down"] = true }); emu.frameadvance(); joypad.set({ ["Down"] = true, ["B"] = true }); emu.frameadvance(); end -- ...
And if you're using TAStudio, you'd call tastudio.submitinputchange instead, then tastudio.applyinputchanges(); before each emu.frameadvance();.
I contribute to BizHawk as Linux/cross-platform lead, testing and automation lead, and UI designer. This year, I'm experimenting with streaming BizHawk development on Twitch. nope Links to find me elsewhere and to some of my side projects are on my personal site. I will respond on Discord faster than to PMs on this site.
Hey look buddy, I'm an engineer. That means I solve problems. Not problems like "What is software," because that would fall within the purview of your conundrums of philosophy. I solve practical problems. For instance, how am I gonna stop some high-wattage thread-ripping monster of a CPU dead in its tracks? The answer: use code. And if that don't work? Use more code.
Darth_Marios
He/Him
Joined: 5/11/2015
Posts: 111
Thank you! So i just needed to repeat the frameadvance function :/ (i thought it was too easy). About the macro feratures, it doesnt seems to works with mame games (at least, not with tekken games. As well hex editor and ram watch/write)
Editor, Skilled player (1500)
Joined: 7/9/2010
Posts: 1321
YoshiRulz wrote:
And if you're using TAStudio, you'd call tastudio.submitinputchange instead, then tastudio.applyinputchanges(); before each emu.frameadvance();.
In TAStudio you don't need to do the stuff with emu.frameadvance(), instead you pass in the framenumber into tastudio.submitinputchange(), best way to do that is to just use tastudio.getselection() for the framenumber where to put it in and use a keyboard key for the macro to execute.
Favorite animal: STOCK Gt(ROSA)26Sortm1.1(rtTA,EGFP)Nagy Grm7Tg(SMN2)89Ahmb Smn1tm1Msd Tg(SMN2*delta7)4299Ahmb Tg(tetO-SMN2,-luc)#aAhmb/J YouTube Twitch
Post subject: Inconsistencies with savestate.save and pcall between Lua console and script
Sand
He/Him
Player (144)
Joined: 6/26/2018
Posts: 184
I'm trying to make a wrapper around savestate.save with a return value that indicates whether the save happened or not. The obvious way of doing it, calling savestate.save inside pcall, works the way I expect when I type it into the input box of the Lua Console, but it doesn't work the same way inside a script. Inside a script, pcall returns unexpected values, and the script then crashes with a LuaScriptException anyway. This is with BizHawk 2.10 on Linux. Saving a savestate to a file could fail for various reasons, such as insufficient permissions. savestate.save calls SaveStateLuaLibrary.Save, which doesn't have a return value; it relies on throwing an exception to signal error. SaveStateLuaLibrary.Save in turn defers to APIs.SaveState.Save and _mainForm.SaveState. In the Lua Console, if I try to save a savestate to a path to which I do not have permission, I get a LuaScriptException stack trace (which is expected):
savestate.save("/root/foobar")
NLua.Exceptions.LuaScriptException: [string "input"]:1: A .NET exception occured in user-code
System.UnauthorizedAccessException: Access to the path "/root/foobar" is denied.
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x001ef] in <12b418a7818c4ca0893feeaaf67f1e7f>:0
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean isAsync, System.Boolean anonymous) [0x00000] in <12b418a7818c4ca0893feeaaf67f1e7f>:0
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access) [0x00000] in <12b418a7818c4ca0893feeaaf67f1e7f>:0
  at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess)
  at BizHawk.Client.Common.FrameworkZipWriter..ctor (System.String path, System.Int32 compressionLevel) [0x00006] in <2abb4b338b4a4b62a6b02f91d843b06c>:0
  at BizHawk.Client.Common.ZipStateSaver..ctor (System.String path, System.Int32 compressionLevel) [0x00006] in <2abb4b338b4a4b62a6b02f91d843b06c>:0
  at BizHawk.Client.Common.SavestateFile.Create (System.String filename, BizHawk.Client.Common.SaveStateConfig config) [0x00007] in <2abb4b338b4a4b62a6b02f91d843b06c>:0
  at BizHawk.Client.EmuHawk.MainForm.SaveState (System.String path, System.String userFriendlyStateName, System.Boolean fromLua, System.Boolean suppressOSD) [0x00091] in <f3ae97cbaae74109853bdae22c2312c8>:0
  at BizHawk.Client.Common.SaveStateApi.Save (System.String path, System.Boolean suppressOSD) [0x00000] in <2abb4b338b4a4b62a6b02f91d843b06c>:0
  at BizHawk.Client.Common.SaveStateLuaLibrary.Save (System.String path, System.Boolean suppressOSD) [0x00023] in <2abb4b338b4a4b62a6b02f91d843b06c>:0
  at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0007c] in <12b418a7818c4ca0893feeaaf67f1e7f>:0
In the Lua Console, wrapping the call in pcall works as I expect. Saving the savestate fails, and pcall returns false and a string representation of the error:
pcall(savestate.save, "/root/foobar")
False	NLua.Exceptions.LuaScriptException: A .NET exception occured in user-code
The above technique with pcall would be adequate for my needs. The problem is, it doesn't work the same way inside a script. I create a file test.lua:
print("before")
print(pcall(savestate.save, "/root/foobar"))
print("after")
When I run the script with ./EmuHawkMono.sh --lua=test.lua ROMFILE, I get one line of output for the "before", one line of output for the pcall, and then a LuaScriptException stack trace:
before
False	/root/foobar
NLua.Exceptions.LuaScriptException: A .NET exception occured in user-code
System.UnauthorizedAccessException: Access to the path "/root/foobar" is denied.
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x001ef] in <12b418a7818c4ca0893feeaaf67f1e7f>:0
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean isAsync, System.Boolean anonymous) [0x00000] in <12b418a7818c4ca0893feeaaf67f1e7f>:0
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access) [0x00000] in <12b418a7818c4ca0893feeaaf67f1e7f>:0
  at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess)
  at BizHawk.Client.Common.FrameworkZipWriter..ctor (System.String path, System.Int32 compressionLevel) [0x00006] in <2abb4b338b4a4b62a6b02f91d843b06c>:0
  at BizHawk.Client.Common.ZipStateSaver..ctor (System.String path, System.Int32 compressionLevel) [0x00006] in <2abb4b338b4a4b62a6b02f91d843b06c>:0
  at BizHawk.Client.Common.SavestateFile.Create (System.String filename, BizHawk.Client.Common.SaveStateConfig config) [0x00007] in <2abb4b338b4a4b62a6b02f91d843b06c>:0
  at BizHawk.Client.EmuHawk.MainForm.SaveState (System.String path, System.String userFriendlyStateName, System.Boolean fromLua, System.Boolean suppressOSD) [0x00091] in <f3ae97cbaae74109853bdae22c2312c8>:0
  at BizHawk.Client.Common.SaveStateApi.Save (System.String path, System.Boolean suppressOSD) [0x00000] in <2abb4b338b4a4b62a6b02f91d843b06c>:0
  at BizHawk.Client.Common.SaveStateLuaLibrary.Save (System.String path, System.Boolean suppressOSD) [0x00023] in <2abb4b338b4a4b62a6b02f91d843b06c>:0
  at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0007c] in <12b418a7818c4ca0893feeaaf67f1e7f>:0
Important points to notice:
  1. The pcall function actually runs and returns. The first return value is false, as expected.
  2. However, the second return of pcall is not the expected LuaScriptException error message, but rather the argument to savestate.save, "/root/foobar".
  3. Even though pcall catches the error, the script crashes with a LuaScriptException stack trace anyway, before the print("after") line.
But get this: if I don't immediately print the return values of pcall, but instead store them in variables, then the stack trace does not occur until after the print("after") line:
print("before")
x, y = pcall(savestate.save, "/root/foobar")
print("after")
before
after
NLua.Exceptions.LuaScriptException: A .NET exception occured in user-code
System.UnauthorizedAccessException: Access to the path "/root/foobar" is denied.
...etc...
The x and y variables that were assigned to in the script remain set, and their values can still be seen in the Lua Console:
print(x, y)
False	/root/foobar
I'm not sure if this is a bug in BizHawk, or if I am doing something wrong. The fact that pcall returns one of its arguments, rather than the expected error messages, and that the script doesn't crash until the next call to print, makes me suspect something is getting mixed up with the Lua virtual stack. Below is the function I would like to be able to write. In fact, I can define the function in a script, and call it from the Lua Console, and it works as expected. But if I try to call it from the same script, it exhibits the inconsistencies described above.
function save_savestate_to_file(path)
	local ok, err = pcall(savestate.save, path)
	if ok then
		return true
	else    
		return nil, err
	end
end
YoshiRulz
Any
Editor, Emulator Coder
Joined: 8/30/2020
Posts: 127
Location: Sydney, Australia
Sand wrote:
[a bunch of stuff about needing Lua error handling because savestate.save throws on error]
Grab a dev build.
I contribute to BizHawk as Linux/cross-platform lead, testing and automation lead, and UI designer. This year, I'm experimenting with streaming BizHawk development on Twitch. nope Links to find me elsewhere and to some of my side projects are on my personal site. I will respond on Discord faster than to PMs on this site.
Hey look buddy, I'm an engineer. That means I solve problems. Not problems like "What is software," because that would fall within the purview of your conundrums of philosophy. I solve practical problems. For instance, how am I gonna stop some high-wattage thread-ripping monster of a CPU dead in its tracks? The answer: use code. And if that don't work? Use more code.
Post subject: Re: Inconsistencies with savestate.save and pcall between Lua console and script
Sand
He/Him
Player (144)
Joined: 6/26/2018
Posts: 184
YoshiRulz wrote:
Grab a dev build.
I mean, sure—f409cc4 fixes the immediate problem of checking for error from savestate.save. But I think you have overlooked the more important part of my post. Namely:
Sand wrote:
  1. The pcall function actually runs and returns. The first return value is false, as expected.
  2. However, the second return of pcall is not the expected LuaScriptException error message, but rather the argument to savestate.save, "/root/foobar".
  3. Even though pcall catches the error, the script crashes with a LuaScriptException stack trace anyway, before the print("after") line.
The fact that pcall returns one of its arguments, rather than the expected error messages, and that the script doesn't crash until the next call to print, makes me suspect something is getting mixed up with the Lua virtual stack.
The problem is more general than savestate.save. For example, try any of these in the Lua Console input box and in a script file:
  • print("before") print(pcall(client.screenshot, "/root/foobar")) print("after")
  • print("before") print(pcall(memory.hash_region, memory.getcurrentmemorydomainsize(), 1)) print("after")
In the Lua Console, they work as expected: pcall catches the error and the program finishes. In a script, the second return value of pcall is wrong, and the program crashes after pcall returns. Surely that's not intended? Having to use pcall sometimes when calling Wiki: BizHawk/LuaFunctions doesn't bother me. That's normal Lua programming. The problem is the inconsistency between how it works in the Lua Console input box and in a script file. I spent some time trying to understand what's different when Lua code is run from the console versus in a script file. When code is run from the input box in the console, it calls LuaSandbox.Sandbox with a null thread and a null exceptionCallback. When a Lua file is enabled, thread is null and exceptionCallback is non-null. When a Lua file is restarted or resumed, both thread and exceptionCallback are non-null. But I could not see how these differences would lead to the described inconsistency. LuaMethodWrapper.Call looks to be where the Lua virtual stack manipulations happen. On seeing a bug like this one, where function parameters are wrongly interpreted as return values, and values from a previous function call affect the next function call, I would tend to first suspect code like this that does low-level stack manipulations. But again it was not clear to my why anything in this function should be different when run from the Lua Console input box and from a script. Note that there is no problem when the error occurs before the function is actually called, such as when an attempt is made to call a function with the wrong number of parameters. print("before") print(pcall(joypad.get, "wrong type")) print("after") works as expected both in the Lua Console input box and in a script file.
YoshiRulz
Any
Editor, Emulator Coder
Joined: 8/30/2020
Posts: 127
Location: Sydney, Australia
CPP pushed a fix for pcall, so please try again with a new dev build.
I contribute to BizHawk as Linux/cross-platform lead, testing and automation lead, and UI designer. This year, I'm experimenting with streaming BizHawk development on Twitch. nope Links to find me elsewhere and to some of my side projects are on my personal site. I will respond on Discord faster than to PMs on this site.
Hey look buddy, I'm an engineer. That means I solve problems. Not problems like "What is software," because that would fall within the purview of your conundrums of philosophy. I solve practical problems. For instance, how am I gonna stop some high-wattage thread-ripping monster of a CPU dead in its tracks? The answer: use code. And if that don't work? Use more code.
1 2
13 14

1741033273