I'm getting the Error
"_bomberGroup = |#|_bomberGroup + [_uid]; undefined variable in expression _bomberGroup"
I can't find this on the internet all the tutorials seem to just cover basic use. Here is my code, it could be completely wrong but I am new to this but getting better every day.
if (!isServer || !isDedicated) then {
waitUntil {!isNull player};
waitUntil {(getPlayerUID player) != ""};
while {true} do {
_uid = getPlayerUID player;
_bomberGroup = [];
_bomberGroup = _bomberGroup + [_uid];
};
} forEach allUnits;
if (isServer || isDedicated) then {
_rnd = floor (random (count _bomberGroup));
bomber = _bomberGroup select _rnd;
bomber addBackpack "B_AssaultPack_khk";
bomber addMagazine "DemoCharge_Remote_Mag";
};
_bombHint = "You have the bomb";
if (bomber == player) then {
hint _bombHint;
};
I'm trying to give one of the players that join at the start of the mission a backpack and a charge. I had this code kinda working but it seemed like more than one player would get the bomb sometimes. I think this way would prevent that. thanks for any help
Do you call this script in a single file or is it split into two?
First the errors I can see :
forEach allUnits misses its opening "{"
There is an infinite loop (while{true}) which, in this case, is not what you'd like I guess.
Even if this loop weren't infinite you'd be setting (_bomberGroup = [];) to an empty array each times, you'd better do it only once (before entering the loop).
Now I can think of an easier way to do that just see if it suits your style :)
What I would do is to give a name (in the editor) to each unit that might be a bomber.
Then you'd simply select one of them and give him the package.
Could look like that:
I need more details to help you(when/where/who do you call this script?)
Is it 2 separated script files? It's the complete code?
_bomberGroup = [];
{//u missed this one
if (!isServer || !isDedicated) then {
waitUntil {!isNull player};
waitUntil {(getPlayerUID player) != ""};
while {true} do {//infinite loop???
_uid = getPlayerUID player;
// _bomberGroup = [] scope local in the loop
_bomberGroup set [ count _bomberGroup, [_uid]];
//_bomberGroup = _bomberGroup + [_uid];
//use set instead of operator "+" _bomberGroup set [ count _bomberGroup, [_uid]];
};
} forEach allUnits;
if (isServer || isDedicated) then {//server exec
_rnd = floor (random (count _bomberGroup));
bomber = _bomberGroup select _rnd;//bomber is a global variable?
bomber addBackpack "B_AssaultPack_khk";
bomber addMagazine "DemoCharge_Remote_Mag";
};
_bombHint = "You have the bomb";//i'm not sure but the hint won't be displayed for the player but check
if (bomber == player) then {
hint _bombHint;
};
Be careful with the scopes of the variables(see as below, the link)
BIS: Scope
If a local variable is initialized within a Control Structures (i.e. if, for, switch, while) its scope will stay within this structure (i.e. outside of the structure it will still be seen as undefined). This does not apply to global or public variables. if (alive player) then {_living=true}; hint format["%1",_living]; Returns "scalar bool array string 0xe0ffffef", since the local variable was not initialized before being used within a control structure.
_dead=true; if (alive player) then {_dead=false}; hint format["%1",_dead]; Returns "false", since the variable was initialized before the if...then.
To initialize local variables, so that they are available throughout the whole script (including any control structures), either initialize it via the private command (e.g. private ["_varname"];), or by assigning a default value to it (e.g. varname=0;).
If knowledge can create problems, it's not through ignorance that we can solve them.
#Skippy :
Do you call this script in a single file or is it split into two?
First the errors I can see :
forEach allUnits misses its opening "{"
There is an infinite loop (while{true}) which, in this case, is not what you'd like I guess.
Even if this loop weren't infinite you'd be setting (_bomberGroup = [];) to an empty array each times, you'd better do it only once (before entering the loop).
Now I can think of an easier way to do that just see if it suits your style :)
What I would do is to give a name (in the editor) to each unit that might be a bomber.
Then you'd simply select one of them and give him the package.
Could look like that:
Where myBomberi are the names you gave to your bombers. Of course you'd have to make sure to call this only once (call it only on the server maybe).
I can't use already named units because this is a multiplayer mission and it needs to be able to work rather you have 2 players or full server.
Added 1 minute later:
#Br. :
I need more details to help you(when/where/who do you call this script?)
Is it 2 separated script files? It's the complete code?
_bomberGroup = [];
{//u missed this one
if (!isServer || !isDedicated) then {
waitUntil {!isNull player};
waitUntil {(getPlayerUID player) != ""};
while {true} do {//infinite loop???
_uid = getPlayerUID player;
// _bomberGroup = [] scope local in the loop
_bomberGroup set [ count _bomberGroup, [_uid]];
//_bomberGroup = _bomberGroup + [_uid];
//use set instead of operator "+" _bomberGroup set [ count _bomberGroup, [_uid]];
};
} forEach allUnits;
if (isServer || isDedicated) then {//server exec
_rnd = floor (random (count _bomberGroup));
bomber = _bomberGroup select _rnd;//bomber is a global variable?
bomber addBackpack "B_AssaultPack_khk";
bomber addMagazine "DemoCharge_Remote_Mag";
};
_bombHint = "You have the bomb";//i'm not sure but the hint won't be displayed for the player but check
if (bomber == player) then {
hint _bombHint;
};
I think my method does work, see:
First make an array of all possible units that might be bombers, we'll call it masterBomber.
Then check which units are actually in game with objNull. Could look like this:
_masterBomber = playableunits;// mp only, Return a list of playable units (occupied by both AI or players).
_bomberGroup = [];
{
if(isplayer _x) then {
_bomberGroup set [count _bomberGroup, [_x]];
};
}forEach _masterBomber;
Ok, I'll read up on the "set" to see what the differnce is. As for my for loop, I know c and c++ so its natural for me to use script that is similar but if it causes performance issues i'll break that habbit. Ive used mine in the past and peinted the outcome to the rpt to see that it does work. Thanks for all the help, ill try your method tomorrow when I can. I did notice what I thought of today was similar to your last. I didnt see the 2 different arrays when I first looked at it
Just read that link. Thanks it is very helpfull, guess if im going to learn something I should learn the right way.
But with that for loop does it change _i from -1 to 0 before it runs through the first time and does it run atleast once even if the condition is already false like for loops in c++? For exsample if _i was already 12 it would still run it once
This post was edited by pAXton (2013-06-18 02:48, ago)
if(isServer) then {
_bomberGroup = [civ,civ_1,civ_2,civ_3,civ_4,civ_5,civ_6,civ_7,civ_8,civ_9];
_playerBomberGroup = [];
_bombHint = "You have the bomb";
waitUntil {(getPlayerUID player) != ""};
for "_i" from 0 to 10 step 1 do {if ( _bomberGroup select _i == player) then {
_bombuild = _bomberGroup select _i;
_playerBomberGroup set [_i, _bombuild];};
diag_log text format ["ReturnedCounter: %1", _i]; //print the where the counter is at to rpt
diag_log text format ["ReturnedUnitAdded: %1", _bombuild]; //print the units that were added to rpt
};
_rnd = floor (random( count (_playerBomberGroup)));
_bomber = _playerBomberGroup select _rnd;
diag_log text format ["ReturnedRandomInt: %1", _rnd]; //print the generated random number to rpt
diag_log text format ["ReturnedBomber: %1", _bomber]; //print the generated bomber unit to rpt
_bomber addBackpack "B_AssaultPack_khk";
_bomber addMagazine "DemoCharge_Remote_Mag";
if (_bomber == player) then {
hint _bombHint;
};
};
RPT file
ReturnedCounter: 0
ReturnedUnitAdded: any
Error in expression <g text format ["ReturnedUnitAdded: %1", _bombuild];
};
_rnd = floor (random( c>
Error position: <_bombuild];
};
_rnd = floor (random( c>
Error Undefined variable in expression: _bombuild
File mpmissions\RandomBombingV0%2e11.Stratis\SuicideBomber.sqf, line 14
ReturnedCounter: 1
ReturnedUnitAdded: any
Error in expression <g text format ["ReturnedUnitAdded: %1", _bombuild];
};
_rnd = floor (random( c>
Error position: <_bombuild];
};
_rnd = floor (random( c>
Error Undefined variable in expression: _bombuild
File mpmissions\RandomBombingV0%2e11.Stratis\SuicideBomber.sqf, line 14
ReturnedCounter: 2
ReturnedUnitAdded: any
Error in expression <g text format ["ReturnedUnitAdded: %1", _bombuild];
};
_rnd = floor (random( c>
Error position: <_bombuild];
};
_rnd = floor (random( c>
Error Undefined variable in expression: _bombuild
File mpmissions\RandomBombingV0%2e11.Stratis\SuicideBomber.sqf, line 14
ReturnedCounter: 3
ReturnedUnitAdded: any
Error in expression <g text format ["ReturnedUnitAdded: %1", _bombuild];
};
_rnd = floor (random( c>
Error position: <_bombuild];
};
_rnd = floor (random( c>
Error Undefined variable in expression: _bombuild
File mpmissions\RandomBombingV0%2e11.Stratis\SuicideBomber.sqf, line 14
ReturnedCounter: 4
ReturnedUnitAdded: any
Error in expression <g text format ["ReturnedUnitAdded: %1", _bombuild];
};
_rnd = floor (random( c>
Error position: <_bombuild];
};
_rnd = floor (random( c>
Error Undefined variable in expression: _bombuild
File mpmissions\RandomBombingV0%2e11.Stratis\SuicideBomber.sqf, line 14
ReturnedCounter: 5
ReturnedUnitAdded: any
Error in expression <g text format ["ReturnedUnitAdded: %1", _bombuild];
};
_rnd = floor (random( c>
Error position: <_bombuild];
};
_rnd = floor (random( c>
Error Undefined variable in expression: _bombuild
File mpmissions\RandomBombingV0%2e11.Stratis\SuicideBomber.sqf, line 14
ReturnedCounter: 6
ReturnedUnitAdded: any
Error in expression <g text format ["ReturnedUnitAdded: %1", _bombuild];
};
_rnd = floor (random( c>
Error position: <_bombuild];
};
_rnd = floor (random( c>
Error Undefined variable in expression: _bombuild
File mpmissions\RandomBombingV0%2e11.Stratis\SuicideBomber.sqf, line 14
ReturnedCounter: 7
ReturnedUnitAdded: any
Error in expression <g text format ["ReturnedUnitAdded: %1", _bombuild];
};
_rnd = floor (random( c>
Error position: <_bombuild];
};
_rnd = floor (random( c>
Error Undefined variable in expression: _bombuild
File mpmissions\RandomBombingV0%2e11.Stratis\SuicideBomber.sqf, line 14
ReturnedCounter: 8
ReturnedUnitAdded: any
Error in expression <g text format ["ReturnedUnitAdded: %1", _bombuild];
};
_rnd = floor (random( c>
Error position: <_bombuild];
};
_rnd = floor (random( c>
Error Undefined variable in expression: _bombuild
File mpmissions\RandomBombingV0%2e11.Stratis\SuicideBomber.sqf, line 14
ReturnedCounter: 9
ReturnedUnitAdded: any
Error in expression <g text format ["ReturnedUnitAdded: %1", _bombuild];
};
_rnd = floor (random( c>
Error position: <_bombuild];
};
_rnd = floor (random( c>
Error Undefined variable in expression: _bombuild
File mpmissions\RandomBombingV0%2e11.Stratis\SuicideBomber.sqf, line 14
ReturnedCounter: 10
ReturnedUnitAdded: any
Error in expression <g text format ["ReturnedUnitAdded: %1", _bombuild];
};
_rnd = floor (random( c>
Error position: <_bombuild];
};
_rnd = floor (random( c>
Error Undefined variable in expression: _bombuild
File mpmissions\RandomBombingV0%2e11.Stratis\SuicideBomber.sqf, line 14
ReturnedRandomInt: 0
ReturnedBomber: <null>
Error in expression <_log text format ["ReturnedBomber: %1", _bomber];
_bomber addBackpack "B_Assaul>
Error position: <_bomber];
_bomber addBackpack "B_Assaul>
Error Undefined variable in expression: _bomber
File mpmissions\RandomBombingV0%2e11.Stratis\SuicideBomber.sqf, line 22
I don't understand what i'm doing wrong at this point
I found a major mistake i made...
if you noticed I added unit to the array and assigned its index _playerBomberGroup set [_i, _bombuild];};
then when picking the random unit from that array based on the size of the array
so if only 1 unit was added to the array but it was on the 3rd loop because i was starting mission in a couple slots down to see if i could see if the error was created by the player or the start of the loop
the _rnd would only be index of 0, while i was added in the array at index 2
which is why _bomber was null
This post was edited by pAXton (2013-06-21 05:08, ago)