Benefits of Entity Scripting

Continuing the discussion from Add an action called "change gravity for player":

I saw this thread about how to apply different gravity to each entity, and mino answered that we should use entity script to implement that. I will use entity script later, but I have several questions about it.

Then, I would like to answer several questions about entity script.
(Suppose there are N entities overall.)

・When it is compared with a conventional script that will explore all entities in game, is it less CPU-intensive?
・Is it because entity script does not traverse all entities every time they are called, while the conventional method I mentioned does check all entities, taking O(n) time whatsoever?

If so, it is one of the essences of using entity scripting. When there are only a few entities (UnitTypes) where something is applied, we can save computational cost to a great extent.

And I would like to answer this question as well.
・Still, does the game engine traverse all entities to see if there is any entity script attached to the entity each frame?

1 Like

These are good questions. For the record, taro2 is open-source now and can be found at github.com/moddio/taro2.

It is designed to be less CPU-intensive. There are still use-cases for global scripts, but in the old way that I believe you’re referring to, entity scripts would be better. Like when you have to perform an operation on a specific unit. You no longer have to iterate through all units in the game to find that unit.

Yes, entity scripts don’t traverse every entity in the game. The scripts are explicitly called through triggers. When the engine process starts, the triggers that the game uses are mapped to the scripts they are used for. This way, the only iteration done is on the collection of scripts that were triggered.

The same logic can be extended to scripts that are run from playEffects or abilities.

For your final question: not really. Since scripts are loaded at the beginning of the game, there is no need to check for them every frame.

3 Likes