Jump to content
Sign in to follow this  
Rough Knight

Passing a container to another script?

Recommended Posts

Hi Guys,

I will try explain my problem.

In one script I have:

obj1 = 1; * I want to pass on "obj1" to another script ie the container [not the value of obj1].

so:

nul = [obj1] execvm "myscript.sqf"; * this passes on the value rather than the container?

Is there a way to pass on the container rather than the value, to my "myscript.sqf" so it can referece a changing value of obj1?

The reason I need this, is because I would like to use the same script to be activated also by obj2, obj3..etc which all have different and changing values.

I am not sure if I have explained myself well, but I am open to suggestions. I am sure there is a simple solution, I just don't know it or can't think of it.

trying nul = [format ["%1", obj1]] execvm "myscript.sqf"; just changes the value of obj1 to a string I believe?

Thanks

Frosty

Share this post


Link to post
Share on other sites

obj would be a global variable and be available to all scripts anyway.

Can you give a specific example of what you're trying to do and what your script does? Might be an easier method.

Share this post


Link to post
Share on other sites

Hi kylania,

Thanks for your reply.

In one script I have:

//values to illustrate issue. Lines are actually in different scripts
[color="Red"]obj1 = 1[/color];
[color="red"]obj5 = 1[/color];

//calling code.
nul = [pilot1, f35b_1, "obj1_start_location", "airbase", 500, (getdir f35b_1), [color="Red"]obj1[/color]] execvm "resetpos.sqf";
nul = [pilot3, a10_1, "obj5_start_location", "airbase", 500, (getdir a10_1), [color="red"]obj5[/color]] execvm "resetpos.sqf";

then resetpos.sqf:

* _objective would ideally be "obj1" rather than the value of obj1 which can be any number from 0 to 3.

// Script to reset aircraft postion on mission completion.

private ["_unit", "_vehicle", "_marker1", "_marker2", "_distance", "_angle", "_objective"];

_unit 	            = _this select 0;
_vehicle	            = _this select 1;
_marker1	            = _this select 2;
_marker2	            = _this select 3;
_distance            = _this select 4;
_angle	            = _this select 5;
[color="red"]_objective[/color]           = _this select 6;

sleep 0.1;

waituntil [color="red"]{_objective[/color] >= 2}; //Here is the "obj1" container I would like referenced in the calling script.

//rest of script.

When the script above gets called, typically obj1 or obj5 would be of value 1. Depending on mission outcomes the value changes from 2-4. Basicaly I want to hold the script until a particular value of obj1 or obj5 is met.

I hope that explains it a bit better? Soz mate I'm a very noob scripter.

Thanks

Frosty

Edited by Rough Knight

Share this post


Link to post
Share on other sites

Pass the objective to resetpos.sqf as "obj1" - a string, with the quotes.

Then in resetpos.sqf:

call compile format["waituntil {%1 >= 2}", _objective]; //Here is the "obj1" container I would like referenced in the calling script.

Share this post


Link to post
Share on other sites

put the obj's in an array

objarray = [obj1,obj2,obj3];

then pass the obj number (0, 1 or 2) to the script,

then use:

waituntil {(objarray select _objective) >= 2};

Edited by bravo1romeo

Share this post


Link to post
Share on other sites
waituntil {(objarray select _objective) >= 2};

That won't work as it's simply passing the current value of the obj1 at the time to the waitUntil. If you change the value of obj1 later that waitUntil won't know about it and never trigger.

That's why you have to use the call compile to send the object not the value to the waitUntil so that when you change the value of obj1 later the waitUntil registers the change.

Share this post


Link to post
Share on other sites

@kylania;

bravo1romeo's method isn't passing the current value; it's just passing a number that tells the script which element of the "objarray" array to check. If the array is changed later, this would be reflected in the script.

Your method won't quite work, athough something similar would:

waitUntil {(call compile _objective) >=2}

The difference here is that the variable is being continuously checked as the call is within the waitUntil loop. In your version, the call takes place outside of the loop, so the variable is only checked once; the initial value will be used perpetually.

Share this post


Link to post
Share on other sites

I tested both methods. Mine worked when you changed the value after calling the scripts. His didn't. Test them.

Share this post


Link to post
Share on other sites

After testing, both methods work. It's interesting that the call in your example doesn't evaluate the variable within the waitUntil loop (which is why it works), but on second thought, I guess that makes sense. As for the array method, it does work, but the caveat is that it requires you to manually update the array every time you change any of the elements (i.e., "objArray = [obj1,obj2,obj3]" will need to be run every time there is a change in order to update). Alternatively, you could just a have a continuous loop running somewhere that does this, but that isn't really ideal.

So, in conclusion, just forget what I said and use kylania's method. :)

Edited by ST_Dux

Share this post


Link to post
Share on other sites

Thanks kylania,

That works a treat.

I knew there was a simple way to do it.

I am not sure I fully understand call and compile comands as the wiki info is pretty vague in regards to it.

Thanks again

Frosty

Share this post


Link to post
Share on other sites
I am not sure I fully understand call and compile comands as the wiki info is pretty vague in regards to it.

Yeah, the Biki doesn't do the best job explaining these concepts. Basically, it works like this:

All "compile" really does is convert a string to code. Think of it as simply removing the quotation marks and replacing them with curly braces.

"Call" is used to execute code blocks; that's all that it does. Generally, it's not necessary to use call because normal lines of code are executed anyway, either within the editor or through exec/execVM referencing an outside file. It becomes necessary to use call in two cases:

1. As in this case, call is used when you want a certain part of your code to be dynamic. By using "format" to create a string using a variable and "compile" to then convert that string into code, call can be used to effectively run a variable line of code.

2. Call is used to execute preprocessed functions, which can either be declared within the script file you're working on or created in a separate file. Functions are essentially a (sometimes quite large) set of instructions within a code block and are usually created whenever you're going to need to complete a certain coding task several times. Rather than "hard-code" everything, you can create functions that take variable parameters and call them in any script at any time using "call."

Anyway, I hope that helps. The important thing to remember about these two commands is that basically they only do one simple thing each. Compile converts "strings" to {code}, and call takes {code} and runs it through the engine. Simple commands with a lot of potential.

Share this post


Link to post
Share on other sites

I've seen another related way of creating functions in the Op Arrowhead campaign, they create functions by simply assigning blocks of script to a variable name, then using call on the variable when needed. There is no format or compile used. I don't know if this means that such functions are slower, but it looks very organized. They sometimes set up several functions in one file.

Share this post


Link to post
Share on other sites

Thanks for the info guys.

Are you guys from programming backgrounds or you just learn this on the fly like me?

Thanks

Frosty

Share this post


Link to post
Share on other sites
Are you guys from programming backgrounds or you just learn this on the fly like me?

I've just been learning on the fly since OFP; I have no background in programming. There are a lot of folks on these forums who do have programming backgrounds, though.

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  

×