PDA

View Full Version : general scripting help



Mongoose_84
Dec 19 2006, 10:26
i'm currently working on a way to make respawning in MP work the way i want it to. i've been wondering for quite some time now, why some things won't work, that i thought were done right. now i realised that i can't just write down code the way i'm used to (from programming in C), but have to follow some strange rules (at least strange to me). i want to post what i've done so far and would really appreciate, if someone could explain to me, what i'm doing wrong, why it is wrong and how i do it right. (btw, even if all of the code already worked, it wouldn't do exactly as i want - just want this to work at all, so i can finalize it)

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">
_nearest = objNull;
_distNearest = -1;

{
distX = _this distance _x;
if &#40;&#40;alive _x&#41; AND &#40;_distNearest == -1 OR distX &#60; _distNearest&#41;&#41; then
{
_nearest = _x;
_distNearest = distX;
}
} forEach units group _this;

if &#40;_distanceNearest == -1&#41; then
{
{
_this addMagazine &#39;8Rnd_9x18_Makarov&#39;
} forEach &#91;1,2,3,4,5,6,7,8&#93;;
_this addWeapon &#39;Makarov&#39;;
} else
{
_newWeapons = weapons _nearest;
_weaponsCount = count _newWeapons;
_newMagazines = magazines _nearest;
_magazinesCount = count _newMagazines;
newPosition = getPos _nearest;
newDirection = getDir _nearest;
deleteVehicle _nearest;
for &#91;{_x = 0}, {_x &#60; _magazinesCount}, {_x= _x + 1}&#93; do
{
_this addMagazine &#40;_newMagazines select _x&#41;;
};
for &#91;{_x = 0}, {_x &#60; _weaponsCount}, {_x= _x + 1}&#93; do
{
_this addWeapon &#40;_newWeapons select _x&#41;;
};
_this setPos newPosition;
_this setDir newDirection;
}
[/QUOTE]
edit: of course, "_this" is the newly respawned player (and i don&#39;t know why the empty spaces at the beginning of a line always seem to get less after copying and pasting the code...)

i&#39;m afraid there&#39;s hardly anything that does work the way it is now (especially if/then). but for some reason, i always got a Makarov and one magazine for it on respawn. so i wrote that forEach-thing into only one line and then i got all eight.
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">
{_this addMagazine &#39;8Rnd_9x18_Makarov&#39;} forEach &#91;...&#93;;
[/QUOTE]

but what about the rest?... i mean i can&#39;t be expected to squeeze all the code into a single line for it to work, can i? or does it mean, i have to say good bye to clean programming and start making excessive use of "goto"? http://forums.bistudio.com/oldsmileys/crazy_o.gif

Mongoose_84
Dec 19 2006, 13:26
just in case anyone wanted to do something similar - i got it to work now:


Quote[/b] ]
_nearest = ObjNull;
_distNearest = -1;

{_distX = (_this distance _x); if ((alive _x) AND (_x &#33;= _this) AND (_distNearest == -1 OR _distX < _distNearest)) then {_nearest = _x; _distNearest = _distX}} forEach units group _this;

if (_distNearest == -1) then {goto "newUnit"};

_newWeapons = weapons _nearest;
_weaponsCount = count _newWeapons;
_newMagazines = magazines _nearest;
_magazinesCount = count _newMagazines;
_newPosition = getPos _nearest;
_newDirection = getDir _nearest;
deleteVehicle _nearest;
for [{_x = 0}, {_x < _magazinesCount}, {_x= _x + 1}] do {_this addMagazine (_newMagazines select _x)};
for [{_x = 0}, {_x < _weaponsCount}, {_x= _x + 1}] do {_this addWeapon (_newWeapons select _x)};
_this setPos _newPosition;
_this setDir _newDirection;
exit;

#newUnit

{_this addMagazine &#39;8Rnd_9x18_Makarov&#39;} forEach [1,2,3,4,5,6,7,8];
_this addWeapon &#39;Makarov&#39;;
_this selectWeapon &#39;Makarov&#39;;

it&#39;s quite handy, because it makes you respawn into group members, while there are any left without ending the game, if there aren&#39;t. (i mean to use this in a kind of rts-style map, where your group size can change dynamically)

what&#39;s missing so far, is that it won&#39;t work, if the nearest unit is inside a vehicle.

i would also like to hear any other suggestions of how to improve this code. for example: is it possible to adept the current stance (prone, standing, kneeling) also? and is there a way to make the new unit draw it&#39;s weapons instantly (without animation)?

edit: oh and does anyone know the return value for "primaryWeapon _x" (and accordingly secondary/side arm), if it is none at all?
edit2: ok i read, that it returns "" in that case, but i couldn&#39;t find a function for the sidearm/handgun that does, what primaryWeapon/secondaryWeapon does for the other weapon slots... if anyone wants to join my monologue, just go ahead http://forums.bistudio.com/oldsmileys/icon_rolleyes.gif