Scripting: Globals vs Locals

Post » Mon Sep 05, 2011 5:44 am

Ok, CS folks, here comes another question from Sadun :) I'll have a vast amount of variables and, unfortunately, they must be accessible almost all the time.

So, I can store every variable as a global and it'll work without any probles, as I suppose. let's call this 'A option'.

However, I can make a script with only and only variables inside. Then, I would use StartScript/StopScript to get or store my vars. This's 'B option'.

Now, which one is less destructive in your opinion?
User avatar
Mimi BC
 
Posts: 3282
Joined: Sat Oct 07, 2006 10:30 pm

Post » Sun Sep 04, 2011 8:50 pm

I think that I'd need a little more info... like how are you calling and updating these variables in the script? If you're calling/updating them from dialogue, then attaching the script to a object is the way to go, I'd think.

If you're having to write/run extra scripts to use startscript, that;s not likely the best option.
User avatar
Riky Carrasco
 
Posts: 3429
Joined: Tue Nov 06, 2007 12:17 am

Post » Sun Sep 04, 2011 10:25 pm

E.g. there are several abilities, one of these is the Precision. In my custom level-up menu variable will be set to 1 (if player is obtaining this skill, naturally). Now, imagine that there are many more abilities which require its own boolean determiner. A great deal of the abilities are strongly connected with fighting and script pinned to objects has to check mentioned booleans (e.g. if creature is hit by bow it checks precision's boolean).
I don't want to have a mess in files, so maybe it's better to run global script only with vars instead of adding uncountable amount of globals
User avatar
Farrah Barry
 
Posts: 3523
Joined: Mon Dec 04, 2006 4:00 pm

Post » Mon Sep 05, 2011 1:14 am

Use globals.

The primary reason is you have no other options. Member locals accessed cross-script involve some overhead and introduce significant bugs; there are quite a few places you can't use them, they don't work with all functions, and sometimes just don't work.

Quite a bit of benchmarking was done some time ago regarding globals, and adding and using up to 250,000 or so caused no measurable slowdown and a bit more than 1 MB of memory use. Just use a good, unique modder and mod prefix.
User avatar
Danial Zachery
 
Posts: 3451
Joined: Fri Aug 24, 2007 5:41 am

Post » Mon Sep 05, 2011 4:16 am

That's really good to know! I've wondered a time or two myself when the globals seemed to really be piling up.
User avatar
leni
 
Posts: 3461
Joined: Tue Jul 17, 2007 3:58 pm


Return to III - Morrowind