Jump to content
Sign in to follow this  
dr_eyeball

Scope of variables

Recommended Posts

I'm currently calling code in the Initialization field of lots of units with code like:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">Initialization field:

fn=compile preprocessFile "InitCivilian.sqf"; [this] call fn;

What I want is to be able to initialize the function variable once in either init file for lots of re-use in the Initialization fields, by doing something like:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">Init.sqf or Init.sqs:

functionX = compile preprocessFile "InitCivilian.sqf";<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">Initialization field:

[this] call functionX;

Q: What is the scope of (function) variables?

Q: How would this be done properly or is there a better way?

Currently functionX is undefined and just totally ignored, if done as shown in the bottom code and using the top code is just plain excessive.

Any advice appreciated.

Share this post


Link to post
Share on other sites

Look at the variables article in the Biki.

It says that script variables are visible inside the function the script calls, it says nothing about function variables visible in calling scripts though.

In my experience the way you want to load the function in the init.sqs works, and you can use it like a global variable in any onActivation line in the editor or scripts (you have to use "call" of course).

Share this post


Link to post
Share on other sites

Hi,

The problem is, the init fields of each unit are called before the init.sqs and init.sqf. Not 100% sure about init.sqf, but it sounds like thats the case from what you say.

From Chris Death:

Quote[/b] ]- init fields get executed in logical order of: first unit being

placed onto the map = first init field being executed

You can read the entire thread about the order of init events, here.

So you perhaps only need to load your function from the init line of the first unit you add to the mission? If you can't remember which one it is, either add\check it manualy in your mission.sqm. Or perhaps you can copy and cut all the objects in the mission editor, place a game logic and add your:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">functionX = compile preprocessFile "InitCivilian.sqf";

To the logics init field, then paste all the objects back into the mission?

Can't say for sure, but it might be one way of doing it?

Share this post


Link to post
Share on other sites

The problem is that in init fields etc, you probably need to use a dummy variable like:

du=[this] call functionX;

Lol, same time posted with UNN... It is indeed possible that the init.sqf is later executed.

Share this post


Link to post
Share on other sites

Sorry to bring this one up again.

But that didn't do the trick either, atleast not for me.

Having in my mission.sqm

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">class Item0

{

side="LOGIC";

class Vehicles

{

items=1;

class Item0

{

position[]={13592.503906,19.980000,9301.290039};

id=0;

side="LOGIC";

vehicle="Logic";

leader=1;

skill=0.600000;

text="TAURUS";

init="ScrpRemIfSf = compile preProcessFile ""miscstf\scrpRemIfSf.sqf"";";

description="FIRST";

};

};

};

And then later.

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">...

class Item12

{

side="EAST";

class Vehicles

{

items=10;

class Item0

{

position[]={14447.137695,58.534901,9503.787109};

id=164;

side="EAST";

vehicle="SquadLeaderE";

leader=1;

rank="SERGEANT";

skill=0.466667;

init="nil = [this,2] spawn ScrpRemIfSf;";

};

The script isn't executed.

Problem is when I use

blabla = [this,2] execVM "myScript.sqf";

in the init, it seems the game engine doesn't get time to execute all of them, this line is placed in like 46 or so units inits.

Putting

blabla = [this,2] spawn compile preProcessFile "myScript.sqf";

However works, but seems not to wise.

Please advice.

Share this post


Link to post
Share on other sites

You could try adding this to all your init lines:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">If ((TypeName ScrpRemIfSf)=="CODE") Then {[this,2] spawn ScrpRemIfSf} Else {ScrpRemIfSf=compile preProcessFile "miscstf\scrpRemIfSf.sqf"; [this,2] spawn ScrpRemIfSf}

This way, it does not matter which init line is called first and it will only compile the code once.

Share this post


Link to post
Share on other sites

have you tried call instead of spawn?

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">[this,2] call ScrpRemIfSf;

you keep calling it a "function" but you spawn it like a script...

edit: I see Dr Eyeball used call... nvrmind..

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×