Home   Help Search Login Register  

Author Topic: Call Script from trigger  (Read 5982 times)

0 Members and 1 Guest are viewing this topic.

Offline Knight Trane

  • Members
  • *
Call Script from trigger
« on: 31 Jan 2009, 17:28:06 »
I have a script that places units like so (small sample):
Code: [Select]
//keep private variables lower case
private["_gl","_trigger","_factory","_unit0","_unit1","_unit2","_unit3","_unit4","_unit5","_unit6","_unit7","_unit8","_unit9","_unit10","_unit11","_unit12","_unit13","_unit14","_unit15","_unit16","_unit17","_unit18","_unit19","_unit20","_unit21","_unit22","_unit23","_unit24","_unit25","_unit26","_unit27","_unit28","_unit29","_unit30","_unit31","_unit32","_unit33","_unitAT1","_unitAT2","_unitAT3","_specops"];

_gl=_this select 0;

sleep(.1);

//  specops is the Group to be created.  Your units will be part of this group.
_specops = createGroup east;
_factory = nearestBuilding _gl;

if (isNull _factory) then {
hintc "No building was found!!!";
} else {
_unit0 = _specops createUnit ["ACE_SoldierEAR_IRAQ_INS", getPos _factory, [] ,0 ,"seargent"];
[_unit0] join _specops;
_unit0 setDir 180;
_specops selectLeader _unit0;
_unit0 disableAI "MOVE";
_unit0 disableAI "TARGET";
_unit0 setUnitPos "down";
_unit0 allowfleeing 0;
_unit0 setBehaviour "AWARE";
_unit0 setPos [2436.4,2376.3,0]
         };

This works fine.  It gives me the ability to place the unit in a BuildingPos or setPos them where I want.  I Call the script from a radio trigger "Radio Charlie", with this:
Code: [Select]
puhandle=[this] execVM "PlaceUnits.sqf";
After the units are created, I want to reposition them into they'er fighting positions.  E.G. Machinegunners roll out right to their position in the street, AT guys go to kneeling position, ect...

This is the script I am writing for that:
Code: [Select]
_unit0 playMove "amovppnemstpsraswrfldnon_amovppnemevaslowwrfldr";
_unit20 playMove "amovppnemstpsraswrfldnon_amovppnemevaslowwrfldr";
_unitAT1 setUnitPos "MIDDLE";
_unitAT2 setUnitPos "MIDDLE";
_unitAT3 setUnitPos "MIDDLE";

I trying to call this from a radio trigger  "Radio Delta."  I've tried everything I can think of.

Code: [Select]
[]execVM "MyScript.sqf";

[this]execVM "MyScript.sqf";

mshandle=[]execVM "MyScript.sqf";

mshandle=[this]execVM "MyScript.sqf";

and even

[]exec "MyScript.sqs";

None of these produce any result.  I know it has to do with carrying a variable or some such nonsense.  Pulling hair out.  Please help.

Offline Deadfast

  • Members
  • *
Re: Call Script from trigger
« Reply #1 on: 31 Jan 2009, 17:47:42 »
Well, these are 2 separate scripts and you appear trying to access local variables for the 1st one from the second one.

That's not possible, you'd have to make them global.

Offline Knight Trane

  • Members
  • *
Re: Call Script from trigger
« Reply #2 on: 31 Jan 2009, 17:51:49 »
Is it as simple as taking those Variable names out of the "Private" array?

Offline dr. seltsam

  • Members
  • *
Re: Call Script from trigger
« Reply #3 on: 31 Jan 2009, 17:55:10 »
I think in a trigger the term "this" means the trigger condition setting like "west present" or "east detected by west", etc.

"thislist" is an array of all units that are in the triggerzone and are matching with the side of units that the trigger should detect.

You should give your trigger a name, like for example

trig1

After that you execute your script:
[trig1] exec "MyScript.sqs";
or
[trig1] execVM "MyScript.sqf";

That way the line
_gl=_this select 0;
gets the trigger object as value...

:)

Well, here is something interesting i found out while using the "exec" command. Lets say the value

variable1

is not defined in the mission...
If you now try to execute a script with it...

variable1 exec "test.sqs";

"test.sqs" will not start at all! Maybe you have the same result with the "execVM" command...

Try to debug your scripts by displaying the input values at the beginning with:

player globalchat format ["%1",_this]

:):)

« Last Edit: 31 Jan 2009, 18:00:53 by dr. seltsam »

Offline Knight Trane

  • Members
  • *
Re: Call Script from trigger
« Reply #4 on: 31 Jan 2009, 18:14:29 »
I've tried using "trg1" as trigger name.  Nothing

Its's just not getting called.

Quote
you'd have to make them global.

How do I do that?

Offline hoz

  • OFPEC Site
  • Administrator
  • *****
Re: Call Script from trigger
« Reply #5 on: 31 Jan 2009, 18:31:40 »
Code: [Select]
_unit0 playMove "amovppnemstpsraswrfldnon_amovppnemevaslowwrfldr";
_unit20 playMove "amovppnemstpsraswrfldnon_amovppnemevaslowwrfldr";
_unitAT1 setUnitPos "MIDDLE";
_unitAT2 setUnitPos "MIDDLE";
_unitAT3 setUnitPos "MIDDLE";


In this instance _unit20 is  a private variable. Not global.

When you create the unit you need to give it a proper name. line Unit20. So when you create the unit rather then using _unit0

Code: [Select]
_unit0 = _specops createUnit ["ACE_SoldierEAR_IRAQ_INS", getPos _factory, [] ,0 ,"seargent"];
use Unit0  and the variable willl be global. Then you can manipulate that unit in your other script.

Code: [Select]
unit0 = _specops createUnit ["ACE_SoldierEAR_IRAQ_INS", getPos _factory, [] ,0 ,"seargent"];
When ever you use a _ in front of the variable it makes that variable private and only usable within the script or code block.

Then in your trigger call the script like this...  [unit20]execVM "MyScript.sqf";

edit:

one more tip, you don't need to do this int he private statement.

Code: [Select]
"_unit0","_unit1","_unit2","_unit3","_unit4","_unit5","_unit6","_unit7","_unit8","_unit9","_unit10","_unit11","_unit12","_unit13","_unit14","_unit15","_unit16","_unit17","_unit18","_unit19","_unit20","_unit21","_unit22","_unit23","_unit24","_unit25","_unit26","_unit27","_unit28","_unit29","_unit30","_unit31","_unit32","_unit33","_unitAT1","_unitAT2","_unitAT3"
What you can do is add a counter to your loop something like this.

_a = 0;
loop start

_a = _a+1;
_myUnitName = format["%1", "unit"+_a];
_myUnitName = _specops createUnit ["ACE_SoldierEAR_IRAQ_INS", getPos _factory, [] ,0 ,"seargent"];

loop end

this should create each unit with a name of unit1, unit2, etc.

I might of went beyond what you wanted to know here.   :dunno:
« Last Edit: 31 Jan 2009, 18:46:09 by hoz »
Xbox Rocks

Offline Knight Trane

  • Members
  • *
Re: Call Script from trigger
« Reply #6 on: 31 Jan 2009, 18:49:09 »
OK,  "_unit0" has been changed to "Unit0" in both scripts.  I still have the trouble of how to call the script.  Nothing seems to work.

--Edit----
Ok again,
I have tried every combination of sqf call I can or know about.  And the darn thing isn't having any of it.

Then in your trigger call the script like this...  [unit20]execVM "MyScript.sqf";
The editor gives me the error"Type Script, expected nothing"
????????????

It works in sqs with this
Code: [Select]
[tgr1]execVM "Myscript.sqs" but shouldn't this be able to work as sqf?



« Last Edit: 01 Feb 2009, 06:12:20 by Knight Trane »

Offline hoz

  • OFPEC Site
  • Administrator
  • *****
Re: Call Script from trigger
« Reply #7 on: 31 Jan 2009, 22:40:21 »
from a trigger I usually run something like this. but spooner always says I don't have to include the var =

Code: [Select]
dummy = [player,true] execvm "cutsc1.sqf"
Xbox Rocks

Offline Knight Trane

  • Members
  • *
Re: Call Script from trigger
« Reply #8 on: 01 Feb 2009, 08:06:41 »
Alright,
My bad.  handle=[tgr1]execVM "Myscript.sqf";  is working.  When I changed my variables to global in the sqf I didn't save the changes. and assumed the call was not working.  I took a friend to see my error.

Thanks for all your help.  and the lesson on variables. :-[

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Call Script from trigger
« Reply #9 on: 01 Feb 2009, 10:21:01 »
You only don't need the "nul =" (or "dummy =" or whatever) when you are running inside a script file or inside a function. You need it whenever you use execVM or spawn directly in the editor (unless they happen to be inside {}, in which case they are inside a function and follow normal SQF script rules). The editor tries very hard to force you to code correctly, by not ignoring the script handle generated by these two commands. No idea why, since it is rarely used.

Thus, in the editor, you can do this:
Code: (need to assign the script handle to something) [Select]
nul = [] execVM "frog.sqf";
or
Code: (don't need to assign the handle inside {}) [Select]
if x > 5 then { [] execVM "frog.sqf" };
or even
Code: [Select]
nul = [] spawn { [] spawn FISH_andChips };
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)