Home   Help Search Login Register  

Author Topic: Problem with setvariable for a non-named unit  (Read 3515 times)

0 Members and 1 Guest are viewing this topic.

Offline Mad_Cheese

  • Members
  • *
Problem with setvariable for a non-named unit
« on: 06 Dec 2012, 23:18:16 »
Does someone have an idea why this only works if a unit has a name?

Code: [Select]
_man = _this select 0;
_marker = _this select 1;
_move = {
_man = leader _group;
_WPos = [([_marker] call SHK_pos), 0];
_WP = _group addWaypoint _WPos;
_WP setWaypointType "Move";
_WP setWaypointbehaviour "careless";
_WP setWaypointspeed "limited";
_WP setWaypointStatements ["true", format ["%1 setvariable ['MC_COMPLETE',true,true];",_man]];
};

does anyone have a better solution for this? the "setwaypointstatements" causes a script error (reported as "missing ;") if the unit has no global name... if i call it ie "John", it works fine. My guess it's because a unit without a name returns a sequence of characters, but I don't understand because I can normally set variables to a local name like "_man" without a problem..
« Last Edit: 06 Dec 2012, 23:23:45 by Mad_Cheese »

Offline bardosy

  • Honoured Contributor
  • ***
  • campaign designer
    • CartooDiv
Re: Problem with setvariable for a non-named unit
« Reply #1 on: 10 Dec 2012, 09:23:16 »
When you use _men setvariable [ is very different if you use it in format[. So you are right, this is the problem, what you guess.

My advise if _man is dynamically generated and this is why you didn't name him, to name him dynamically.
Create a global namecounter and then use it to name these _men-s.


Code: [Select]
_man setVehicleVarName format["manname%1",SHK_globalnamecnt];
SHK_globalnamecnt=SHK_globalnamecnt+1;

Of course you have to inicialize the namecounter in the init.sqf:

Code: [Select]
SHK_globalnamecnt = 0;
Fix bayonet!

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Problem with setvariable for a non-named unit
« Reply #2 on: 10 Dec 2012, 20:29:38 »
Are you calling the _move part as a function later on, or why you have set it up like that?

One cause for the problem might be that when you use _move={}; it's a code block that exists in it's own scope and you do not pass the variable _group in it, which is local to the scope it was defined in and thus isn't available for other scopes. Which makes leader _group output something garbled, making the variable _man garbled..
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline Mad_Cheese

  • Members
  • *
Re: Problem with setvariable for a non-named unit
« Reply #3 on: 16 Feb 2013, 22:30:52 »
Hey guys,

sorry I had to take a big editing  break but thanks a lot for the help! I still can't get it to work unfortunately.

@h-
I should have pasted the entire script. Yes, I am calling the _move part as a function, the idea is just to generate waypoints via the waypointstatements so it becomes an infinite loop. I'm not sure that I'm doing things correctly, I know there's other scripts for this but I just really want to learn.

@Bardosy
The concept sounds great, I assigned the varnames but they don't seem to work with my commands. For example, I experimented with this:

Code: [Select]
player setVehicleVarName format["man%1",MC_globalnamecnt];
MC_globalnamecnt = MC_globalnamecnt+1;
hint format ["%1",player];

The hint returned "Man0" correctly, nothing happened when I tried something like
Code: [Select]
removeallweapons man0;

MC_globalnamecnt is off course defined in the init.sqf

Anyways, here is the entire script
Code: [Select]
if (!IsServer) exitwith {};
waituntil {!isnil "bis_fnc_init"};
private ["_man","_marker","_group","_WPos","_WP","_move","_formation"];

_unit = _this select 0;
_marker = _this select 1;
_formation = _this select 2;

_man = nul;
if ( !(_unit iskindof "man")) then {_man = effectivecommander _unit;} else {_man = _unit;};

_man setVehicleVarName format["man%1",MC_globalnamecnt];
MC_globalnamecnt = MC_globalnamecnt+1;
_man = vehiclevarname _man;
//hint format ["%1",_man];


sleep (2 + (random 2));
_man setvariable ["MC_COMPLETE",false,true];
_group = group _man;
_group setformation _formation;

_move = {
_WPos = [([_marker] call SHK_pos), 0];
_WP = _group addWaypoint _WPos;
_WP setWaypointType "Move";
_WP setWaypointbehaviour "careless";
_WP setWaypointspeed "limited";
_WP setWaypointStatements ["true", format ["%1 setvariable ['MC_COMPLETE',true,true];",_man]];
};

[] call _move;

while {{alive _x} count units _group > 0} do {
if (  _man getvariable "MC_COMPLETE") then {
deletewaypoint _WP;
_man setvariable ["MC_COMPLETE",false,true];
[] call _move;
};
sleep 15 + (random 15);
};


Some of that stuff may not make a lot of sense to the experienced scripters, please bear with me :)

The line "_man setvariable ["MC_COMPLETE",false,true];" is what causes a script error.

Offline F2kSel

  • Members
  • *
Re: Problem with setvariable for a non-named unit
« Reply #4 on: 17 Feb 2013, 03:08:22 »
_WP setWaypointStatements ["true", format ["%1 setvariable ['MC_COMPLETE',true,true];",_man]];

That can't be right  you can't use local variables _man    and the whole statement needs to be within ""
format and _man are outside the brackets at least that was my understanding format may be a special case.

Also where is _group being set.


maybe this would work

Code: [Select]
_man = _this select 0;
_marker = _this select 1;

move = {
//_man = leader _group;
_WPos = [([_marker] call SHK_pos), 0];
_WP = group _man addWaypoint [_WPos,0];
_WP setWaypointType "Move";
_WP setWaypointbehaviour "careless";
_WP setWaypointspeed "limited";

_WP setWaypointStatements ["true"," this setvariable ['MC_COMPLETE',true,true];"];
};

I tested a version of it and when any group member got to the waypoint  the variable was set to true and the trigger activated

cond
Code: [Select]
player getvariable "MC_COMPLETE"
onact  
Code: [Select]
hint "complete"
« Last Edit: 17 Feb 2013, 03:41:33 by F2kSel »

Offline Mad_Cheese

  • Members
  • *
Re: Problem with setvariable for a non-named unit
« Reply #5 on: 16 Mar 2013, 21:20:05 »
Thanks F2kSel, that did the trick! I didn't know you could use "this" in a defined waypoint's statement :)

I successfully used this weird format-style in setwaypointstatements before, really can't figure out what the difference is here.. but that's not really important, because it works now! :D

thanks!