# The extent to which to use Lua scripting in a game

This topic is 2604 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm thinking of modularizing all of my game logic (by which I mean A.I.) and placing it in script form as optional attachments to objects (doodads, buildings) and NPCs. First off, let me note that I'm dealing with several hundred+ NPC's (call them monsters) plus tens to more than a hundred buildings on the map at the same time (a kind of an RTS case). This has placed me in a bit sceptical stance. Also, I don't think it'd be very easy for e to profile since I don't even have LUA implemented in the engine yet (which is why I'm asking for suggestions/guesstimations as to whether this would be worth it in the first place).

Here are a few sample cases where I'd like to use script-based AI (most of these are extremely trivial, but can be rather specific, which is precisely why I want to use scripting) - ideally I'd like to make as much behavior as possible externally scriptable:

* a non-animated doodad building composed of two or more objects, each of which can have a behavioral script associated with it. In a very simple case I'd like to ship the doodad as a self-contained custom model that includes the two components and optional LUA scripts for either component's behavior. Something like: "rotate top section by amount of frame time elapsed", essentially making the script AI to simply rotate the top half indefinitely if the artist decides he'd like that.
* a non-animated multi-state building: scan for activity when not engaged by rotating the antenna randomly left and right and build popcorn when some martians land nearby (again, the LUA part is actually the random turning bit)
* a non-animated building with a flicker light or particle emitter node, which is controlled by a LUA script instead of a built-in one

And so forth. At the end of the day I'd like to be able to write up flexible yet powerful motion behavior without including any pre-computed animation (to save space and design time, really) or hard-coded AI path in the model files. Ideally I'd like to take ALL AI out of the main loop and include it case-by-case in script form.

I don't really see a technical limitation to this; neither is there really an advantage if I instead spend the time writing a hardcoded AI path that covers all behaviors and thereby inevitably offers a finite number of possibilties (the major downside is my being stuck with writing a comprehensive editor that would allow me to parametrize every kind of supported AI behavior). TBH I'd like to avoid all that, however, and simply rely on scripts.

However, I'm somewhat wary of the potential computational hit - which is what I'd like some feedback on.

As a case study, how much scripting does WoW use at the end of the day? The entire GUI and casting system? This, indeed, seems to work fine seeing as 40-man raids were done without a hitch a number of years ago with full simulation on the server, which was probably hosting tens or hundreds of these. Should I even be worried about this? I mean, if the speed impact is less than 10-15%, then I won't even bother flinch since I'm targeting multicore systems anyway.

However, I'd really like to hear from people who have actual first-hand experience with this. Naturally, any other related suggestions are welcome!

##### Share on other sites
I have no real world experience of using Lua in a game, but you could also look into LuaJIT2. Its performance astounds me to the extent that I think I might have a nerd-crush on Mike Pall.

##### Share on other sites

As a case study, how much scripting does WoW use at the end of the day? The entire GUI and casting system? This, indeed, seems to work fine seeing as 40-man raids were done without a hitch a number of years ago with full simulation on the server, which was probably hosting tens or hundreds of these. Should I even be worried about this? I mean, if the speed impact is less than 10-15%, then I won't even bother flinch since I'm targeting multicore systems anyway.

However, I'd really like to hear from people who have actual first-hand experience with this. Naturally, any other related suggestions are welcome!

The GUI scripts WoW uses run on the clients only and aren't really affected by the number of people playing (Some addons will parse more data if more players are around though), the performance cost of these addons are fairly small as most of them only run a small 50 or so line script once or twice per frame.
In general the performance cost of using a scripting system sits at around 500% - 25.000% so only use it in areas where the cost is affordable, as a rule of thumb: if something executes less than a few hundred times per frame you can script it and noone will notice, Just remember that loops increase the number of times the code inside of the loops are executed, doing something like
 for i=0,100 do for j=0,100 do for k=0,100 do complexStuffhere(i,j,k) end end end 
will cause complexStuffhere() to be executed one million times and can have a very significant performance impact even if the script as a whole only runs a few times per frame.

High level AI scripts are a good target for scripted behaviour, i'd implement native functions for some expensive shared functionality though (Things like pathfinding problably doesn't have to be very flexible and can often be the single most expensive part of your AI scripts, having it as a native function could reduce the cost quite alot and you could also do things such as caching distance graphs for popular destinations)

##### Share on other sites
Thanks for the replies, guys! I'm definitely going for rather lightwight AI scripts only - I already have more expensive stuff like pathfinding implemented in fixed function. The thing is that I'm thinking of having lots of scripts, even though most are small, to add dynamic non-fixed function iterators (think of the buildings in Starcraft 2: I'm adding similar non-keyframe event-based animation to mine).

In any case, the numbers you've given me are pretty encouraging indeed an combined with pre-compiling the scripts they should end up being pretty fast to load and execute!

##### Share on other sites

In any case, the numbers you've given me are pretty encouraging indeed an combined with pre-compiling the scripts they should end up being pretty fast to load and execute!

Serisously, look at LuaJIT! It out paces precompiled Lua by orders of magnitude.

• 37
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631360
• Total Posts
2999554
×