Jump to content
Sign in to follow this  
Lysandius

Initializing a global variable

Recommended Posts

Hi there,

I'm trying to tag objects in the editor so I can use them in a script later. This functionality would enable me to use every object with a certain tag for specific purposes.

The way I am tagging right now is by adding the object to an array in the object initialization field. And later I can use all elements in that array anywhere in any scripts. However this array has not yet been created at this point so I can not add the object to it yet.

How would I be able to have the array available so I can add anything I've placed in the editor to it by putting 'taggedUnits = taggedUnits + [this]' in the initialization field?

Any help would be appreciated. Thanks!

regards,

Lysandius

Share this post


Link to post
Share on other sites
if (isNil "taggedUnits") then {taggedUnits = []}; taggedUnits set [count taggedUnits, this]

Share this post


Link to post
Share on other sites
declare this at the start of your mission

taggedUnits = [];

is this what you mean?

Thanks for the answer.

This is indeed what I'd like. But where do I put the above code? The init.sqf file would be insufficient since it is run after the object initialization fields. So first the game would add the objects to an array that does not exist and afterwards it would create an empty array.

Just to clarify my problem perhaps...

I'd like to create a vehicle, an ammobox, a building, 3 OPFOR units and 4 BLUEFOR units. A trigger should activate when they're all dead and another trigger should activate when they are 50m from a certain flag. If the mission seems imbalanced I would like to be able to add a fourth OPFOR unit that is taken into account. Or a fifth. Or a second ammobox... I also want to be able to add a third trigger and define that 2 of the OPFOR units are eligible to trigger it as well as the vehicle.

What I would like to avoid is adding unique names of objects in the editor AND in the initialization blocks AND in the trigger condition blocks AND in script files. Just add one unit in the editor and put 'trigger1Units = trigger1Units + [this]' or 'trigger1Units = trigger1Units + [this]; trigger2Units = trigger2Units + [this]' to the initialization field, depending on which trigger(s) it should be linked to.

I hope someone can help me with this problem. :)

---------- Post added at 09:57 ---------- Previous post was at 09:49 ----------

if (isNil "taggedUnits") then {taggedUnits = []}; taggedUnits set [count taggedUnits, this]

Thanks for the reply! This is almost what I want. :)

If I want to add multiple tags to one unit though:

if (isNil "tag1Units") then {tag1Units = []};
tag1Units set [count tag1Units, this];
if (isNil "tag1Units") then {tag1Units = []};
tag3Units set [count tag3Units, this];
if (isNil "tag1Units") then {tag1Units = []};
tag7Units set [count tag7Units, this];

The above seems less clear than the following:

tag1Units set [count tag1Units, this];
tag3Units set [count tag3Units, this];
tag7Units set [count tag7Units, this];

The different tagXUnits would first be declared in a script file preferably.

The problem is not so much that that it's impossible in the editor. It is definitely possible but I would love it to be very easily doable inside the editor once the underlying code has been written. Maybe I should just settle for the normal-people-way. :)

Edited by Lysandius

Share this post


Link to post
Share on other sites

I'd do the following in the init.sqf:

abc_taggedUnits = [];
abc_taggedUnits set [count abc_taggedUnits, p1]
abc_taggedUnits set [count abc_taggedUnits, abc_myTruck]
abc_taggedUnits set [count abc_taggedUnits, abc_myMotorbike]

Then you will need to set a name for each object (p1, abc_myTruck, abc_myMotorbike), and in the init.sqf you create the array that you can use for the rest of your mission development.

Then as a condition in an end trigger (or any trigger) you could write something like this:

(Fires when all units are dead)

({ alive _x } count abc_taggedUnits) == 0;

Btw, always use your personal prefix when writing globals. In this exampel "abc_".

Share this post


Link to post
Share on other sites

If all you want us more units if there are more players you can just put down all the max units you might want in a fully loaded mission. Then in the "extra units" use thier Condition of Presence field to control if the spawn or not using whatever formula say by counting # of players.

Share this post


Link to post
Share on other sites
if (isNil "taggedUnits") then {taggedUnits = []}; taggedUnits set [count taggedUnits, this]

That problem might be solved, but as I was wondering about this, figured I might just ask here. What I would usually do is for example

MissionNameSpace getVariable ["taggedUnits", []];

over the isNil check.

To be perfectly fair, I go this way because it takes less space in my notepad and it looks cool, but in terms of performance, which would be best?

Share this post


Link to post
Share on other sites
That problem might be solved, but as I was wondering about this, figured I might just ask here. What I would usually do is for example

MissionNameSpace getVariable ["taggedUnits", []];

over the isNil check.

To be perfectly fair, I go this way because it takes less space in my notepad and it looks cool, but in terms of performance, which would be best?

Being new to Arma scripting I'm not sure which would perform the best. However I like the namespace one better than checking for nil. Your code takes less space in your notepad but it does not add 'this' to the array. If it does, it's longer.

Edited by Lysandius

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  

×