What it says. I'm using MESEN 0.9.8 ATM and I've recently gotten into making scripts for quick commands in games. But I can't get any working on this one!
Like, for example. I'd wish to be able to write a value unto an adress by pressing Up and B, say in Castlevania 1 writting 0x09 on adress 0x015B. But even when the script runs well, it doesn't do anything. I managed to pull this off on SNES9X-RR so this is really frustrating. Can anybody help me please?
LUA Scripts on Emulator.
-
Fiskbit
- Site Admin
- Posts: 1405
- Joined: Sat Nov 18, 2017 9:15 pm
Re: LUA Scripts on Emulator.
I'll start with the caveat that I've done only very little Lua in Mesen and am not a great person for helping with this, so I hope someone else with more experience chimes in.
Mesen 0.9.8 is many years out of date, and is actually not even the newest version before the massive rewrite for 2.0 (aka Mesen2). (0.9.9 is the newest pre-2.0 release version, and there is also a 0.9.9.62 build that has further improvements.) The Lua interface changed for 2.0 and you may want to target that instead so that people using the current, much-improved version of the emulator can use the scripts you write.
The documentation online is for pre-2.0 versions like yours, while documentation for 2.0 can be generated locally using the Help -> API Reference option in 2.0's script window.
At least for 2.0, simply opening the script window is enough to make the example script start running immediately. It draws stuff on the screen and is clearly functioning. There are also stop and start buttons so you can manually control whether the script is running. I don't think there was ever any more complexity than this for running scripts in pre-2.0 versions of Mesen. Do you even see any results when printing to either the screen or the script window log display using the functions here?
Mesen 0.9.8 is many years out of date, and is actually not even the newest version before the massive rewrite for 2.0 (aka Mesen2). (0.9.9 is the newest pre-2.0 release version, and there is also a 0.9.9.62 build that has further improvements.) The Lua interface changed for 2.0 and you may want to target that instead so that people using the current, much-improved version of the emulator can use the scripts you write.
The documentation online is for pre-2.0 versions like yours, while documentation for 2.0 can be generated locally using the Help -> API Reference option in 2.0's script window.
At least for 2.0, simply opening the script window is enough to make the example script start running immediately. It draws stuff on the screen and is clearly functioning. There are also stop and start buttons so you can manually control whether the script is running. I don't think there was ever any more complexity than this for running scripts in pre-2.0 versions of Mesen. Do you even see any results when printing to either the screen or the script window log display using the functions here?
-
Hero_Fan_71
- Posts: 5
- Joined: Thu Nov 06, 2025 10:34 am
Re: LUA Scripts on Emulator.
Well, yeah, sure. But I've worked and tried using BOTH versions and I get the exact same errors poping out and the same running but no doing anything scripts in both I even know pressing several buttons DOES do something since a different error pops up when I actually press the hotkeys. All of this happens in both. I guess you are right messages display better in 2.0 but that would be the least of my problems there anyway.
If someone wants to help me working with 2.0 then it's fine, I just need to make something work in the emulator XD.
If someone wants to help me working with 2.0 then it's fine, I just need to make something work in the emulator XD.
-
Fiskbit
- Site Admin
- Posts: 1405
- Joined: Sat Nov 18, 2017 9:15 pm
Re: LUA Scripts on Emulator.
If you provide an example script that doesn't work, we can try running it and debugging it. (Preferably for 2.0, since that's what most of us have around these days.)
-
Hero_Fan_71
- Posts: 5
- Joined: Thu Nov 06, 2025 10:34 am
Re: LUA Scripts on Emulator.
This is one of the early ones I made for that.
Code: Select all
local SUBWEAPON_ADDR = 0x015B
local CROSS_VALUE = 0x09
while true do
local input = emu.getInput(0)
if input.up and input.B then
memory.writebyte(SUBWEAPON_ADDR, CROSS_VALUE)
gui.text(10, 10, "Cross equipped!")
end
emu.frameadvance()
end
-
Fiskbit
- Site Admin
- Posts: 1405
- Joined: Sat Nov 18, 2017 9:15 pm
Re: LUA Scripts on Emulator.
Well, at least in Mesen 2.0, that doesn't work because there is no emu.frameadvance() (nor do I see it in the 0.9.9 documentation). Comparing against a Mesen 2.0 example script like Grid.lua, you can see that it instead sets a callback that runs each frame with "emu.addEventCallback(Main, emu.eventType.endFrame);", where Main the function name to call. That's how you get code to run once per frame.
-
Hero_Fan_71
- Posts: 5
- Joined: Thu Nov 06, 2025 10:34 am
Re: LUA Scripts on Emulator.
So, how should I change this? All I need is for at least one of this commands to work so I can copy paste it with different conditions and effects later.
-
Fiskbit
- Site Admin
- Posts: 1405
- Joined: Sat Nov 18, 2017 9:15 pm
Re: LUA Scripts on Emulator.
You've missed something extremely crucial here. You're attempting to run an FCEUX Lua script in Mesen, calling into FCEUX's Lua API, which does not exist in Mesen. FCEUX and Mesen do not have the same Lua APIs. Your script will fail if you try to call the FCEUX API from Mesen, just like it will if you try to call the Mesen API from FCEUX. They are not cross-compatible. You need to use Mesen's Lua references. I linked earlier to the one for 0.9.9, and I explained how to generate one for 2.0. The example Lua scripts in Mesen also show you the basics of how to do these things in Mesen.
I have converted your script to run in Mesen 2.0:
I have converted your script to run in Mesen 2.0:
Code: Select all
local SUBWEAPON_ADDR = 0x015B
local CROSS_VALUE = 0x09
function Foo()
local input = emu.getInput(0)
if input.up and input.b then
emu.write(SUBWEAPON_ADDR, CROSS_VALUE, emu.memType.nesInternalRam)
emu.displayMessage("Script", "Cross equipped!")
end
end
emu.addEventCallback(Foo, emu.eventType.endFrame);-
Hero_Fan_71
- Posts: 5
- Joined: Thu Nov 06, 2025 10:34 am
Re: LUA Scripts on Emulator.
You were right. Thanks a lot!
BTW I tested it on both versions (0.9.8) and (2.1.1) And it works the same in both.
BTW I tested it on both versions (0.9.8) and (2.1.1) And it works the same in both.
-
Fiskbit
- Site Admin
- Posts: 1405
- Joined: Sat Nov 18, 2017 9:15 pm
Re: LUA Scripts on Emulator.
Glad it's working. I don't know which parts of the Mesen API changed for 2.0; I just know that they are not 100% compatible, so you should expect problems if you're trying to make one script work in both.