Lua "state" question for the experts

Pazyryk

Deity
Joined
Jun 13, 2008
Messages
3,584
I have many little "self-contained" Lua files that do stuff, often in response to an Event or GameEvent, and don't really need to interact with other Lua code (except perhaps accessing some mod data from MapModData). There is an example in this thread where a SerialEventCityCreated listener listens for new city graphic creation and then changes that graphic (in this example, city ArtStyleType).

My question is: Is it better to add these files each in their own state as a separate UIAddIns, or to pile them into my mod's main state with includes? At least in my imagination, different states are able to run in parallel and so the former could allow for parallel processing. I've seen some evidence for this (I think) in the timing of print statements from different states. But I could be wrong (I read somewhere that Lua threads aren't really threads). Perhaps there is no right answer from the "code architecture" point of view and it's just modder preference...
 
I think it's modders preference. Lua itself doesn't implement threads - it supports something called co-operative multi-tasking (via co-routines) that can appear as threads. That is not to say that the C++ core couldn't itself run several Lua instances (sand-boxes) in multiple threads, but there is nothing in the core DLL that indicates that it is thread safe (synchronised blocks, object locks, etc) - and indeed a lot of the core DLL is definately NOT thread safe.

I think the apparent threading that is seen from the log messages is more due to uncommitted buffered writes to the log file than anything else - although the sequencing of events definately changed around build 674, it seemed to move from an immediate call model to an queue model.

I suspect the animation code is the heavily threaded part of the system, which is why you can initiate several combats and scroll between them. Also the networking code for multi-player support.

Personally I'd favour small logical modules over one big file, at least that way you don't inadvertantly redefine a function/global. Which would be a bigger concern for me than ever so slightly longer load times.
 
Back
Top Bottom