Home   Help Search Login Register  

Author Topic: SOLVED: Best way to increment names?  (Read 2233 times)

0 Members and 1 Guest are viewing this topic.

Offline corrupt3d_Oracle

  • Members
  • *
  • I'm a llama!
SOLVED: Best way to increment names?
« on: 17 Jul 2007, 05:11:43 »
Ok, I have a AI spawn script that allows the user to spawn as many AI as he wants called from the radio command menu.

The problem is I assign the name of the soldier to be spawned 'soldier1' lets say.

so, once that soldier is spawned, that name is taken and a second soldier cannot be spawned, even if that variable (name) is set to nil or if the 'vehicle' (unit) is deleted using deleteVehicle command.

My solution was to come up with a counter/loop of sorts that would make the number increase on the end of the name. But, as seems obvious, you can't add strings and integers together (we don't have casting in sqs :( ).

Is there a good way to do something like this? Or do I need to make a very large array and just go through it assigning each new soldier a name out of there?

Here is an example of what I want:

Code: [Select]
variable = 1

#loop
~2
hisName = "hisName" + variable; //variable is an int in this case, so it won't actually add (THIS PRODUCES AN ERROR)
hint format["%1", hisName]; //Ideally, this would return hisName1 then hisName2 on the next loop, then hisName3...etc
variable = variable + 1;
goto "loop"

Thanks for any help guys,

Corrupt3d
« Last Edit: 17 Jul 2007, 19:41:37 by corrupt3d_Oracle »

Offline LCD

  • Former Staff
  • ****
    • Everon Cartel
Re: Best way to increment names?
« Reply #1 on: 17 Jul 2007, 10:15:52 »
use da format command :P

hisName = format ["hisname%1",variable]

:D

LCD OUT
"guess being the community has downsides .." - cheetah
Help Perfecting Da Next Best Thing - O-Team Beta

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: Best way to increment names?
« Reply #2 on: 17 Jul 2007, 11:29:47 »
Code: [Select]
...
_variable = 1;
call compile (format ["hisname%1",variable]) = createUnit blah blah blah blah;
_variable = _variable + 1
...

That inside a loop would create global varis hisname1, hisname2, hisname3 ...

Offline LCD

  • Former Staff
  • ****
    • Everon Cartel
Re: Best way to increment names?
« Reply #3 on: 17 Jul 2007, 11:45:50 »
wont it work if we just use

format ["hisname%1",variable]) = createUnit blah blah blah blah;

w/o usin da call compile command ???

LCD OUT
"guess being the community has downsides .." - cheetah
Help Perfecting Da Next Best Thing - O-Team Beta

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: Best way to increment names?
« Reply #4 on: 17 Jul 2007, 12:38:40 »
Nope, you are asigning a unit to a string, that would generate an error.

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
Re: Best way to increment names?
« Reply #5 on: 17 Jul 2007, 16:21:58 »
Quote
w/o usin da call compile command ?
For OFP you would still need to use call format to change the string to a variable. Now for ArmA you must also use compile as above.
urp!

Offline corrupt3d_Oracle

  • Members
  • *
  • I'm a llama!
Re: Best way to increment names?
« Reply #6 on: 17 Jul 2007, 19:41:22 »
Ahh, I was missing the compile bit when I tried using format.

Thanks so much again guys!

Corrupt3d

Offline LCD

  • Former Staff
  • ****
    • Everon Cartel
Re: SOLVED: Best way to increment names?
« Reply #7 on: 17 Jul 2007, 23:17:46 »
ohh right didnt think bout da whole string to vari thing :D

LCD OUT
"guess being the community has downsides .." - cheetah
Help Perfecting Da Next Best Thing - O-Team Beta

Offline Cheetah

  • Former Staff
  • ****
Re: SOLVED: Best way to increment names?
« Reply #8 on: 26 Jul 2007, 15:53:40 »
Right, the following code gives me an error (missing ; --> "...mpile (format ["hisname%1",variable]) #= group playe...")

Code: [Select]
#loop
variable = 1;
call compile (format ["hisname%1",variable]) = group player createUnit ["SoldierWB",_startPos,[],0,"FORM"];
variable = variable + 1;
~1
goto "loop"

What is is that I'm doing wrong? (sqs file).
Like missions? Help with Beta Testing! or take a look at the OFPEC Missions Depot for reviewed missions!

Offline firecontrol

  • Members
  • *
Re: SOLVED: Best way to increment names?
« Reply #9 on: 26 Jul 2007, 17:39:34 »
Code: [Select]
#loop
_i = 1;
call compile format ["hisname%1 = group player createUnit [""SoldierWB"", _startPos, [], 0, ""FORM""]", _i];
_i = _i + 1;
~1
if (_i <= 10) then {goto "loop"}


Note that the way it was set up, we'd get an infinite loop, and you'll have thousands of men spawning, one per second....

add a:
Code: [Select]
if (variable <= 10) then {goto "loop"};

to limit the numbers of men. Normally for this we can use a local variable, like _i instead of a global one, too.


As a matter of fact... here's one of my scripts. It's creating reinforcements, putting them into a truck, and sending them somewhere. It creates anywhere from 2 to 9 men, based on how many men the player's squad has when called... see below:

Get's player squad strength, calls next script.
Code: [Select]
//Author:       FireControl
//File:         reinforce.sqf
//Function:     Creates a backup team for the player, makes player's squad 10 total. Brings some ammo as well.
//Parameters:   [none]
//Returns:      Nothing
//To Use:       Called from artydead.sqs upon the disabling of enemy arty.
//Notes:        None.


private ["_numUnits"];
_numUnits = 0;

waitUntil {rollRF};

_numUnits = count units (group player);

switch (_numUnits) do {
case 8: {handle01 = [2] execVM "createReinf.sqf";};
case 7: {handle01 = [3] execVM "createReinf.sqf";};
case 6: {handle01 = [4] execVM "createReinf.sqf";};
case 5: {handle01 = [5] execVM "createReinf.sqf";};
case 4: {handle01 = [6] execVM "createReinf.sqf";};
case 3: {handle01 = [7] execVM "createReinf.sqf";};
case 2: {handle01 = [8] execVM "createReinf.sqf";};
case 1: {handle01 = [9] execVM "createReinf.sqf";};
};

if (true) exitWith {};


Creates the stuff!
Code: [Select]
//Author:       FireControl
//File:         createReinf.sqf
//Function:     Creates a backup team for the player, makes player's squad 10 total. Brings some ammo as well.
//Parameters:   [Integer] ... number of additional men to create.
//Returns:      Nothing
//To Use:       Called from reinforce.sqf.
//Notes:        None.

private ["_strength","_rfGrp","_a","_numMen","_type"];
_strength = _this select 0;
_a = _strength;
_numMen = 0;
_type = "";
_rfGrp = createGroup west;


ftTruck = createVehicle ["Truck5t", getMarkerPos "reinforce", [], 0, "NONE"];
ftTruck setDir 180;

clearMagazineCargo ftTruck;
ftTruck addMagazineCargo ["30Rnd_556x45_Stanag",50];
ftTruck addMagazineCargo ["1Rnd_HE_M203",20];
ftTruck addMagazineCargo ["200Rnd_556x45_M249",12];
ftTruck addMagazineCargo ["100Rnd_762x51_M240",12];
ftTruck addMagazineCargo ["15Rnd_9x19_M9",20];
ftTruck addMagazineCargo ["M136",15];
ftTruck addMagazineCargo ["PipeBomb",5];

while {_strength > 0} do {
switch (_nummen) do {
case 0: {_type = "SoldierWB";};
case 1: {_type = "SoldierWB";};
case 2: {_type = "SoldierWMedic";};
case 3: {_type = "SoldierWAT";};
case 4: {_type = "SoldierWAR";};
case 5: {_type = "SoldierWG";};
case 6: {_type = "SoldierWMG";};
case 7: {_type = "SoldierWB";};
case 8: {_type = "SoldierWAT";};
};
call compile format ["rf%1 = _rfGrp createUnit [_type, getMarkerPos ""reinforce"", [], 0, ""NONE""]",_strength];
_strength = _strength - 1;
_numMen = _numMen + 1;
};

if (debugMode) then {
player sideChat format ["_rfGrp: %1",(units group rf1)];
player sideChat format ["# Created: %1",_numMen];
};

while {_a > 1} do {
call compile format ["rf%1 moveInCargo [ftTruck, %1]",_a];
_a = _a - 1;
};


rf1 moveInDriver ftTruck;
sleep 1;
[8] exec "radio.sqs";
sleep 1;

rf1 doMove (getMarkerPos "rfWP0");
waitUntil {(rf1 distance (getMarkerPos "rfWP0")) < 10};
rf1 doMove (getMarkerPos "rfWP1");
waitUntil {(rf1 distance (getMarkerPos "rfWP1")) < 10};
rf1 doMove (getMarkerPos "rfWP2");
waitUntil {(rf1 distance (getMarkerPos "rfWP2")) < 10};
rf1 doMove (getMarkerPos "rfWP3");
waitUntil {(rf1 distance (getMarkerPos "rfWP3")) < 10};
rf1 doMove (getMarkerPos "rfWP4");
waitUntil {(rf1 distance (getMarkerPos "rfWP4")) < 10};
rf1 doMove (getMarkerPos "rfWP5");
waitUntil {(rf1 distance (getMarkerPos "rfWP5")) < 10};
rf1 doMove (getMarkerPos "rfWP6");
waitUntil {(rf1 distance (getMarkerPos "rfWP6")) < 10};
rf1 doMove (getMarkerPos "reinforceWP");

waitUntil {(rf1 distance (getMarkerPos "reinforceWP")) < 10};
waitUntil {(speed ftTruck) <= 1};
sleep 0.5;

{unassignVehicle _x; _x action ["GETOUT", vehicle _x]; sleep 1;} forEach units group rf1;
sleep 2;
{[_x] joinSilent group player} forEach units group rf1;

"reinforceWP" setMarkerType "EMPTY";
"rfWP2" setMarkerType "EMPTY";
"rfWP3" setMarkerType "EMPTY";
"rfWP4" setMarkerType "EMPTY";
"rfWP5" setMarkerType "EMPTY";

if (true) exitWith {};


Feel free to borrow, use, modify, etc... a small credit would be nifty! :)
« Last Edit: 26 Jul 2007, 17:51:18 by firecontrol »

Offline LCD

  • Former Staff
  • ****
    • Everon Cartel
Re: SOLVED: Best way to increment names?
« Reply #10 on: 26 Jul 2007, 23:05:14 »
@ cheetah

add couple o baracets around da group player.... it shud work boss :D

LCD OUT
"guess being the community has downsides .." - cheetah
Help Perfecting Da Next Best Thing - O-Team Beta

Offline Cheetah

  • Former Staff
  • ****
Re: SOLVED: Best way to increment names?
« Reply #11 on: 27 Jul 2007, 12:25:07 »
Code: [Select]
call compile format ["soldier%1 = group player createUnit [""SoldierWB"", position player, [], 0, ""FORM""]", _i];
This is the line which I got to work, thanks a lot guys. One of the problems seemed to be how I executed the script, first had scriptNAme = [] ExecVM.., now I use call { ... };

Note: transformed it to SQS to see if the code would work in that format, sorry for me not completing the loop. It's just that a loop wouldn't help me getting this line to stop producing errors  :-[

EDIT: hmm, at least I hope it works not sure.. Works.
« Last Edit: 27 Jul 2007, 12:42:58 by Cheetah »
Like missions? Help with Beta Testing! or take a look at the OFPEC Missions Depot for reviewed missions!