Author Topic: Trying to set a global variable without using(storage);  (Read 85 times)

ninekorn

  • Newbie
  • *
  • Posts: 41
    • View Profile
Trying to set a global variable without using(storage);
« on: August 02, 2017, 09:12:47 pm »
Hi,

Ive got this question about creating a global variable in VoidExpanse javascript. Again, its something super easy in Unity3d but im not sure i get it in VoidExpanse. For example i created this script called globalArrays.js to store global variables.

Code: [Select]
var globalSwitchForMining = false;


var globalArrays =
{
miningSwitch: function (args)
    {
globalSwitchForMining = args;
console.Print(globalSwitchForMining + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    },

isMiningSwitchOn: function ()
    {
console.Print(globalSwitchForMining + "?????????????????????????????????????????????????????????");
return globalSwitchForMining;
    },
};


Then from another script I use this:
Code: [Select]
globalArrays.miningSwitch(true);

if (globalArrays.isMiningSwitchOn() == true)
{
  console.Print("YOU TURNED THE SWITCH ON");
}
else
{
console.Print(" THE SWITCH IS STILL OFF");
}

And in my last script to verify the global Variable i just use:
Code: [Select]
if (globalArrays.isMiningSwitchOn() == false)
{
console.Print("isMiningSwitchOn == false");
}


if (globalArrays.isMiningSwitchOn() == true)
{
console.Print(" WHERE AM I STUCK");
goMineSomeAsteroids();
canFollowPlayer = false;
}

and bam. Houston we've got a problem. the global variable is always false from the third script. If i cant create my own global variables then ill go back to use the using(storage) to set global variables.

ai_enabled

  • AtomicTorch Founder
  • Hero Member
  • *****
  • Posts: 988
    • View Profile
Re: Trying to set a global variable without using(storage);
« Reply #1 on: August 03, 2017, 03:46:43 am »
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.