Understanding Boss AI scripting (Lua with Luabind)

Started by
16 comments, last by NordCoder 12 years, 11 months ago

@ ddn3: Ok :) Thanks for the suggestions, I guess I'll just have to give it a try and code something then while trying to keep up good coding practices ;)

@Nanoha: One question though. I've read that when embedding a scripting system, you should let the engine (C++) side take care of the heavy stuff, i.e. drawing, collision detection, audio, physics and path finding while the script should do stuff like collision events, path finding selection, sound events and decision-making. Are you calling drawing functions from Lua (bound to the actual drawing functions in the engine) like the path finding functions? If so, does that still count as letting C++ take care of drawing or does that impose a performance penalty of some magnitude?

EDIT: @ddn3: Isn't it a bad idea to call the script with luaL_dofile each frame performance wise? Is there some other way?


You definitly don't want to call luaL_dofile each frame , a better solution is to intizialize one lua state per script file (or a single lua state for your application if you enforce unique function names across all script files) , load the scripts into that lua state (using for example luaL_dofile , or luaL_dostring) and then just use luabind::call_function as needed.

calling drawing functions from lua is definitly viable but i think its preferable to have the engine handle drawing and only use lua to change the gamestate. (allthough a scriptable renderer might have some advantages you still don't want to mix logic and render code).
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Advertisement
@Nanoha: Hmm. How would you go about keeping drawing on the C++ side then? If I am creating/updating my AI entities on the scripting side, I would need some kind of way to pass them to C++ to get drawn which sounds like a bad idea to me. Either that, or you could have a global/singleton EntityManager that both Lua and C++ has access to...In short, I would probably need some interface between the two.

@SimonForsman: I see, so I could just call the main updating function of the script. As for drawing, I pose the same question as above to you :)

@Nanoha: Hmm. How would you go about keeping drawing on the C++ side then? If I am creating/updating my AI entities on the scripting side, I would need some kind of way to pass them to C++ to get drawn which sounds like a bad idea to me. Either that, or you could have a global/singleton EntityManager that both Lua and C++ has access to...In short, I would probably need some interface between the two.


I never create entities in lua, I use factories and xml to load them so I guess I could just expose the factory to load an entity. The factory adds the entity to my entity manager. I'd go for an entity manager of some kind (which exists c++ side)..

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Yeah, like others have mentioned you don't want to do a dofile each update, since the idea isn't to reload/recompile the scripts each update rather maintain a single V M or as few VMs as you need (I have 2 one for the game and one for the editor), which manages your objects persistently between frame.

Something like a main.lua which holds your initial stateful objects like a game manager which then persistently manages objects for you script side.. Then each frame you call a global Lua function or maybe call the game manager Lua impl through a Luabind function... etc.. The specific are particular to your implementation. Since i personally like to avoid tight coupling i call into a global Lua function like onUpdate() and that cascades to all the script objects and sub-managers that need processing that frame..Something like..


----------------------------------------------------------------------------
--main update function for the scripts, called from the game
function onUpdate()
end

Thats the function right out my Lua framework.. I think the Keep It Simple methodology goes along way..

Good Luck!

-ddn

You definitly don't want to call luaL_dofile each frame , a better solution is to intizialize one lua state per script file (or a single lua state for your application if you enforce unique function names across all script files) , load the scripts into that lua state (using for example luaL_dofile , or luaL_dostring) and then just use luabind::call_function as needed.


An even better solution is to use tables and modules to define name spaces within your Lua code so that you can have multiple functions with the same name but in a different name space. This would let you combine scripts and functions much easier for reuse in a single context.

This goes back to what was said earlier about maintaining good software development practises :)
I apologize for my late response, I've been very busy lately...

@Nanoha: I like that approach, maybe I'll try implementing it myself :)

@ddn3: That makes sense. I guess you could have some state maintained within lua onUpdate function which updates according to which state the game is in.

@phantom: Yeah, I read that elsewhere a while back. I'll do my best :)

I have one last questions though, and I'll let you off the hook ;P If a variable (say a global "state" identifier, e.g. introState/gameState/pauseState etc.) is bound to your lua code and the state changes on the C++ (the user unpauses the game for instance) will the variable change from the perspective of lua automatically or will you need to "update" it somehow to tell lua that the contents of the variable has changed?


Thank you all again for your help! I really feel I am beginning to grasp the concepts. For starters, I'll try and write a script that animates the intro screen I have already set up :)
That depends on how the var is exposed.
If it is just a value living in lua then yes, it will have to be updated from the C++ side; if it is a value which is somehow bound directly to a C++ value then on query it will appear to change automagically as it will reflect whatever the C++ value is.

Personally if there is a value which will need to be queried a lot from Lua but is only going to change rarely in the C++ side you'd be better off pushing that value in to Lua directly rather than suffer the overhead of jumping into the C++ code on each query.
Ok. Thanks :)

When I started writing a small script for my game, I came up with something:
Say I have an object which has been declared in a Lua script (a class from C++ bound to Lua). After the script has been loaded from C++, does this value stay in existence? I.e. when I call the script's update function (like ddn3's code) will the state of the object be preserved on consecutive calls? If not, how can I achieve this? Closures? Coroutines?

This topic is closed to new replies.

Advertisement