Jump to content
Sign in to follow this  
sbsmac

Why won't 'createdUnits' move ?

Recommended Posts

I've written a function to dynamically spawn some soldiers near the player. I then want them to run around randomly. The problem is that no matter what I do, I can't get them to move from the spot they were created! Here's some sample code (note that 'randpos' is just a function to return a position close to the player)...

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

populate = {

for "_x" from 0 to 10 do {

_s = group player createUnit ["SoldierWB", call randPos, [], 0, "NONE"];

_s moveTo position player;

}

};

When I call populate from an action, the soldiers are created and move into formation but then refuse to move any further. If I give each of them a dedicated group (using 'createGroup west' instead of the player group, they just stand still. Other approaches such as adding waypoints or using the old 'move' command are similarly unsuccessful. Strangely, other commands do work, eg '_s setUnitPos "DOWN"' makes the units lie down.

Anyone got any ideas ? Thanks in advance.

Share this post


Link to post
Share on other sites

It definitely was never working that way -- even in OFP -- as createUnit does not return anything. You have to put<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">myGuy1 = thisin the init string.

Share this post


Link to post
Share on other sites

Raedor,

Thanks for the reply. I think CreateUnit must be returning something in ArmA though. If I execute this code for example:

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

populate = {

for "_x" from 1 to 10 do {

_g = createGroup west;

_s = _g createUnit ["SoldierWB", call randPos, [], 0, "NONE"];

if (_x > 5) then {_s setUnitPos "DOWN";};

}

};

I get ten new units standing near my player and half of them lie down, implying that _s is being correctly set to the new unit.

Note that I'm using the 'createUnit array' as described on Biki here rather than the old-style OFP createUnit. The new form doesn't actually contain an init string. confused_o.gif

*edit*

It only seems to be movement that doesn't work. Commands such as setDir and setUnitPos are all fine. 'moveto' and 'move' don't work. Trying to add 'MOVE' waypoints doesn't work. I've even tried calling 'enableAI "move"'. sad_o.gif

Share this post


Link to post
Share on other sites

I'd like to add to this question if I may? How do you name a unit? I want to create a gamelogic named "gamelogic1" on top of a crashed helo, but I don't know how to actually add a name to the unit... which is annoying.

Share this post


Link to post
Share on other sites

Editor names are nothing but global variables that hold object reference. Putting unit named "duuude" in editor and "duuude = "SoldierWB" createVehicle getPos player" result in same thing from scripting point of view.

Share this post


Link to post
Share on other sites

Is 'setIdentity' what you are looking for ?

details on Biki

I guess you would use it like this ...

_gl = createUnit ... ;

_gl setIdentity "my gamelogic" ;

*edit* As Feersum Enjinn says, you really only need this if you are determined to find your units by name-string rather than directly through reference.

Share this post


Link to post
Share on other sites
Note that I'm using the 'createUnit array' as described on Biki here rather than the old-style OFP createUnit. The new form doesn't actually contain an init string. confused_o.gif

Ah, sorry then.

@feer: There has to be a little difference, otherwise the command setVehicleVarName would be quite useless. Eg things like MyTank1D instead of driver MyTank1 are only working with editor names.

Share this post


Link to post
Share on other sites
It definitely was never working that way -- even in OFP -- as createUnit does not return anything. You have to put<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">myGuy1 = this
in the init string.

He is using the NEW ArmA command, which does return the created unit.

So he doesn't need to put "myGuy1 = this" into the init field string.

This new command is working fine, and i don't see any error in this.

edit: i overlooked Raedor's last message where he wrote the same thing as i did here, so ...nevermind this

Share this post


Link to post
Share on other sites
I've written a function to dynamically spawn some soldiers near the player. I then want them to run around randomly. The problem is that no matter what I do, I can't get them to move from the spot they were created! Here's some sample code (note that 'randpos' is just a function to return a position close to the player)...

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

populate = {

for "_x" from 0 to 10 do {

_s = group player createUnit ["SoldierWB", call randPos, [], 0, "NONE"];

_s moveTo position player;

}

};

When I call populate from an action, the soldiers are created and move into formation but then refuse to move any further. If I give each of them a dedicated group (using 'createGroup west' instead of the player group, they just stand still. Other approaches such as adding waypoints or using the old 'move' command are similarly unsuccessful. Strangely, other commands do work, eg '_s setUnitPos "DOWN"' makes the units lie down.

Anyone got any ideas ? Thanks in advance.

It might look like a nonsense, but try adding the Join command after each unit creation - like this:<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">populate = {

for "_x" from 0 to 10 do {

_s = group player createUnit ["SoldierWB", call randPos, [], 0, "NONE"];

[_s] Join group player;

_s moveTo position player;

}

};

I had some weird problems when i created a "center", and then new group for this "center".

The problems disappeared when i added the Join command after the creation of each unit.

Share this post


Link to post
Share on other sites

Hello

"CommandMove" and "DoMove" work.

If you want the units to to join a script-created group using "CreateGroup", do as 5133p39 said and add the join "command".

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">for "_x" from 0 to 10 do {

_g = createGroup west;

_s = _g createUnit ["SoldierWB", getpos tr, [], 0, "NONE"];

[_s] join _g;

_s CommandMove position tr_1;

}

Or:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">for "_x" from 0 to 10 do {

_s = group player createUnit ["SoldierWB", getpos tr, [], 0, "NONE"];

_s CommandMove position tr_1;

}

I too have experienced some weird problems with "CreateGroup": the game would freeze for instance when the units engaged enemies. Thank you 5133p39 for this very useful tip with "join".

Regards,

Igor.

Share this post


Link to post
Share on other sites

Thanks 5133p39 and Igor. Making the new unit join a group now allows them to run around using 'move' ! My civilian targets are now running around nicely in the desired 'headless chicken' manner. biggrin_o.gif

Strangely though, some commands don't work after the unit has joined !

The ones I've tried so far are:

move: only works joined units.

setDir: works with joined or unjoined units.

setUnitPos: only works with unjoined units.

When Biki is back up I'll try a few more.

Share this post


Link to post
Share on other sites

What if you wanted to create a group that randomly spawned around your player, but rather then being the same side, they would be like civilians.

How would you do that?

Share this post


Link to post
Share on other sites
What if you wanted to create a group that randomly spawned around your player, but rather then being the same side, they would be like civilians.

How would you do that?

_pos = GetPos Player;

_r = Random 50;

_cCtr = CreateCenter Civilian;

_cGrp = CreateGroup _cCtr;

_u = _cGrp CreateUnit ["Civilian", _pos, [], _r, "NONE"];

[_u] Join NPC_GRP;

Share this post


Link to post
Share on other sites

You don't need to create a "center" each time you create a unit or group, do you? It should be 1 active center per active side?

Share this post


Link to post
Share on other sites

I don't want to dredge this up without good reason, but I had just experienced this problem without having read the conclusion of this thread.

Through my experimentation, here's what I found:

My specific problem: All but the group leader where heading off to GF57, just to the right of the docks there. Units that where supposed to be patroling after being spawned where going in that direction, whatever the terrain inbetween. In some cases, they would plummet to their deaths. In some, they would go for a swim. And in some they would make it. Just depending on their starting position.

The fix, although I tried the join command, had nothing to do with that.

Here's what I did:

1: used every command I could think of to make the AI go to the group leader until I found something that worked, which I didn't... Well, it did, but then the group leader would stay put and the group would leave again.

2: Immediatly suspecting the group leader was somehow at fault, I altered all those movement oriented commands to be a different position from the group leaders (player's position). And Voila, they stayed!

3: Started trimming every command off that didn't effect the outcome one by one...

The result of that was:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">{_x doMove getpos player} forEach units _group;

4: Knowing that moving the group to the player would be impractical in 99.99% of all cases, I started experimenting with something that would be reliable, and not have to make the group move too much...

Result:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">{_x doMove [(getPos (units _group select 0) select 0)+10, (getPos (units _group select 0) select 1)+10, 0]} forEach units _group;

Then, I decided to make a forum post about my bad experience with the createGroup command and my findings, and decided to search to see if anyone else had this problem, so I found this thread then wrote this.

Unfourtunately, this bug exists in my populate island script as of version 1.0 (I can't believe I missed that...). I will be fixing it very soon though.

For those of you using that script and would like a hotfix:

In CSL_PopulateIsland_B.sqf add the following line at line number 206:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">{_x doMove [(getPos (units _group select 0) select 0)+10, (getPos (units _group select 0) select 1)+10, 0]} forEach units _group;

It should look like this:

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

// increment the counter

_unitFillCounter = _unitFillCounter + 1;

// debugging code. only enable to convince yourself it's working correctly

//player sidechat "group restored";

};

{_x doMove [(getPos (units _group select 0) select 0)+10, (getPos (units _group select 0) select 1)+10, 0]} forEach units _group;

// cleans the group when not in use anymore

[_group] execVM "CSL_Utility\CSL_RemoveGroupWhenDeadSimple.sqf";

_groupInStorage = false;

If someone would be so kind as to confirm this solution working, I'll update the creategroup command in the biki with information about this bug and the workaround.

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  

×