LUA scripting for BMX
BlitzMax Forums/BlitzMax Programming/LUA scripting for BMX
| ||
EDIT: Check out the module tweaks forum for this module and samples. Hi People, As you may know, I mentioned I'd be working on a LUA module for BMX (free for everyone to use). I have now successfully executed a LUA script through BMX and while it's still a little early, it's working great! Best of all it's cross-platform... Here is some code from the sample I'm putting together. Note that this sample uses the interface to C directly and therefore looks a little scary for Blitz people. I'll be wrapping this into a nice script engine for all of us to use for AI, Missions logic etc: Strict Import Pub.lua ' Call our entry point main() Function main() Local lua_state ' hold info about the current state of lua DebugStop ' Initialize lua lua_state = lua_open() ' Open libs luaopen_base(lua_state) luaopen_table(lua_state) luaopen_io(lua_state) luaopen_string(lua_state) luaopen_math(lua_state) Local script:String = "print (~qHello LUA World!~q)" ' Execute! Local error:Int = luaL_loadbuffer(lua_state, script.ToCString(), script.length, "testscript") Or .. lua_pcall(lua_state, 0, 0, 0) ' Check for error If (error) Then RuntimeError ("Error: " + lua_tostring(lua_state, -1)) lua_pop(lua_state, -1) End If ' Shut down lua lua_close(lua_state) End Function |
| ||
This will be a great asset for the Max community. External content is key for games (and of course apps) today. Best of luck with your module - it's getting there! |
| ||
Its a great idea, potentially very useful in RPG-type games. Can I suggest that you might want to simplify the syntax a little. For example, the lua_loadbuffer function should just be passed the script string, it can then do the conversion and get the length itself inside the function. |
| ||
This is just the module. Therefore it is low level. I will write a script engine module which will deal with the common usage of LUA in a more abstract way. So don't get hung up on the syntax unless you plan to write your own script engine :) |
| ||
Nearly there. The script engine can now run from a string, from a file, call a function in LUA (pass in parameters, get the result). All that's left to do is the callback stuff for when LUA call you and I'll release it to everyone. Here is a taste of the required code to run from a scriptfile:Function main() Local SEObject:ScriptEngine = New ScriptEngine ' Run the script If (Not SEObject.RunScriptFile("testscript.txt")) Then Print "Error: " + SEObject.GetLastErrorString() Else Print "Script finised." End If ' Shut it down SEObject.ShutDown() End Function |
| ||
Here's something that neat. A quick console example I've knocked up that allows you to type LUA commands in and have them executed when you pressed ENTER... :) :Strict Import Pub.scriptengine ' Call our entry point main() Function main() Local SEObject:ScriptEngine = New ScriptEngine Local command:String = "" Print ("Some commands to try (LUA commands are case sensitive):") Print ("a = 1 + 1") Print ("print (a)") Print ("b = a + 3") Print ("print (b)") Repeat Print ("Enter lua command! (quit to exit)") command = Input (":") If (Lower(command) = "quit") Then End ' Load script from string If (SEObject.SetScriptText(command, "ConsoleCommand")) Then ' Run the script If (Not SEObject.RunScript()) Then Print "Error: " + SEObject.GetLastErrorString() Else Print "Done." End If Else Print "Error: " + SEObject.GetLastErrorString() End If Forever ' Shut it down SEObject.ShutDown() End Function |
| ||
It's done just waiting for Mark to approve it getting added to the pub modules. |
| ||
For the uninitiated, clueless and generally inexperienced, could someone please describe for us what this `LUA` is? Thanks |
| ||
Haha tommy, that's hillarious -- I was going to do a lua module too before I knew about yours! :) Anyway cool job, hurry up and release it! :D http://www.lua.org/ |
| ||
For the uninitiated, clueless and generally inexperienced, could someone please describe for us what this `LUA` is? Thanks No probs. LUA is a tight scripting language. Take a look at http://www.lua.org . Basically the idea here is that you can add scripting to your game/game engine making it modable by the public or you can use it to script missions, script AI or even as I configuration file system. Tommy. |
| ||
A truly excellent addition, then. |
| ||
Sounds interesting. |
| ||
I can't wait to try this. failed misiribly to port it over to b3d ages back. Nice work. |
| ||
Should be available soon via syncmod. Tommy. |
| ||
rofl, I tried way back then too -- sorta got stuck when I realized I needed function pointers :) |
| ||
atm the lua module seems to be missing lua.a? I had to delete it in order to compile programs. |
| ||
The lua module comes as source, so you gotta Build Modules |
| ||
Yep you'll need to build your modules after sync. |
| ||
Is it for mac only Tommy? I don't see any new public mods. Even deleted my mod folder and re-synced everything to be sure. rofl, I tried way back then too -- sorta got stuck when I realized I needed function pointers :) Heh, do what any self respecting coder does. Blame the language, and walk away. :) |
| ||
Is it for mac only Tommy? I don't see any new public mods. Even deleted my mod folder and re-synced everything to be sure. Nope it is for all platforms. Don't think Mark has released it yet. Don't know how AngelDaniel go it actually... :) But yeah the idea behind writing modules is to make them cross-platform. Otherwise they are kind of useless in terms of BMX. |
| ||
I've dumped the modules and samples on my server for people to grab. Take a look in the module tweak forum. Tommy. |
| ||
I got lua.mod via sync mods this morning but not luascript.mod. This is on my mac, it doesn't sync to my PC for whatever reason. I'm yet to try it out yet as my Max decided to take a day off on the very day I had devoted to using it. On a complete side note, I'm with internode too. Great service (both technical and customer). I downloaded them off your server and will try them out as soon as I'm free. This module is such a great contribution and I'm sure it'll be appreciated by many. |
| ||
Yep internode has probably the best service I've ever experienced! :) Okay... I think Mark might be making some changes to the high level wrapper, but you can at least give the original one a go. |
| ||
Excellent work, Tommy - I was holding off buying BlitzMax because I needed scripting language support, but John Pickford and someone else pointed me here on another forum so Mark's got you to thank for another sale! The problem I had integrating Lua when I used it with C was getting it to be able to run several scripts at once in a kinda multitasking way - I could never get my head around how to do it. It's a big job, but do you plan to tackle it? If not I may be looking at degenerating into a dribbling wreck again trying to get it working... ;) Cheers, Ed |
| ||
Youll need some form of Threading support, although you can do it with callback methods. Problem is, they wont really be running paralel. |
| ||
I think it could be done without threads, by building some sort of "controller" that give each script time-slices, like how Windows did multitasking back in the day (and still may do, I don't remember)... |
| ||
Excellent work, Tommy - I was holding off buying BlitzMax because I needed scripting language support, but John Pickford and someone else pointed me here on another forum so Mark's got you to thank for another sale! Cheers! The scriptengine needs a little more work but you can do pretty much all the LUA scripting via BMX now. The problem I had integrating Lua when I used it with C was getting it to be able to run several scripts at once in a kinda multitasking way - I could never get my head around how to do it. It's a big job, but do you plan to tackle it? If not I may be looking at degenerating into a dribbling wreck again trying to get it working... ;) Okay what you could do is create a ScriptEngine object for each enemy or task or whatever. Because each instance has it's own lua_state, each scriptengine is isolated from the other, in theory you could tackle your problem. You could even attach the scriptengine instances to actual instances of enemies or AI agents and have each script interact with each unit privately etc. Hope that makes some sort of sense... |
| ||
Yeah, that works great for a lot of tasks, it's just if you need any of the scripts to modify engine-wide global variables it falls down, but then you've got extra issues of making sure there's no access errors... maybe it'd just be easier to constrain myself and future users! I think during my investigations there was some way of getting Lua to essentially "copy" a state into a new one so that you'd have access to the parent state's globals. There's also the issue of blocking functions, for which wrapping lua_yield and lua_resume might be handy - although I could never get my head around those two functions! I don't want to sound like I'm not grateful - I am, immensely - I'm just pointing out some further extensions if you so wished to do it. No doubt that if you don't others and myself will kludge our own solutions together ^^. Cheers, Ed |
| ||
Nah I'm open to suggestions... then again the source is open to change hehehe |
| ||
No probs. LUA is a tight scripting language. Take a look at www.lua.org . Basically the idea here is that you can add scripting to your game/game engine making it modable by the public or you can use it to script missions, script AI or even as I configuration file system. I have something hiding in the back alley for when the Max3D module is released! ^.^ |