Jump to content
Sign in to follow this  
frosties

How to use execVM properly

Recommended Posts

Yes, i have searched and searched but cant find what im looking for, probably already answered somewhere but i cant find it.

Im trying to change loadouts on a soldier and in a vehicle.

Its working when i put it in the INIT field, but its getting annoying trying to change it so i was planning on executing it from a SQF file instead.

I have put:

this = [] execVM "loadout.sqf"

in the INIT field of the soldier.

And

removeAllWeapons this;
removeAllMagazines this;

this addMagazine "30Rnd_9x19_MP5",5;
this addWeapon "MP5A5";

this selectWeapon "MP5A5";

in the loadout.sqf file.

But its not working?

What is the correct usage of this?

Is it the same INIT line on the soldier as it is on the vehicle?

Share this post


Link to post
Share on other sites

In init:

xhandler = [this] execVM "loadout.sqf"

In loadout.sqf:

_Soldier = _This select 0;

removeAllWeapons _soldier;

removeAllMagazines _soldier;

_soldier addMagazine "30Rnd_9x19_MP5",5;

_soldier addWeapon "MP5A5";

_soldier selectWeapon "MP5A5";

Basically, in the init line, you start with any variable (I prefer 'xhandler' - I used to say 'null' but that apparently messes with something. You need to use a variable that will not affect anything else. Use 'xhandler'.) for executing the script so there is no error by the engine (it doesn't like executing a script straight-up from an init line).

The '[this]' part of it is the array the script 'owns', so to speak. In the script, '_this select 0' is pointing to the first part of this array (in this example there is only one 'part'). Therefore, '_this select 0' means 'Get the first part of the array' (which is 'this' in our init line: xhandler = [this] execVM "loadout.sqf"

Arrays are done this way as it's a 'zero index'; it counts up starting from 0. The first element of an array is found with 'select 0', the 2nd element with 'select 1', and so on.

_Soldier = _This select 0; assigns the variable local to the script ('_' before a variable means the variable is local ONLY to that script, and nothing else uses it, nor can they see it). So, within the script, whenever we say '_soldier', it picks the FIRST element from the array we run the script using (back in our init line), as '_soldier' is equal/set to '_this select 0'. By this, I mean;

Init:

xhandler = [this] execVM "loadout.sqf"

Script:

_Soldier = _This select 0;

removeAllWeapons _soldier;

removeAllMagazines _soldier;

_soldier addMagazine "30Rnd_9x19_MP5",5;

_soldier addWeapon "MP5A5";

_soldier selectWeapon "MP5A5";

See how the bolded parts are linked as per my earlier explanation?

Does this make sense? :D

Edited by HateDread

Share this post


Link to post
Share on other sites

thanks, will try this at once

---------- Post added at 15:11 ---------- Previous post was at 15:05 ----------

i tried this, and what happens is that i loose my "default" weapon, but doesnt get a new one?

INIT:

xhandler = [this] execVM "loadout.sqf";

SQF:

_Soldier = _This select 0;
removeAllWeapons _soldier;
removeAllMagazines _soldier;

_soldier addMagazine "100Rnd_556x45_M249",5;
_soldier addWeapon "BAF_L110A1_Aim";
_soldier selectWeapon "BAF_L110A1_Aim"; 
_soldier addWeapon "ACE_Earplugs";

Edited by Frosties

Share this post


Link to post
Share on other sites

Sorry, that was my mistake; I didn't read over your commands exactly.

For now, use this:

_Soldier = _This select 0;

removeAllWeapons _soldier;

_soldier addMagazine "100Rnd_556x45_M249";

_soldier addMagazine "100Rnd_556x45_M249";

_soldier addMagazine "100Rnd_556x45_M249";

_soldier addMagazine "100Rnd_556x45_M249";

_soldier addMagazine "100Rnd_556x45_M249";

_soldier addWeapon "BAF_L110A1_Aim";

_soldier selectWeapon "BAF_L110A1_Aim";

_soldier addWeapon "ACE_Earplugs";

Share this post


Link to post
Share on other sites

worked like a charm!

guess it was the boxes that messed it up uh?

Share this post


Link to post
Share on other sites

Two problems:

this addMagazine "30Rnd_9x19_MP5",5; doesn't work, as you'd need an array at the end. (Something like this addmagazine ["30Rnd_9x19_MP5",5]; but that only works with vehicle cargo). I.e. what you were trying doesn't work.

And removeAllMagazines _soldier; doesn't exist. Removeallweapons takes out both weapon and magazines, unless it's a vehicle, then only magazines will go.

I advise you to add -showscripterrors to your ArmA 2 shortcut's target line. Then you can see if you're doing it wrong. That's why I assumed it was right, as you hadn't indicated otherwise; now I know that's because you didn't know.

Also; BIS Wiki, and specifically this section shall help you SO MUCH. Search using 'Control F' on this page for everything you need!

http://community.bistudio.com/wiki/Category:Scripting_Commands_ArmA2

Happy scripting! Feel free to send any questions my way! And don't be afraid to post :)

- HateDread.

Share this post


Link to post
Share on other sites

Squint will save you a lot of time with syntax errors such as the addMagazine mistake above !

Share this post


Link to post
Share on other sites

Agree - squint is a wonderful tool for locating blatent or subtle errors.

Share this post


Link to post
Share on other sites

I use squint myself on occasion, but it tend to be only when I'm getting into serious problems I can't figure out myself. Is it something for the complete noob? I'm not so sure (feel free to disagree). A big part of learning to script is learning to find the information you're looking for, pluss find a ton of useful information you're not looking for. :) I consider myself a fairly experienced scripter, but even today I find useful information about stuff that I wasn't really looking for - just kinda stumble onto it from time to time. Squint kind of takes that part away.

The learning curve is steep, and the number of commands so many it's quite difficult to find the right one. Use squint by all means, but be careful not to rely too much on it, because it can take away some important lessons. Even frustrating ones. The thing to be weary of is the Biki and how it often contains examples that you typically can't just copy & paste and expect it to work (may be outdated classes used etc).

Share this post


Link to post
Share on other sites

Thanks for the help guys.. :)

regarding the loadout.sqf..

If i would like different loadouts for different "roles" do i need to have different SQF-files for each role, or can i build in some kind of IF setting?

Share this post


Link to post
Share on other sites

Something like this maybe?

if (_role == "machinegunner") then
{
   _Soldier = _This select 0;
removeAllWeapons _soldier;

_soldier addMagazine "100Rnd_556x45_M249";
_soldier addMagazine "100Rnd_556x45_M249";
_soldier addMagazine "100Rnd_556x45_M249";
_soldier addMagazine "100Rnd_556x45_M249";
_soldier addMagazine "100Rnd_556x45_M249";
_soldier addWeapon "BAF_L110A1_Aim";
_soldier selectWeapon "BAF_L110A1_Aim";
_soldier addWeapon "ACE_Earplugs";
}
else
{
   if (_role == "sniper") then
   {
       _Soldier = _This select 0;
removeAllWeapons _soldier;

_soldier addMagazine "ACE_5Rnd_762x51_T_M24 ";
_soldier addWeapon "M24";
_soldier selectWeapon " M24";
_soldier addWeapon "ACE_Earplugs";
   }
};

Share this post


Link to post
Share on other sites

This works:

INIT:

xhandler = [this,"ROLE"]execVM "loadout.sqf";

E.G. xhandler = [this,"sniper"]execVM "loadout.sqf";

Loadout.sqf:

_Soldier = _This select 0;

_role = _This select 1;

removeAllWeapons _soldier;

if (_role == "machinegunner") then

{

_soldier addMagazine "100Rnd_556x45_M249";

_soldier addMagazine "100Rnd_556x45_M249";

_soldier addMagazine "100Rnd_556x45_M249";

_soldier addMagazine "100Rnd_556x45_M249";

_soldier addMagazine "100Rnd_556x45_M249";

_soldier addWeapon "BAF_L110A1_Aim";

_soldier selectWeapon "MBAF_L110A1_Aim";

_soldier addWeapon "ACE_Earplugs";

}

else

{

if (_role == "sniper") then

{

_soldier addMagazine "ACE_5Rnd_762x51_T_M24 ";

_soldier addWeapon "M24";

_soldier selectWeapon " M24";

_soldier addWeapon "ACE_Earplugs";

}

};

Share this post


Link to post
Share on other sites

great thanks,

and for additonal roles i just copy and paste:

if (_role == "sniper") then
{

_soldier addMagazine "ACE_5Rnd_762x51_T_M24 ";
_soldier addWeapon "M24";
_soldier selectWeapon " M24";
_soldier addWeapon "ACE_Earplugs";
}

and change sniper to grenadier for example? .. and the loadout of course.. :)

Share this post


Link to post
Share on other sites

Sorry for late reply.

Yes, you just add each of those 'sections' to it :)

Hope it all helps!

Share this post


Link to post
Share on other sites

Lol, err, no :D I pointed you to switch do and you used the first example which shows what switch do is designed to avoid. Sometimes it pays off to read the lines between the examples :bounce3:

Share this post


Link to post
Share on other sites

I´ve been trying to add more roles in my loadout.sqf but only the 2 first roles are working, the other roles doenst get anything at all...

Can somebody help me?

_Soldier = _This select 0;
_role = _This select 1;

removeAllWeapons _soldier;

if (_role == "role1") then
{
_soldier addMagazine "ACE_30Rnd_556x45_T_Stanag";
_soldier addMagazine "ACE_30Rnd_556x45_T_Stanag";
_soldier addMagazine "ACE_30Rnd_556x45_T_Stanag";
_soldier addMagazine "ACE_30Rnd_556x45_T_Stanag";
_soldier addMagazine "ACE_30Rnd_556x45_T_Stanag";
_soldier addWeapon "ACE_M27_IAR_ACOG";
_soldier selectWeapon "ACE_M27_IAR_ACOG";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addWeapon "glock17_EP1";
_soldier addWeapon "ACE_Earplugs";
_soldier addWeapon "ACE_M136_CSRS";
_soldier addWeapon "ACE_GlassesTactical";

}
else
{
if (_role == "role2") then
{

_soldier addMagazine "200Rnd_556x45_M249";
_soldier addMagazine "200Rnd_556x45_M249";
_soldier addMagazine "200Rnd_556x45_M249";
_soldier addMagazine "200Rnd_556x45_M249";
_soldier addMagazine "200Rnd_556x45_M249";
_soldier addWeapon "M249_m145_EP1";
_soldier selectWeapon "M249_m145_EP1";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addWeapon "glock17_EP1";
_soldier addWeapon "ACE_Earplugs";
_soldier addWeapon "ACE_M136_CSRS";
_soldier addWeapon "ACE_GlassesTactical";
}

{
if (_role == "role3") then
{

_soldier addMagazine "200Rnd_556x45_M249";
_soldier addMagazine "200Rnd_556x45_M249";
_soldier addMagazine "200Rnd_556x45_M249";
_soldier addMagazine "200Rnd_556x45_M249";
_soldier addMagazine "200Rnd_556x45_M249";
_soldier addWeapon "M249_m145_EP1";
_soldier selectWeapon "M249_m145_EP1";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addWeapon "glock17_EP1";
_soldier addWeapon "ACE_Earplugs";
_soldier addWeapon "ACE_M136_CSRS";
_soldier addWeapon "ACE_GlassesTactical";
}
else
{
if (_role == "role4") then
{

_soldier addMagazine "200Rnd_556x45_M249";
_soldier addMagazine "200Rnd_556x45_M249";
_soldier addMagazine "200Rnd_556x45_M249";
_soldier addMagazine "200Rnd_556x45_M249";
_soldier addMagazine "200Rnd_556x45_M249";
_soldier addWeapon "M249_m145_EP1";
_soldier selectWeapon "M249_m145_EP1";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addWeapon "glock17_EP1";
_soldier addWeapon "ACE_Earplugs";
_soldier addWeapon "ACE_M136_CSRS";
_soldier addWeapon "ACE_GlassesTactical";
}
};

Share this post


Link to post
Share on other sites

A noob needs more help than the expert in finding those syntax errors, so tools like squint will help a lot.

It's not perfect, neither is lint or splint in the C program world. But life is far better with all those tools around.

Share this post


Link to post
Share on other sites

I agree, but learning your lesson the hard way is often the best way. You won't be productive at at the start anyway, so learning to find the information and get (understand) the principles behind the syntax is of very high value.

It's like that first miserable attempt on doing a simple web page. Notepad and a web reference will teach you more than some automatic tool. At least back in my day when automatic tool meant Frontpage or (dare I even say it) - Word :D

Share this post


Link to post
Share on other sites

i´ve installed the squint tool, but cant seem to figure out what im doing wrong.

the first two roles are working, so i guess it has something to do with the second IF ..

Share this post


Link to post
Share on other sites

Your version, reindented showed at least some errors easily:

_Soldier = _This select 0;
_role = _This select 1;

removeAllWeapons _soldier;

if (_role == "role1") then
{
_soldier addMagazine "ACE_30Rnd_556x45_T_Stanag";
_soldier addMagazine "ACE_30Rnd_556x45_T_Stanag";
_soldier addMagazine "ACE_30Rnd_556x45_T_Stanag";
_soldier addMagazine "ACE_30Rnd_556x45_T_Stanag";
_soldier addMagazine "ACE_30Rnd_556x45_T_Stanag";
_soldier addWeapon "ACE_M27_IAR_ACOG";
_soldier selectWeapon "ACE_M27_IAR_ACOG";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addMagazine "17Rnd_9x19_glock17";
_soldier addWeapon "glock17_EP1";
_soldier addWeapon "ACE_Earplugs";
_soldier addWeapon "ACE_M136_CSRS";
_soldier addWeapon "ACE_GlassesTactical";

}
else
{
if (_role == "role2") then
{

	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addWeapon "M249_m145_EP1";
	_soldier selectWeapon "M249_m145_EP1";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addWeapon "glock17_EP1";
	_soldier addWeapon "ACE_Earplugs";
	_soldier addWeapon "ACE_M136_CSRS";
	_soldier addWeapon "ACE_GlassesTactical";
}
   else
{
	if (_role == "role3") then
	{

		_soldier addMagazine "200Rnd_556x45_M249";
		_soldier addMagazine "200Rnd_556x45_M249";
		_soldier addMagazine "200Rnd_556x45_M249";
		_soldier addMagazine "200Rnd_556x45_M249";
		_soldier addMagazine "200Rnd_556x45_M249";
		_soldier addWeapon "M249_m145_EP1";
		_soldier selectWeapon "M249_m145_EP1";
		_soldier addMagazine "17Rnd_9x19_glock17";
		_soldier addMagazine "17Rnd_9x19_glock17";
		_soldier addMagazine "17Rnd_9x19_glock17";
		_soldier addMagazine "17Rnd_9x19_glock17";
		_soldier addWeapon "glock17_EP1";
		_soldier addWeapon "ACE_Earplugs";
		_soldier addWeapon "ACE_M136_CSRS";
		_soldier addWeapon "ACE_GlassesTactical";
	}
	else
	{
		if (_role == "role4") then
		{

			_soldier addMagazine "200Rnd_556x45_M249";
			_soldier addMagazine "200Rnd_556x45_M249";
			_soldier addMagazine "200Rnd_556x45_M249";
			_soldier addMagazine "200Rnd_556x45_M249";
			_soldier addMagazine "200Rnd_556x45_M249";
			_soldier addWeapon "M249_m145_EP1";
			_soldier selectWeapon "M249_m145_EP1";
			_soldier addMagazine "17Rnd_9x19_glock17";
			_soldier addMagazine "17Rnd_9x19_glock17";
			_soldier addMagazine "17Rnd_9x19_glock17";
			_soldier addMagazine "17Rnd_9x19_glock17";
			_soldier addWeapon "glock17_EP1";
			_soldier addWeapon "ACE_Earplugs";
			_soldier addWeapon "ACE_M136_CSRS";
			_soldier addWeapon "ACE_GlassesTactical";
		};
	};
};
};

My version, using switch do. Neater look and tend to be more effective:

_Soldier = _This select 0;
_role = _This select 1;

removeAllWeapons _soldier;

switch (_role) do {
case "role1" : {
	_soldier addMagazine "ACE_30Rnd_556x45_T_Stanag";
	_soldier addMagazine "ACE_30Rnd_556x45_T_Stanag";
	_soldier addMagazine "ACE_30Rnd_556x45_T_Stanag";
	_soldier addMagazine "ACE_30Rnd_556x45_T_Stanag";
	_soldier addMagazine "ACE_30Rnd_556x45_T_Stanag";
	_soldier addWeapon "ACE_M27_IAR_ACOG";
	_soldier selectWeapon "ACE_M27_IAR_ACOG";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addWeapon "glock17_EP1";
	_soldier addWeapon "ACE_Earplugs";
	_soldier addWeapon "ACE_M136_CSRS";
	_soldier addWeapon "ACE_GlassesTactical";
};
case "role2" : {
	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addWeapon "M249_m145_EP1";
	_soldier selectWeapon "M249_m145_EP1";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addWeapon "glock17_EP1";
	_soldier addWeapon "ACE_Earplugs";
	_soldier addWeapon "ACE_M136_CSRS";
	_soldier addWeapon "ACE_GlassesTactical";
};
case "role3" : {
	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addWeapon "M249_m145_EP1";
	_soldier selectWeapon "M249_m145_EP1";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addWeapon "glock17_EP1";
	_soldier addWeapon "ACE_Earplugs";
	_soldier addWeapon "ACE_M136_CSRS";
	_soldier addWeapon "ACE_GlassesTactical";
};
case "role4": {
	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addMagazine "200Rnd_556x45_M249";
	_soldier addWeapon "M249_m145_EP1";
	_soldier selectWeapon "M249_m145_EP1";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addMagazine "17Rnd_9x19_glock17";
	_soldier addWeapon "glock17_EP1";
	_soldier addWeapon "ACE_Earplugs";
	_soldier addWeapon "ACE_M136_CSRS";
	_soldier addWeapon "ACE_GlassesTactical";
};
};

Didn't test either of them, could be more errors that I didn't catch. No, didn't bother to fire up squint for this one :p

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
Sign in to follow this  

×