Jump to content
markhansaven

Help with Sector Control, Sector Module doesn't accept 'this' in scheduled script in Init field...

Recommended Posts

Hello there, 

I'm making a sector contol map, and I'm trying to custom spawn units using a sector's 'Init' field. 

The first thing I did was get AI moving for West by setting a trigger, condition: not present, anybody with this in it's Init:

call{[] spawn { 
_start = true;   
while { _start } do {    
    
  {if ((side _x) == west) then {      
      
   
  _targetSector = ""; 
  if (unitReady _x) then {  
   
  _y = _x;   
      
     
     {    
      
      _owner = _x getVariable "owner"; 
   if((getPos _x) distance _y > 50 && (_x getVariable "owner") != west) then    
   {    
    _targetSector = _x;   
 
   };    
     } forEach (true call BIS_fnc_moduleSector);     
    
  _y moveTo getPos _targetSector;    
  _y doMove getPos _targetSector;       
  }; 
    
  }; 
    
  } forEach allUnits;    
    
 sleep 30;    
};    
};}

 

But the problem I'm having is with the Sector. I can't use 'this' for some reason in it's Init field using a scheduled script? Why is that?

call{[] spawn {  
_started = true; 
while { _started } do {   
 
       cnt = {alive _x} count allUnits;
       hint format ["NumOfUnits: %1", cnt];
       
       if (cnt < 80) then { 
           if ((this getVariable "owner") == independent) then {
                _newGroup = createGroup independent; 
                _newGroup createUnit ["LIB_US_SMGunner", getPos this,[],0,"NONE"];  
                _newGroup createUnit ["LIB_US_Grenadier", getPos this,[],0,"NONE"];
                _newGroup createUnit ["LIB_US_Corporal", getPos this,[],0,"NONE"];
                _newGroup createUnit ["LIB_US_AT_Soldier", getPos this,[],0,"NONE"];
                _newGroup createUnit ["LIB_US_Medic", getPos this,[],0,"NONE"];
           };
           
           if ((this getVariable "owner") == west) then { 
                _newGroup = createGroup west; 
                _newGroup createUnit ["SG_sturmtrooper_unterofficer", getPos this,[],0,"NONE"];  
                _newGroup createUnit ["SG_sturmtrooper_rifleman", getPos this,[],0,"NONE"];
                _newGroup createUnit ["SG_sturmtrooper_medic", getPos this,[],0,"NONE"];
                _newGroup createUnit ["SG_sturmtrooper_sapper_gefr", getPos this,[],0,"NONE"];
                _newGroup createUnit ["SG_sturmtrooper_mgunner", getPos this,[],0,"NONE"];
           };
       };

 sleep 80;  
};   
};}

 

If I could only use 'this' there it would be finished and ready to play. So, what I tried was in a trigger putting all of the sector modules into an array like this: 

Sectors = []; 
{ 
 Sectors pushBack _x; 
} forEach (true call BIS_fnc_moduleSector);   

 

The idea is that now I can use Sectors like so within the sector module:

thisSectorID = Sectors find this;
thisSector = Sectors select thisSectorID;

But now yet ANOTHER problem emerges, and that's that I have no way of telling if Sectors has been declared yet. So, I try 

call{[] spawn {  
_started = true; 
while { _started } do {  
   waitUntil {(count Sectors) > 0};
};   
};}

But then 'Sectors' is invalid and doesn't exist in the scheduled script. I'm so confused. Are scheduled and unscheduled scripts not able to communicate with each other? Why aren't scheduled scripts able to grab unscheduled scripts variables (in this case Sectors). 

Share this post


Link to post
Share on other sites

When you spawn a new thread, you need to send the local variables with it:

 

[this] spawn {

  params ["_var"];

  //code

 

this will now be defined as _var in the newly spawned thread. 

Share this post


Link to post
Share on other sites

Even if you might not want it, here is some random script advice.

 

10 hours ago, markhansaven said:

call{[] spawn {

The outer call is useless.

 

10 hours ago, markhansaven said:

then { _targetSector = _x; }; } forEach

that should be using findIf

 

10 hours ago, markhansaven said:

Why is that?

"this" is a local variable.

Local variables carry over to lower scopes (call, then, while/do) but not into different scripts. spawn spawns a completely new script, which doesn't know where it was spawned from and also doesn't know about any variables that might have been present where it was spawned, so you need to pass the variables as argument into it as beno wrote above.

 

10 hours ago, markhansaven said:

cnt = {alive _x} count allUnits;

Don't use such a generic global variable, it seems like this variable is only used locally so a global variable makes no sense here.

Also global variables should all have a specific tag that is the same for all your variables to make sure they don't conflict with anyone elses variables.

Something like "mar_count" or "mark_cnt" or something like that, to make sure the variable is unique.

 

10 hours ago, markhansaven said:

cnt = {alive _x} count allUnits;

hint format ["NumOfUnits: %1", cnt];

 if (cnt < 80) then { 

 

A scheduled script might suspend (pause) at any time, now you variable name is not unique at all, and other scripts might be using it.
So it might happen then your script get's paused after the hint, then someone elses script runs who also uses a cnt variable, and the other script puts "test" into the cnt variable.
Now next time your script runs, cnt contains "test" and you are trying to do "test" < 80, which will error out, and you will be completely stumped as to how that error can happen.

 

10 hours ago, markhansaven said:

Are scheduled and unscheduled scripts not able to communicate with each other?

They can communicate just like every other script.

 

10 hours ago, markhansaven said:

Why aren't scheduled scripts able to grab unscheduled scripts variables (in this case Sectors).

Scripts generally cannot see local variables of other scripts. That's why the variables are called local. Local to that specific script and not visible anywhere else.

  • Like 1

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

×