OFPEC Forum

Editors Depot - Mission Editing and Scripting => OFP - Editing/Scripting General => Topic started by: RKurtzDmitriyev on 05 Jun 2010, 00:33:54

Title: preProcessFile--local vs. global variables
Post by: RKurtzDmitriyev on 05 Jun 2010, 00:33:54
Hi guys.

Question
Suppose I'm running an SQS script. If I assign a local variable to a function using the preProcessFile command (see example), will the pre-processed code be discarded after the script exits? If not, will running that same script again cause a duplicate function to be created? If so, will the cost of RAM be significant?

Example
Code: [Select]
_distanceFormula = preProcessFile "distancepos.sqf"

_dist = [getpos player, getpos alife] call _distanceFormula
? _dist > 1000 : hint "You are very far from having a life"
exit

Explanation

I'm making a mission that uses a lot of functions. Right now I have them all assigned to global variables in the init.sqs file.

Code: [Select]
distanceFormula = preprocessfile "distancepos.sqf"
gridCoord = preProcessFile "kurtzgridcoord.sqf"
gridCenter = preProcessFile "gridCenter.sqf"
;etc....

This is using a lot of global variables and I'm concerned that I could run aground on the :banghead: saveGame bug. Therefore, I'm considering listing the needed functions at the beginning of each SQS script (like in the example above), rather than having a single master list of functions.

However, I'm worried that this technique will create useless duplicates of functions saved in RAM, because each script might load a new instance of each .sqf file without deleting the old ones.

Any suggestions? :)
Title: Re: preProcessFile--local vs. global variables
Post by: h- on 05 Jun 2010, 05:40:47
I have no idea whether duplicate preprocessed functions glog up the memory, but as long as I can remember everybody has made the inits always so (in addons) that if the init preprocesses functions that init part will only run once so I guess is just better to be safe.

You could try to arrange your global variables in global array(s) because one array regardless of how much it contains is just one variable, so this way you can reduce 100 global variables into 1.

Like functions = [distanceFormula = preprocessfile "distancepos.sqf", gridCoord = preProcessFile "kurtzgridcoord.sqf",gridCenter = preProcessFile "gridCenter.sqf"]

And then later when you need a function
Code: [Select]
_blah = _bleh call (functions select 1)
Not sure that would work with functions but at least it works with regular global variables.
Title: Re: preProcessFile--local vs. global variables
Post by: RKurtzDmitriyev on 05 Jun 2010, 14:13:03
Ah, thanks h-. I was worried that no one knew/cared enough about old OFP scripting to answer such an advanced question. :)

A single global array sounds like a good idea and I bet it will work. However, are you sure you meant to type this

Code: [Select]
functions = [distanceFormula = preprocessfile "distancepos.sqf", gridCoord = preProcessFile "kurtzgridcoord.sqf",gridCenter = preProcessFile "gridCenter.sqf"]

Or something more like this?

Code: [Select]
functions = [preProcessFile "distancepos.sqf", preProcessFile "kurtzgridcoord.sqf", preProcessFile "gridCenter.sqf"]

The former example would seem to be using lots of global variables anyway.

But anyway, thanks for the input. I didn't think about the global arrays option, that will probably capture the best of both worlds.
Title: Re: preProcessFile--local vs. global variables
Post by: h- on 05 Jun 2010, 18:29:50
The latter option would most likely give you just errors because the preprocessed function has to be stored in a variable.

You should of course use local variables instead of the global ones, my bad  :D
Code: [Select]
functions = [_distanceFormula = preprocessfile "distancepos.sqf", _gridCoord = preProcessFile "kurtzgridcoord.sqf",_gridCenter = preProcessFile "gridCenter.sqf"]