Need guidance on concurrent scripts

Started by
1 comment, last by ddn3 15 years, 4 months ago
Hey all, My aim is to have a script executing for each NPC in my game, often several at once (C++, Lua). Using one state corrupts the stack and I end up with a bunch of runtime errors because I've popped a bad value. Giving each NPC it's own lua-state seems like overkill to me. Especially since I would (correct me if I'm wrong) have to register my C++ utility functions for every state. I'm happy to do it if there's no better way. Co-routines make scripts take turns running (?), is this ideal when various scripts will be running constantly. The question really is: conceptually, how should I approach this scenario? Importance is placed on performance/reliability. Help is greatly appreciated, M [Edited by - mwmey1 on December 3, 2008 12:43:06 AM]
Advertisement
/seconded im in EXACTLY the same quagmire atm! Im amazed how few tutorials/dicussions there are on the net regarding this. It must be such an almost fundamental issue that crops up with regard to game scripting, yet i can find practically nothing!

Im currently faced with inplementing a lua_State for EACH entitity instance. However... im actually getting (very unhelpful!) runtime errors even trying to doing that!
Scripting isnt any different than writting code in C or C++. Do what you would normally do in those lanagues.

Rarely do you need true concurrent exectuion (ie true threads) for game logic. Normally it's done in several forms, depending upon the requirements of the game.

-Simply iterate over each entity, which contains both its logic and context within a single unit (in C++ this would be a instance of a class, in Lua it would be a table with associated metamethods etc.. ). Your entites will be stored in a container of some sort, be it table, list, array, map, etc..

-More procedrual update, where entites register with a scheduler if they have something to do. Scheduler sorts the updates in time and iterates over them, with hard timeouts if the updates exceed a given allocation. This allows you to support more entites than the brute force method but its also more complex.

If you really need true concurrent exection you will have to use external threads, as Lua doens't natively support true threads. You can run a VM in each thread, sync up any global data manually through protected accessors and use some sort of message driven programming to get around problems of locking. This is almost never worth it for game logic.

Good Luck!

-ddn

This topic is closed to new replies.

Advertisement