Jump to content
HonzaVinCZ

[Need your help] Understanding variables

Recommended Posts

Hello guys, I need your help with variables in this script:

#define __dbug__
_posATL_1 = _this select 0;
_posATL_2 = _this select 1;
_posATL_3 = _this select 2;
_posATL = selectRandom [_posATL_1, _posATL_2, _posATL_3];
_wCache = "CUP_BOX_TK_MILITIA_Wps_F" createVehicle [0,0,0];
_wCache allowDamage false;
_wCache enableSimulation false;
_wCache setDir (random 359);
_wCache setPosATL _posATL;
_wCache enableSimulation true;
_wCache enableDynamicSimulation true;
_wCache allowDamage true;
_pos = getPos _wCache;
_loc = text nearestLocation [_pos, ""];
tskCrt = {
	_taskwcache = [west, ['wcachetask_'], [format ['Destroy enemy weapon cache near %1 found by our forces', _loc], format ['Destroy Weapon Cache near %1', _loc], ''], _wCache, 'CREATED', -1, true, 'destroy', true] call BIS_fnc_taskCreate;
	deleteVehicle _trig;
};
_trig = createTrigger ["EmptyDetector", _pos];
_trig setTriggerArea [10,10,0,FALSE,10];
_trig setTriggerActivation ["ANYPLAYER","PRESENT",false];
_trig setTriggerTimeout [0,0,0,false];
_trig setTriggerStatements[
	"this",
	"call tskCrt",
	""];

#ifdef __dbug__
cacheMkrs=[];
_mkrID=format["wCache_%1",_loc];
_mkr=createMarker[_mkrID,_pos];
_mkr setMarkerShape"ICON";_mkr setMarkerType"mil_dot";_mkr setMarkerBrush"Solid";_mkr setMarkerAlpha 1;_mkr setMarkerColor"ColorEast";_mkr setMarkerText format ["wCache %1", _loc];
cacheMkrs pushBack _mkr;
#endif

_exists = [_taskwcache] call BIS_fnc_taskExists;
waitUntil {sleep 1; !alive _wCache};

	if (_exists) then {
  		[[_taskwcache],"SUCCEEDED"] call BIS_fnc_taskSetState;
  		"Bo_GBU12_LGB" createVehicle getPos _wCache;
	} else {
  		"Bo_GBU12_LGB" createVehicle getPos _wCache;
  		deleteVehicle _trig;
	};

Basically when player enters the trigger, it creates a task but variables I set on the beginning of the script doesn't work in the task creation line - the place in the desc and title shows as "any". Also when I want to check if task exists in the end of the script, it says _taskwcache variable doesn't exist. What do I do wrong please? Thank you.

Share this post


Link to post
Share on other sites

_taskwcache variable is inside tskCrt so it wont exist in the root scope. And when you call tskCrt the _taskwcache is local variable and doesn't exist anywhere else

to fix you should use global variables or something

  • Like 2

Share this post


Link to post
Share on other sites
7 minutes ago, gc8 said:

_taskwcache variable is inside tskCrt so it wont exist in the root scope. And when you call tskCrt the _taskwcache is local variable and doesn't exist anywhere else

to fix you should use global variables or something

That is what I thought. Can I ask you if you could show me example of that on my script please? I have really no idea how could I use publicVariable in this. :down:

  • Like 1

Share this post


Link to post
Share on other sites
10 minutes ago, HonzaVinCZ said:

That is what I thought. Can I ask you if you could show me example of that on my script please? I have really no idea how could I use publicVariable in this. :down:

 

no need to use publicvariable on this, that's multiplayer stuff

well the first thing to advice you is not to try get the return value of BIS_fnc_taskCreate because it only returns boolean

instead define the task ID as the second parameter like you seem to have done already. Then use the ID in:

 

_exists = ["wcachetask_"] call BIS_fnc_taskExists;

 

(not 100% sure of the syntax)

 

so you just refer to the task with the task ID (no need for variables...)

 

will check this thread again later

  • Like 2

Share this post


Link to post
Share on other sites
1 hour ago, gc8 said:

 

no need to use publicvariable on this, that's multiplayer stuff

well the first thing to advice you is not to try get the return value of BIS_fnc_taskCreate because it only returns boolean

instead define the task ID as the second parameter like you seem to have done already. Then use the ID in:

 

_exists = ["wcachetask_"] call BIS_fnc_taskExists;

 

(not 100% sure of the syntax)

 

so you just refer to the task with the task ID (no need for variables...)

 

will check this thread again later

_exists = ["wcachetask_"] call BIS_fnc_taskExists;
waitUntil {sleep 1; !alive _wCache};
	if (_exists) then {
  		[["wcachetask_"],"SUCCEEDED"] call BIS_fnc_taskSetState;
  		"Bo_GBU12_LGB" createVehicle getPos _wCache;
  		deleteVehicle _wCache;
	} else {
  		"Bo_GBU12_LGB" createVehicle getPos _wCache;
  		deleteVehicle _trig;
  		deleteVehicle _wCache;
	};

Sadly, it doesn't work. Shows no error but also the mission doesn't get accomplished as it should.

 

1 hour ago, gc8 said:

will check this thread again later

ok, thank you.
I'll also need some explain how to "import" the variables to that tskCrt, because I really need them there. Same with the task ID. 
 

  • Like 1

Share this post


Link to post
Share on other sites
13 hours ago, HonzaVinCZ said:

I'll also need some explain how to "import" the variables to that tskCrt, because I really need them there.

 

use global variables. example:

 

put this at the beginning of the script so that the variable is "defined"

myGlobalVar = 777;

 

then in tskCrt:

 

hint format["var is %1", myGlobalVar ];

 

13 hours ago, HonzaVinCZ said:

Same with the task ID. 

 

 

Task Id is just a string or array with strings (array supposed to have two strings where the last one is parent task ID) so you don't need to import that. Just pass your task ID string to the task functions

 

 

  • Like 2

Share this post


Link to post
Share on other sites
1 hour ago, gc8 said:

 

use global variables. example:

 

put this at the beginning of the script so that the variable is "defined"


myGlobalVar = 777;

 

then in tskCrt:

 


hint format["var is %1", myGlobalVar ];

 

 

 

Task Id is just a string or array with strings (array supposed to have two strings where the last one is parent task ID) so you don't need to import that. Just pass your task ID string to the task functions

 

 


Sorry, I still have massive problems with variables here. Suffering with it over three hours today already and can't make it work. I do something wrong, I know, but I can't make it working, I just don't know how. I need variable for each weapon cache. Each weapon cache needs its position, task, marker and location name but it doesn't work the way I try all time. I changed the way weapon caches are recognized, I placed them on the map and used this to call the script for them: 

20201015114511-1.jpg

 

this is how it looks like on the map... tasks are creating at one same place and it seems like they get accomplished randomly when I destroy any of the caches.

20201015114029-1.jpg

 

actual script looks like this:
 

// Lythium weapon caches script by HonzaVinCZ
#define __dbug__
_wCache = _this select 0;
wCache = _wCache;
_posATL_1 = _this select 1;
_posATL_2 = _this select 2;
_posATL_3 = _this select 3;
_posATL = selectRandom [_posATL_1, _posATL_2, _posATL_3];
_wCache setDir (random 359);
_wCache setPosATL _posATL;
_pos = getPos _wCache;
_loc = text nearestLocation [_pos, ""];
loc = _loc;
tskCrt ={
	taskwcache = [west, [format["task_%1",(random 1000)]], [format ['Destroy enemy weapon cache near %1', loc], format ['Destroy Weapon Cache near %1', loc], ''], wCache, 'CREATED', -1, true, 'destroy', true] call BIS_fnc_taskCreate;deleteVehicle thisTrigger};
_trig = createTrigger ["EmptyDetector", _pos];
_trig setTriggerArea [10,10,0,FALSE,10];
_trig setTriggerActivation ["ANYPLAYER","PRESENT",false];
_trig setTriggerTimeout [0,0,0,false];
_trig setTriggerStatements[
	"this",
	"call tskCrt",
	""];
#ifdef __dbug__
_mkrID=format["wCache_%1",_pos];
_mkr=createMarker[_mkrID,_pos];
_mkr setMarkerShape"ICON";_mkr setMarkerType"mil_dot";_mkr setMarkerBrush"Solid";_mkr setMarkerAlpha 1;_mkr setMarkerColor"ColorEast";_mkr setMarkerText format ["wCache %1", _loc];
#endif
_wCache enableDynamicSimulation true;
waitUntil{sleep 1; !alive _wCache};
	if (true) then {
  		[taskwcache,"SUCCEEDED"] call BIS_fnc_taskSetState;
  		"Bo_GBU12_LGB" createVehicle _pos;
  		deleteVehicle _wCache;
	} else {
  		"Bo_GBU12_LGB" createVehicle _pos;
  		deleteVehicle _trig;
  		deleteVehicle _wCache;
	};

 

Share this post


Link to post
Share on other sites
// Lythium weapon caches script by HonzaVinCZ
#define __dbug__
_wCache = _this select 0;
wCache = _wCache;
_posATL_1 = _this select 1;
_posATL_2 = _this select 2;
_posATL_3 = _this select 3;
_posATL = selectRandom [_posATL_1, _posATL_2, _posATL_3];
_wCache setDir (random 359);
_wCache setPosATL _posATL;
_pos = getPos _wCache;
_loc = text nearestLocation [_pos, ""];
loc = _loc;

_taskID = format["task_%1",(random 1000)]; // Create task ID

tskCrt ={
_taskID = thisTrigger getVariable 'taskId'; // Get the task ID
_wCache = thisTrigger getVariable 'wCache';

[west, _taskID, [format ['Destroy enemy weapon cache near %1', loc], format ['Destroy Weapon Cache near %1', loc], ''], _wCache, 'CREATED', -1, true,'destroy', true] call BIS_fnc_taskCreate;

deleteVehicle thisTrigger;
};

_trig = createTrigger ["EmptyDetector", _pos];
_trig setTriggerArea [10,10,0,FALSE,10];
_trig setTriggerActivation ["ANYPLAYER","PRESENT",false];
_trig setTriggerTimeout [0,0,0,false];
_trig setTriggerStatements[
	"this",
	"call tskCrt",
	""];

_trig setVariable ["taskId", _taskID]; // Save taskID so trigger code can use it
_trig setVariable ["wCache", _wCache]; // Save weaponcache too

#ifdef __dbug__
_mkrID=format["wCache_%1",_pos];
_mkr=createMarker[_mkrID,_pos];
_mkr setMarkerShape"ICON";_mkr setMarkerType"mil_dot";_mkr setMarkerBrush"Solid";_mkr setMarkerAlpha 1;_mkr setMarkerColor"ColorEast";_mkr setMarkerText format ["wCache %1", _loc];
#endif

_wCache enableDynamicSimulation true;
waitUntil{sleep 1; !alive _wCache};
	if (true) then {
  		[_taskID,"SUCCEEDED"] call BIS_fnc_taskSetState;
  		"Bo_GBU12_LGB" createVehicle _pos;
  		deleteVehicle _wCache;
	} else {
  		"Bo_GBU12_LGB" createVehicle _pos;
  		deleteVehicle _trig;
  		deleteVehicle _wCache;
	};

something like this.. code not tested

Edited by gc8
_wCache
  • Thanks 1

Share this post


Link to post
Share on other sites
7 minutes ago, gc8 said:

something like this.. code not tested

just tested it, tasks doesn't get created.

Share this post


Link to post
Share on other sites
3 minutes ago, HonzaVinCZ said:

just tested it, tasks doesn't get created.

 

no error messages? you could put hint as the first thing in tskCrt to make sure it triggers...

  • Like 2

Share this post


Link to post
Share on other sites

modified the code a bit, _wCache was not properly set

  • Like 2

Share this post


Link to post
Share on other sites
9 minutes ago, gc8 said:

modified the code a bit, _wCache was not properly set

You're right, now the mission works perfectly! Thank you. :respekt: Now I only need to fix the location names doesn't show properly in task desc, title and debug markers. Simliar way as you did here?

  • Like 1

Share this post


Link to post
Share on other sites
1 minute ago, HonzaVinCZ said:

You're right, now the mission works perfectly! Thank you. :respekt: Now I only need to fix the location names doesn't show properly in task desc, title and debug markers. Simliar way as you did here?

 

yes save the _loc to variable like the others are and then get

 

glad to hear it works!

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
9 minutes ago, gc8 said:

 

yes save the _loc to variable like the others are and then get

 

glad to hear it works!

Added _loc same way as you did and it works!... but still some places seem like they don't recognize location. Any idea why?


idk, code below seems good to me but it isn't for some reason.

_loc = text nearestLocation [_pos, ""]; 

 

20201015123949-1.jpg

Share this post


Link to post
Share on other sites

maybe some locations dont have a name, IDK. try with:

 

_loc = text nearestLocation [_pos, "nameCity"];

 

 

  • Like 2

Share this post


Link to post
Share on other sites
16 minutes ago, gc8 said:

maybe some locations dont have a name, IDK. try with:

 

_loc = text nearestLocation [_pos, "nameCity"];

 

 

Tried "nameCity", "nameVillage", "name" and it shows just one name, for ex. Arobster on all markers 😕 

Share this post


Link to post
Share on other sites

try this old function of mine that I dug up from my project:

 

getNearestLocation =
{
private ["_pos", "_loc", "_nearLocs"];
_pos = _this;

_loc = locationNull;
// ignore these since they dont have name:  ["Hill"]
_nearLocs = nearestLocations [_pos, ["Airport","NameCityCapital","nameCity","NameVillage","NameLocal"], 90000000];
if(count _nearLocs > 0) then
{
_loc = _nearLocs select 0;
};

_loc
};

 

Usage:

 

_loc = text (_pos call getNearestLocation);

 

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
4 minutes ago, gc8 said:

try this old function of mine that I dug up from my project:

 


getNearestLocation =
{
private ["_pos", "_loc", "_nearLocs"];
_pos = _this;

_loc = locationNull;
// ignore these since they dont have name:  ["Hill"]
_nearLocs = nearestLocations [_pos, ["Airport","NameCityCapital","nameCity","NameVillage","NameLocal"], 90000000];
if(count _nearLocs > 0) then
{
_loc = _nearLocs select 0;
};

_loc
};

 

Usage:

 


_loc = text (_pos call getNearestLocation);

 

Woah, now this works! Thank you very much for all your help! 

  • Like 2

Share this post


Link to post
Share on other sites

 

@gc8 I found one more thing I need to ask you or someone else about... This code creates crater when IED explodes but when there is more than one crater, it always deletes just one instead of each of them after 60s. The idea is each crater gets deleted after given time, not all of them at one time. It has to do something with the variables again and I do something wrong again too. The 0.5 sec sleep is to delay the hideObject true because otherwise the explosion gets blocked by the object and it doesn't damage anything. Then the 60 sec delay is for crater deletion, by default it'll be about 15 mins. I tried to use getVar and setVar but this time I use it propably wrong way. I didn't know what to attach the var when it is in [] spawn {}; 
I have this code called in trigger, it is not all but I'm quite sure there must be the problem:


EDIT
I had typo in [] spawn {}; where I wrote crater instead _crater... Anyway it leaves crater on the ground even when I have this code after bomb gets spawned

{deleteVehicle _x}forEach nearestObjects[getPosATL _iedObj,["Crater"],4];


 

_crater = "Crater" createVehicle [0,0,0];
_crater setDir (random 359);
_crater enableSimulation false; 
_crater allowDamage false;
_crater hideObject true;
_crater setPos (getPos thisTrigger);
_crater1 = "Land_ShellCrater_02_small_F" createVehicle [0,0,0];
_crater1 setDir (random 359);
_crater1 enableSimulation false; 
_crater1 allowDamage false;
_crater1 hideObject true;
_crater1 setPos (getPos thisTrigger);
_crater2 = "Land_ShellCrater_02_debris_F" createVehicle [0,0,0];
_crater2 setDir (random 359);
_crater2 enableSimulation false; 
_crater2 allowDamage false;
_crater2 hideObject true;
_crater2 setPos (getPos thisTrigger);
[] spawn {_crater = missionNamespace getVariable 'crater'; _crater1 = missionNamespace getVariable 'crater1'; _crater2 = missionNamespace getVariable 'crater2'; sleep 0.5; _crater hideObject false; _crater1 hideObject false; _crater2 hideObject false; sleep 60; deleteVehicle _crater; deleteVehicle _crater1; deleteVehicle _crater2};
missionNamespace setVariable ["crater", _crater];
missionNamespace setVariable ["crater1", _crater1];
missionNamespace setVariable ["crater2", _crater2];

 

Share this post


Link to post
Share on other sites

well the first thing I notice is that spawn should be last. because with this order setVariable has not yet set the variable when spawn happens and so it wont work

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
7 minutes ago, gc8 said:

well the first thing I notice is that spawn should be last. because with this order setVariable has not yet set the variable when spawn happens and so it wont work

ok, gonna test it

EDIT:
Yeah, this works fine, thank you. But as I edited above, the crater mark on the ground doesn't get deleted, any clue why? 

Share this post


Link to post
Share on other sites

Don't know but there's 60 sec delay

Share this post


Link to post
Share on other sites
3 minutes ago, gc8 said:

Don't know but there's 60 sec delay

Sure but it doesn't get deleted with the crater model. The crater mark is still on the ground when the crater gets deleted and the mark from the explosion of the IED should not be even there because just after it explodes, there is code below and after that the whole crater thing I want is created.

{deleteVehicle _x}forEach nearestObjects[getPosATL _iedObj,["Crater"],4];

Some of the crater markers is still there but idk which one. If that from IED of that I spawned by the code.
 

Share this post


Link to post
Share on other sites

i would check if nearestObjects returns list of the craters or is it empty

Share this post


Link to post
Share on other sites
47 minutes ago, gc8 said:

i would check if nearestObjects returns list of the craters or is it empty

Hm, it doesn't detect the crater mark created by the explosion. It detects craters placed by the script but for some reason it can't detect crater mark created by the explosion itself. Tested it with hint.

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

×