Multithreaded Scripting (LUA?)

Started by
6 comments, last by Necrolis 11 years, 4 months ago
Hi,

I'm planning on adding a scripting interface to my game to my RTS game, however most stuff I've read on scripting interfaces seems to only allow a single thread of script execution where as I would like to make use of all the available cores.

For example, lets imagine I have 50 units in play at a particular instant in time. For each unit I may periodically fire an Update event that causes a piece of script to be executed. The script can then do whatever it likes, such as initiate a move order, or fire a weapon etc.

In a single threaded scripting engine I'd have to call the Update() script code for each of the 50 units one after the other, but if I have a machine with 8 cores then I might want to allow 6 threads of scripting (Leaving 2 for the main game and rendering etc), so multiple Updates scripts can run in parallel.

I'd obviously need to manage the usual multithreading data sharing type things, so for example any 2 functions running in parallel would not be able to access the same data (Unless its constant or has some kind of mutex type access control, but I'd rather not expose that to the script).

I could allow per Unit read/writable data by guaranteeing that for any one unit only a single thread of execution is running (For example, if an Update event and a HitDamage event is fired for a single unit then each of the associated functions must be run sequentially, not in parallel).

So, getting to the actual question... are there any scripting languages out there that support this (Plus the other usual prereq's such as being relatively fast, simple to learn\understand etc)?

As LUA seems to be the language of choice for game scripting (Although I'm not 100% sure why), do you know if LUA supports multithreading?

Note that I don't want explicit multithreading from the scripts. i.e. I don't want the scripts to have functions to launch threads, have synchronisation functions etc. I think things such a LUA Lanes are explicit threading libraries for LUA, and so doesn't meet this requirement.

I've actually written such a scripting system before as part of my day job, but that was about 10 years ago, and the code isn't really suitable for inclusion in a game.

Any help would be appreciated.

Thanks
Ben
Advertisement
angelscript supports multithreaded environments, i'd highly recommend it over LUA.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.
I've not used Lua across multiple threads before, but from what I understand, you have to create a separate Lua state for each thread to use, and have to disable the garbage collector from automatically triggering.
The issue is that the Lua GC interacts with every Lua state, so you have to manually trigger the GC process at appropriate times (when you know that no Lua states are in use).

angelscript supports multithreaded environments, i'd highly recommend it over LUA.


Thanks slicer4ever,

Other than the multithreading, what other reasons do you recommend AngelScript over LUA?

To be honest, I'm a little surprised that more traditional/established languages aren't used such as Javascript, BASIC, PASCAL etc.

Thanks
Ben

I've not used Lua across multiple threads before, but from what I understand, you have to create a separate Lua state for each thread to use, and have to disable the garbage collector from automatically triggering.
The issue is that the Lua GC interacts with every Lua state, so you have to manually trigger the GC process at appropriate times (when you know that no Lua states are in use).


Thanks Hodgeman, I'll see if I can find out some more info about that.

Thanks
Ben
You could always use AngelScript, which has gained popularity recently, and you could also add that functionality yourself in LUA with POSIX threads or something. LUA is written in ANSI C.

C dominates the world of linear procedural computing, which won't advance. The future lies in MASSIVE parallelism.


[quote name='slicer4ever' timestamp='1353491588' post='5002875']
angelscript supports multithreaded environments, i'd highly recommend it over LUA.


Thanks slicer4ever,

Other than the multithreading, what other reasons do you recommend AngelScript over LUA?

To be honest, I'm a little surprised that more traditional/established languages aren't used such as Javascript, BASIC, PASCAL etc.

Thanks
Ben
[/quote]

it's a bit of personal taste, but i personally hate type-less languages, i use them, but i absolutely hate what they can imply.

also, angelscript is hands down far easier to register with your game, lua requires a bit of hassle to easily and dynamically register functions from your game/engine to be used, where as angelscript is incredible simple, i can also completely disable GC on all my types, by passing one simple flag when creating a new type.

it's really leagues better imo, but that is just my opinion.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

it's a bit of personal taste, but i personally hate type-less languages, i use them, but i absolutely hate what they can imply.

also, angelscript is hands down far easier to register with your game, lua requires a bit of hassle to easily and dynamically register functions from your game/engine to be used, where as angelscript is incredible simple, i can also completely disable GC on all my types, by passing one simple flag when creating a new type.

it's really leagues better imo, but that is just my opinion.
LuaJIT's FFI totally elimination the no-so-fun binding process to register script function, it also has many other advances over standard Lua, plus it just came out of beta :) there is a far better GC in the works as well to eliminate a lot of common problems with the GC's too. IMO, for game stuff, if your going to look at Lua, skip vanilla Lua and go straight to LuaJIT

This topic is closed to new replies.

Advertisement