[luabind] re: Giving different scripts to objects of the same class...
Brought to you by:
arvidn,
daniel_wallin
From: fries <lnm...@ho...> - 2003-12-17 11:12:11
|
> now my problem is for different monsters... > As my LUA script for the one monster example defines a lua function > named monter_function, function that I will call from C++, at each > tick of action, I now don't see how I could make two monsters have > different scripts... > And moreover, my purpose is to have n-monsters with n possibly > different scripts.... So changing names of LUA defined functions won't > work... I came across this same problem, and found a relatively good solution for it. *looks for his source code...* What i did was in the class that i wanted to have different scripts, i defined a member like this: class Monster { ... constructor and otehr stuff.... ... luabind::object methods; } Now, i expose the class: class_<Monster>("Monster") ... other stuff ... .def_readwrite ( "methods", &Monster::methods ) Now, what this does, is it lets me use 'methods' as a table to store Lua methods and variables. So in the script i do something like this (after making the global 'this' in the script point to my instance of the mosnter class in c++): function this.methods.init(this) print("Initilising Monster"); end function this.methods.update(this) %% insert monster specific update loop here end This i find is very flexable, and you can call these functions from c++ by doing: monster.methods["init"](monster); monster.methods["update"](monster); you have to pass the pointer to the monster because the init function takes it as a paramater. You can also have additional paramaters like: monster.methods["init"](monster, param1, param2, ...); but your init function has to take paramaters ;) I hope this helps. I have the source code to a simple 'robowars' type game that i made, where you script the AI for different robots using LUA, if you would like to take a look or if you would like me to clarify anything, email me at lnm...@ho.... The source code is pretty messy, but i think it would be usefull to look at ;) |