• Welcome to AtomicTorch Studio Forums.
 

discontinued 2023 july 29th . by ninekorn discontinued 2023 july 29th . by ninek

Started by ninekorn, August 02, 2017, 09:12:47 PM

ninekorn

discontinued 2023 july 29th . by ninekorn

ai_enabled

Let me clarify how scripting works in VE.
All the JS files are loaded into isolated script engine instances. It means each script in the game (including topics) has independent global space. There are no shared global variables between script instances.
When you import another JS file into your JS file, its code is loaded separately into the same instance. But the global state is still not shared. So when you import another JS file, in fact, you're simply injecting its content into the importing JS file (it's totally different from C# "namespaces" concept, as you see). So that's why we also don't recommend importing huge JS files everywhere - only where you're really need.

Why is scripting in VE so limited? Because it was much easier for us to implement it this way. It allowed us to avoid many future problems. And we also using some global variables per-script/topic (from example, PLAYER_SHIP - when you're inside the topic script).

Another problem with global variables - we cannot serialize them easily and write into the savegame. So even if we had global variables (shared across all the scripts), it will be very easy to write bad code (which will prevent the game from properly saving the game state). So, we have storage scripting API instead. It's also very limited, but for the most cases, it was enough to get the things done.

ninekorn

discontinued 2023 july 29th . by ninekorn

ai_enabled

Well, I was not really clear when wrote "All the JS files are loaded into isolated script engine instances".

Imagine that for each JS file (except libs) we create a "script instance". All "include(*.js)" are simply injected (inlined) into the JS file.

That means that we create the script instances only once (during the startup) and then simply reusing them.

And that means we're using the same AI script instance for every NPC ship which shares the same AI script name. So if we Trader.js AI script, we using the same instance of this script for every trader NPC in the game.

And you might have noticed that our AI scripts don't store any variables. If we need to store or access anything, we're using storage API.

Regarding the "switch" - it will not work if the switchFunction variable is global (and so shared across all NPC's using this AI script; BTW, next time you will ever need to debug something like that - wrap all writes of the global variable into a function with console.print() printing current and new values together with ShipID at the function first line - it will be obvious when a global variable is shared as different NPCs will modify the same global variable).

The only way to make it work is to use storage API or "objectives" API (npc.AddObjective(), npc.GetCurrentObjective() - see Trader.js for example).

Regards!

ninekorn

discontinued 2023 july 29th . by ninekorn