Multiple AIs with one Lua state

Started by
23 comments, last by ddn3 14 years, 12 months ago
Quote:Original post by Guy Meh
Technically, yes. My scripts in general are basically implementations of C++ methods except that (a) I don't want to have to recompile and (b) I want to program in a higher level language.

Why don't you simply prefix your function or use a table as name space?
C++
class NPC1 { void nextMove() {  call_lua_function("NPC1.nextMove",this->state); } ...}

Lua
NPC1 = { nextMove = function(npc1_state) ... end, ...}-- for another type of NPCNPC2 = { nextMove = function(npc2_state) ... end, ...}


the Lua functions can access global variables and still have local variables in their own name space, the object specific variables are stored in the C++ class and passed to the Lua functions. Now you just need a function like "environment()" in Lua that returns information about the rest of world relevant for the NPCs. (Or send environment as second parameter)
You can also reuse some functions easily, for example
NPC3 = { -- behaves similar to NPC1 nextMove = NPC1.nextMove, ...}


This a non-OOP solution but very minimal and simple.
Advertisement
I found Kambiz's solution interesting. So, for each script, I'd enclose all of the functions in a table, and the table name is different for each function. This solves the problem with function names colliding, and is especially good for objects that don't need their own state. I was hoping to avoid putting any Lua AI state inside C++ since I wanted my AI's to have as few assumptions as possible so that I could experiment with any AI I wanted. Don't worry about how the AIs detect their environment; I pass in all required information when I need to.

ddn3, I noticed that your main program is another Lua script. How different should I expect it to look in C++? Also, is there any way to replicate some of the repeated code in the scripts like the header in C++ (like, say, when the script file is loaded) so that there's less work to do in the script files themselves?
Quote:Original post by Guy Meh
I found Kambiz's solution interesting. So, for each script, I'd enclose all of the functions in a table, and the table name is different for each function. This solves the problem with function names colliding, and is especially good for objects that don't need their own state. I was hoping to avoid putting any Lua AI state inside C++ since I wanted my AI's to have as few assumptions as possible so that I could experiment with any AI I wanted. Don't worry about how the AIs detect their environment; I pass in all required information when I need to.
Dude, did you even read the link I posted? That was exactly what I advised you to do.
Quote:Original post by Sneftel
Dude, did you even read the link I posted? That was exactly what I advised you to do.

*feels embarrassed*
If you don't need to store per instance data, then wrapping the functions in a table is the best way, as Sneftel suggested. If you do want per instance data I suggest you use something like I suggest, it's basically creating a class in Lua, but with exclusive file scope namespace ( so you don't have to worry about namespace collision between scripts ).

Now that I think about it, I can see some use for this methodology. I create levels in Lua and it's a pain in butt to name them uniquely (ie Level1, Level2, etc..). Using this technique i can just use a common api (ie Level:init(), etc..).

Good Luck!

-ddn

This topic is closed to new replies.

Advertisement