Jump to content
Sign in to follow this  
Tankbuster

dynamic object names with part of name provided by a variable

Recommended Posts

Guys, how might I do this? I'm sure it's using format, but trial and error hasn't shown me the way yet.

I want a series of objects named 1obj, 2obj, 3obj where the number is provided by the counter in a loop.

Sort of (psuedocode)

tx=0
while {tx<10} do {
txobj = createVehicle blah etcetc;
tx = tx + 1:
};

Also, as I'm sure I'm not asking the right question, if someone could suggest how I might better title the thread, I'd be obliged. :)

Thanks,

Tanky -Paul-

Share this post


Link to post
Share on other sites

Try this mate....

_tx=0;
while {_tx<10} do {
   call compile format ["txobj%1 = 'AH1Z' createVehicle (position player)",_tx];
   _tx = _tx + 1:
   sleep 0.01;
};

You should end up with txobj0, txobj1, _txobj2 etc...

Also... there's no need for tx to be global unless you are using it somewhere else in your scripts...which you won't be in this little snippet that you posted...so keep it local with the underscore _tx.

Edited by twirly
Clarity

Share this post


Link to post
Share on other sites

for "_i" from 0 to 9 do {
   _var = "txobj" + str(_i); // Store in _var so you can use publicVariable with it as well if needed
   missionNamespace setVariable [_var, "AH1Z" createVehicle (position player)];
   // publicVariable _var; // Uncomment if you want the variable to exist on all connected machines
};

However generally speaking there are often only few reasons why you would need to assign a variable to a vehicle - aka, you can also collect the objects in arrays, and use setVehicleVarName or setVariable to tag the unit.

Edited by Sickboy

Share this post


Link to post
Share on other sites
for "_i" from 0 to 9 do {
   _var = "txobj" + str(_i);
   missionNamespace setVariable [_var, "AH1Z" createVehicle (position player)];
   // publicVariable _var;
};

Nice code. I don't pretend to know why that works... but it sure does... I just tested it.

Thanks for the snippet!

Share this post


Link to post
Share on other sites
Nice code. I don't pretend to know why that works... but it sure does... I just tested it.

Thanks for the snippet!

NP :-)

It is not only cleaner but also better performance; try to avoid call compile format where unneeded (99,9% of the cases).

Checkout the BIKI for command reference, e.g http://community.bistudio.com/wiki/missionNamespace

Edited by Sickboy

Share this post


Link to post
Share on other sites

My problem now is that I can see and understand how twirlys script works, but when I look at SB's more efficient example, I don't understand it! :)

I've had a look over the biki page. Perhaps we could discuss this a little deeper so I can understand it?

So missionNamespace is sort of like public variable space?

I can see how _var 'builds' the object name. Are we using missionNamespace to execute the createVehicle command in this example? If so, why does the createVehicle command look unlike the biki examples?

If I can't get my head around it, is twirlys script *so* inefficient that I shouldn't use it? I mean, when you say "99.9% of the time it's unneeded", I naturally think I shouldnt be doing call compile format. For me, it's only going to happen 100 times per mission - is the problem with call compile format a 'one off-at execution time' thing or will it affect server performance for ever afterwards.

Share this post


Link to post
Share on other sites

IMO the question isn't how often you're going to use it but how to improve your coding practices across the board.

Only move forward, not backward or sideways :)

Besides, you never know what else you will add to the mission, or what you will combine, use in other missions etc ;)

So seems sensible to apply good coding practices and improve them over time no?

I think you misunderstood me partially though, I meant 2 things:

1 - You generally dont need call compile format, try to avoid it

2 - You generally dont need to assign a unique variable to an object, as you can use Arrays and/or Object variables (setVariable) to tag objects. We talked about this before, in another thread about setVariable IIRC.

The following code examples all yield the same result:

abc1 = "AH1Z" createVehicle (position player);

_id = 1;
call compile format ["abc%1 = 'AH1Z' createVehicle (position player), _id];

_id = 1;
missionNamespace setVariable ["abc" + str(_id), "AH1Z" createVehicle (position player)];

Perhaps this helps in understanding the missionNamespace example better:

_id = 1;
_veh = "AH1Z" createVehicle (position player);
missionNamespace setVariable ["abc" + str(_id), _veh]; // same as: abc1 = _veh;

Edited by Sickboy

Share this post


Link to post
Share on other sites

Thanks Sicky.

I'm all in favour of moving forwards and I do appreciate yours and other help. :) But equally, I need to understand why method x is better than method y in a given situation so that I can work out which to use next time. To mangle your metaphor, perhaps a slight sideways move improves perspective sometimes? :)

Yes we did discuss setVariable, but although it wasn't made clear (by me) at the time, I was talking about object setVariable. missionNamespace is a whole new brain strain for me. I'm old - I'm not the information sponge I used to be! :)

I'm not assigning variables to objects any more, though I'm grateful for the insight from that other thread. In fact, I'm creating triggers on the server and instead of having a complex condition, I'm simply removing the triggers based on conditions. My tests have shown removing triggers that have complex conditions is good practice and to do this, they need to have unique, global names.

Your last example is particularly helpful. Thanks.

To paraphrase Cpt Oates. "I'm going into the editor. I may be some time." :)

Share this post


Link to post
Share on other sites

I guess the part I didn't get... and still don't get... is why creating a variable in missionNamespace that contains "AH1Z" createVehicle (position player) actually executes it as code!!

:confused:

Share this post


Link to post
Share on other sites

@Tank - Here is a thread from last year that documents a little more why you should use missionNameSpace setvariable over call compile:

http://forums.bistudio.com/showthread.php?t=99033

In sum, it is mostly about speed. see Post #5 by Xeno for details.

As far as exactly what you are doing is concerned, the missionNameSpace, is the nameSpace (container) for all the global variables in your mission. There are three ways to create/update global variables. Obviously the best/easiest is a = b. But, if you want the name of a to vary you can use missionNameSpace setVariable or call compile. missionNameSpace setVariable is the faster/better method.

Share this post


Link to post
Share on other sites
I guess the part I didn't get... and still don't get... is why creating a variable in missionNamespace that contains "AH1Z" createVehicle (position player) actually executes it as code!!

:confused:

plus one!

---------- Post added at 01:14 PM ---------- Previous post was at 01:12 PM ----------

@ Loyal, Ah right. Not read that until now. It seems that call compile format has a runtime performance penalty. Given that I'm going to be doing it maybe 100 times over a 4 hour mission, it's not going to hurt that much.

That said, I still want to understand every method.

Share this post


Link to post
Share on other sites
I guess the part I didn't get... and still don't get... is why creating a variable in missionNamespace that contains "AH1Z" createVehicle (position player) actually executes it as code!!

:confused:

Because it doesn't "Contain ah1z createvehicle (position player)" but the result from that statement (which is an Object Reference).

Hence I made this example to make it more clear:

_id = 1;
_veh = "AH1Z" createVehicle (position player);
missionNamespace setVariable ["abc" + str(_id), _veh]; // same as: abc1 = _veh;

Which also results in the same as:

abc1 = "AH1Z" createVehicle (position player);

Share this post


Link to post
Share on other sites

How do we handle creating code lines that may or may not require an equals sign?

_id = 1;
missionNamespace setVariable ["abc" + str(_id), "AH1Z" createVehicle (position player)];

gives us

abc1 = "AH1Z" createVehicle (position player);

despite there being no equals sign in the first code snippet. Is there some magic I don't know about going on?

So how do we handle, for example, a line that explicitly mustn't have an equals sign when I want to create this code?

For example, will

_id = 1;
missionNamespace setVariable ["abc" + str(_id), setTriggerArea [3,3,0, false]];

make

abc1 setTriggerArea[3, 3, 0, false];

Share this post


Link to post
Share on other sites

(missionNamespace getVariable ("abc + str(_id)) setTriggerArea[3, 3, 0, false];

However this is better readable :D

_trg = missionNamespace getVariable ("abc + str(_id));
_trg setTriggerArea [3, 3, 0, false];

Share this post


Link to post
Share on other sites

OK, I tracked down the equals sign now. :) Many thanks.

Share this post


Link to post
Share on other sites
@Tank - Here is a thread from last year that documents a little more why you should use missionNameSpace setvariable over call compile:

http://forums.bistudio.com/showthread.php?t=99033

Useful link.... good reading. Cheers for posting that.

Because it doesn't "Contain ah1z createvehicle (position player)" but the result from that statement (which is an Object Reference).

That's the bit I was missing. I have no performance issues with my call compiles... but I will have a look and try to understand how I might replace them. Thanks again.

Share this post


Link to post
Share on other sites

I think I've got it now....

As an example that may help others. I replaced:-

call compile format ["%1_PatrolPts = [_mkrnam,[100]] call getMarkerLocs;",_mkrnam];
call compile format ["_spawnpts = %1_PatrolPts",_mkrnam];

With:-

missionNamespace setvariable [_mkrnam + "_PatrolPts",[_mkrnam,[100]] call getMarkerLocs];
_spawnpts = missionNamespace getVariable (_mkrnam + "_PatrolPts");

Works fine!

Always good to learn new stuff. Many thanks to the OP and all concerned.

EDIT: ...and here's one I just can't seem to sort out :-

call compile format ["%1_Contacts set [count %1_Contacts, _enemygrp]",[b]_sid[/b]];

_sid is a side.... EAST, WEST...etc.

:confused:

Edited by twirly
Clarity

Share this post


Link to post
Share on other sites

SB, in post #8, the third code snippet, you use setVariable, and in post #15, it's getVariable. Is this intended?

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  

×