Jump to content
anaximandross

Local Variable in Global Space

Recommended Posts

Test with this:

if(!isServer) exitWith{};

//add a new parameter for array of markers being passed into createBasePlots
params 
[
	["_marker","",[""]], 
    ["_foodCost",99999,[0]]
];

base_num = Param [2,-1,[0]];

if (_marker == "") exitWith {diag_log format ["Debug Error (BaseClaim.sqf): Invalid marker passed in params (%1)",_this];};
if (_foodCost == 99999) exitWith {diag_log format ["Debug Error (BaseClaim.sqf): Invalid food cost passed in params (%1)",_this];};
if (base_num < 0) exitWith {diag_log format ["Debug Error (BaseClaim.sqf): Invalid base number passed in params (%1)",_this];};

//Counts number of enemies around the desired marker
_units = [];
{if (side _x == east && {_marker distance2D _x < 100}) then {_units pushBack _x};} forEach allUnits;

_cnt = count _units;

hint format["Count: %1",_cnt];

comment "rest of script follows here . . . ."

 

Share this post


Link to post
Share on other sites

Ok I got that error fixed. Now I'm back to the original issue of this post! Global variables! I'm getting this now on the .rpt

 

_globalFood = missionNamespace getVariable GLOBALFOOD; 
base_num = _thi>
17:51:26   Error position: <getVariable GLOBALFOOD; 
base_num = _thi>
17:51:26   Error getvariable: Type Number, expected Array,String
17:51:26 File C:\Users\chase\Documents\Arma 3 - Other Profiles\Harbor\missions\State%20of%20Disorder.Malden\BaseFunctions\ClaimBase.sqf, line 22

Oh ok, I see. That only returns array/string. What is a better alternative to getVariable?

Share this post


Link to post
Share on other sites
5 minutes ago, anaximandross said:

Mother of god...that was a dumb mistake! Ok! Well, now that the script is actually executing, its giving me an error that its expecting a number but getting an object. It says the issue is in line8, which is the parameters

 

And thank you so much for your time! I greatly appreciate it.

 

This is where the debug lines come in handy.  As the flagpole addaction params, put this: [base_4,10,-1].  The -1 will cause .rpt log of the passed in params, so you can see what was passed in.  Locate the cause of the bad param, fix it, change -1 back to 4 and test.

Share this post


Link to post
Share on other sites

That's exactly what I did! Ok, so now its saying that _foodCost is undeclared, even though its a parameter. How fix?

 

params 
[
	["_marker","_foodCost"]
];

//Counts number of enemies around the desired marker
_units = [];
{if (side _x == east && {_marker distance2D _x < 100}) then {_units pushBack _x};} forEach allUnits;

_cnt = count _units;
 

hint format["Count:",_cnt];

//_globalFood = missionNamespace getVariable GLOBALFOOD; 
_globalFood = GLOBALFOOD;
base_num = _this select 2;

//checks to see if the area around the base is clear
if(_cnt > 0) then { 
	hint "Clear the area to claim this base!"
	}

//if the area is clear, this allows the player to claim the base
else{ 
	if(_globalFood > _foodCost) then {

			_markPos = getMarkerPos "base_4"; 
			//calculates the amount of food remaining in food stores
			_foodremaining = _globalfood - _foodCost; 
			hint format["You have %1 food remaining!",_foodremaining];
			GLOBALFOOD = _foodremaining; 
			missionNamespace setVariable ["GLOBALFOOD",_foodremaining,true];
			hint "New Home Base Claimed!";
			//determines the current base number to be used for other things
			CURRENTBASENUMBER = base_num; 
			publicVariable "CURRENTBASENUMBER"; 

 

Share this post


Link to post
Share on other sites

Incorrect structure of the params:

params 
[
	["_marker","_foodCost"]
];

 

Test with this:

params ["_marker","_foodCost"];

 

If unaware, this page lists all the commands as links to instructions on how each command can be structured and used.

Share this post


Link to post
Share on other sites

Ah, check and check! I accidentally left an extra set of brackets in when I took the test cases out. Ok, now it says: 

 


else{ 
if(_globalFood > _foodCost) then {

_markPos = getMarke>
18:16:38   Error position: <> _foodCost) then {

_markPos = getMarke>
18:16:38   Error >: Type Object, expected Number,Not a Number
18:16:38 File C:\Users\chase\Documents\Arma 3 - Other Profiles\Harbor\missions\State%20of%20Disorder.Malden\BaseFunctions\ClaimBase.sqf, line 34

It almost seems like GlobalFood is being read as a position? Here's the relevant code:

//checks to see if the area around the base is clear
if(_cnt > 0) then { 
	hint "Clear the area to claim this base!"
	}

//if the area is clear, this allows the player to claim the base
else{ 
	if(_globalFood > _foodCost) then {

			_markPos = getMarkerPos "base_4"; 
			//calculates the amount of food remaining in food stores
			_foodremaining = _globalfood - _foodCost; 
			hint format["You have %1 food remaining!",_foodremaining];
			GLOBALFOOD = _foodremaining; 

 

Share this post


Link to post
Share on other sites

Oh I see. I think its because I used setVariable in the init.sqf. I need to declare them normally.

 

EDIT: Nope, that wasn't it.

Share this post


Link to post
Share on other sites
Error >: Type Object, expected Number,Not a Number

_globalFood is defined from GLOBALFOOD, so the error indicates that somewhere GLOBALFOOD is somehow being redefined as an object.

Share this post


Link to post
Share on other sites

It wasn't, but I fixed it and that wasn't the issue. I can't find any other instances of GLOBALFOOD being used. Would my init do anything? 

 

if (isServer) then {
	fn_getBuildingstospawnLoot = compile preProcessFileLineNumbers "LootSpawn\fn_LSgetBuildingstospawnLoot.sqf"; 
	LSdeleter = compile preProcessFileLineNumbers "LootSpawn\LSdeleter.sqf";
	execVM "LootSpawn\Lootspawner.sqf";
	[]execVM "dynamix\dynamix.sqf";
	[]execVM "VehicleSpawn\vehiclespawner.sqf";
	
	GLOBALFOOD = 20; 
	publicVariable "GLOBALFOOD";

	CURRENTBASENUMBER = 0; 
	publicVariable "CURRENTBASENUMBER";

	NumLgFoodFacilities = 0;
	publicVariable "NumLgFoodFacilities";

	NumSmFoodFacilities = 0;
	publicVariable "NumSmFoodFacilities";

	NumLgWorkshops  = 0;
	publicVariable "NumLgWorkshops";

	NumSmWorkshops = 0;
	publicVariable "NumSmWorkshops";
};

 

Share this post


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

Does publicVariable convert it to a string?

No, it broadcasts the global variable so others get synched as to its value.  But you have to broadcast it as a string:    GLOBALFOOD = _foodRemaining;  publicVariable "GLOBALFOOD";

Share this post


Link to post
Share on other sites

I just remembered, with addAction the passed in params are (_this select 3).

 

(_this select 3) params ["_marker","_foodCost"];
bas_num = (_this select 3) select 2;

 

  • Thanks 1

Share this post


Link to post
Share on other sites
5 hours ago, opusfmspol said:

 Select doesn't use the magic variable "_x",  count and forEach do.

See alternative syntax 5 on the select commands wiki page.

  • Thanks 1

Share this post


Link to post
Share on other sites

Thanks very much Larrow, I've been unaware of that.  Got some uses for it too, now that I know.

Share this post


Link to post
Share on other sites

Can anyone explain to me why this simple script isn't working anymore, I've used it in the past with no problems!

 

_ammo = getArtilleryAmmo [gun1] select 0;
_tgt = getMarkerPos "target1";
gun1 doArtilleryFire[_tgt,_ammo,10];

I've a D-30A with the Variable Name 'gun1' and an invisible marker renamed 'target1', excact same as what I've done before.🤬

Share this post


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

Can anyone explain

 

Where and how are you executing your code?

Share this post


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

 

Where and how are you executing your code?

 

Sorry should have said.

 

Code is in the 'On Activation' box of a trigger which is set with the OPFOR Present condition.

 

My understanding of everything is thatr when an OPFOR unit passes through the trigger, it is activated and the artillery piece (a D-30A with ther Variable Name 'gun1') will fire 10 rounds

Share this post


Link to post
Share on other sites

Should work. The code is OK. Move a little bit your marker, check if the artillery can fire at this position (artillery computer). Check also if this code is running (hint "ok" at start)

Share this post


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

Should work. The code is OK. Move a little bit your marker, check if the artillery can fire at this position (artillery computer)

 

Checked the arty piece and its definately in range, could it be a mod thing?

 

Share this post


Link to post
Share on other sites

Is this trigger scripted? If not, are you wrapping your code in a call or using CBA? Pasting that code into an OnAct or init field yields the "local in global" error unless you are using CBA.

 

Works fine here.

Share this post


Link to post
Share on other sites

Wait, does CBA no longer wrap init/onact field code in a call? Just tested with CBA and it's throwing the local in global error as well.

 

Maybe my brain is just turning to oatmeal.

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

×