Jump to content
Sign in to follow this  
MrRightclick

Spawning an ammo box in to a random town

Recommended Posts

Hello!

I've just gotten myself in to Arma 2 scripting, and I'm facing some problems with getting locations of towns and teleporting stuff around. What I'm trying to do is I've placed an ammobox in my map called "ammo1", and the idea of this script-training mission is to talk to a guy who tells you the location of this ammo box by telling you the name of the nearest town/city/village. The script would be called from the ammo box init field.

So far what's working is the dude telling you the location of the handplaced ammo box, so I'd like it to be so that when the mission is loaded on a server (this will be later used on an online map on a dedicated server), the ammo box gets placed in to a random town in Chernarus.

I can't really figure out how to select a random town and make the crate move there. I know the getPos and setPos commands to move the crate, but the actual select-random-town command I can't figure out. Also some randomization in the spawn location would be nice, so the box doesn't just get spawned right in the middle of the town.

I've tried doing this through markers placed on top of towns, placing them in an array and then choosing a random one from there, getting it's position and then teleporting my character there just to try the script, but all it does is teleport my character to the ocean every time.

TL;DR:

How to get a random city/town/village out of all Chernarus towns and move a handplaced crate there, possibly randomizing the location it's placed in by let's say by 100 meters? Choosing a random marker for spawn is also fine.

Thanks in advance!

Share this post


Link to post
Share on other sites

I actually got this working once I figured out getNearestLocations just returns an array.

if (isServer) then {
_box = _this select 0;

//Get an array of all the towns
_townlist = nearestLocations [getpos _box, ["NameCityCapital","NameCity","NameVillage"],10000];

//Get random town from array
_rnd = floor random (count _townlist);
_town = _townlist select _rnd;
_town_name = text (_town);

//Get town location
_locationPos = locationPosition (_town);

//Move ammobox to town location
_box setPosATL _locationPos;
};

The problem I'm having now is making the box stay on ground level, since it seems to get teleported underground every time I spawn it in.. :(

Is there a command to make it appear on ground level? :butbut:

Share this post


Link to post
Share on other sites

_locationPos = locationPosition (_town); seems to return the sea level

you just need to set it to zero

if (isServer) then {
_box = _this select 0;

//Get an array of all the towns
_townlist = nearestLocations [getpos _box, ["NameCityCapital","NameCity","NameVillage"],10000];

//Get random town from array
_rnd = floor random (count _townlist);
_town = _townlist select _rnd;
_town_name = text (_town);

//Get town location
_locationPos = locationPosition (_town);

//Move ammobox to town location
_box setPosATL [_locationPos select 0, _locationPos select 1,0];
};

Share this post


Link to post
Share on other sites

Thanks for the reply!

I tried that out, but the box is still nowhere to be seen when I teleport to the position it's supposed to be in.

EDIT:

Well the box spawns on the ground level with this code:

if (isServer) then {
_box = _this select 0;

//Get the position of the box
_pos = getPosATL _box;

//Get an array of all the towns and select one at random
_townlist = nearestLocations [getpos _box, ["NameCityCapital","NameCity","NameVillage"],10000];

//Get town from array
_rnd = floor random (count _townlist);
_town = _townlist select _rnd;
_town_name = text (_town);

//Get town location
_locationPos = locationPosition (_town);

//Move ammobox to town location
_box setPosASL [_locationPos select 0, _locationPos select 1, 0];


//Fix ammo box to ground level
_newpos = getPosASL _box;

_box setPos [_newpos select 0, _newpos select 1, 0];
};

Still need to test that in multiplayer, because here http://community.bistudio.com/wiki/setPos it says in a comment that

SetPos for static objects like a ammo crate do not work in MP.
Edited by MrRightclick

Share this post


Link to post
Share on other sites

Double posting is cancer, but anyway:

The above script worked in multiplayer just fine.

Now all that's left is to get the box out of a house if it spawns inside one.. Is there a way to check if a house is on top of it? :O

Share this post


Link to post
Share on other sites

I noticed you are mixing 3 different positioning commands. The ASL's should be used over water in most situations, like setPosASL. In your case, I would use setPosATL and getPosATL for all instances. That will assure the box is at the proper elevation.

Share this post


Link to post
Share on other sites
I noticed you are mixing 3 different positioning commands. The ASL's should be used over water in most situations, like setPosASL. In your case, I would use setPosATL and getPosATL for all instances. That will assure the box is at the proper elevation.

I actually tried the getPosATL with all of the commands, but it made no difference. Only the normal setPos command in the end made the crate appear above ground. Forgot to change them back to ATL after testing with the ASL, so that's why it's ASL in the script.. :)

Share this post


Link to post
Share on other sites

I mainly mean to be consistent in the script, because the 3 versions will return different z values depending upon where it is used. And that leads to unhappiness. :yay:

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  

×