Jump to content
🛡️FORUMS ARE IN READ-ONLY MODE Read more... ×
twakkie

Using Case switching with Variable name

Recommended Posts

Hi Guys

 

I know there is probably an easy solution to this, but how would you use Case switching with a entity Variable Name?

A little background: I want to create a unit loadout for my mp mission and decided to write it myself rather than pulling it from the internet with the hopes of learning something.

Because all playable units where of the same entity type, I named each entity p_0 to p_13 and wrote the following script:

//initPlayerLocal.sqf
null = [_this select 0] execVM "scripts\loadout.sqf";
//Loadout.sqf
private ["_unit"];
_unit = _this select 0;

waitUntil {!isNull _unit};  

switch (_unit) do {
        case p_0: { 
		comment "Food Stall Owner";

		comment "Remove existing items";
		removeAllWeapons _unit;
		removeAllItems _unit;
		etc.
	};
	
	case p_1: {
		comment "Hunter";

		comment "Remove existing items";
		removeAllWeapons _unit;
		removeAllItems _unit;
		etc.
	};
...
...
...
       case p_13: {
		comment "Exported from Arsenal by Colgate aka Twakkie";

		comment "Remove existing items";
		removeAllWeapons _unit;
		removeAllItems _unit;
		etc.
	};
	
       default { hint "default" };
};

if(true) exitWith{};

When spawning in from the lobby, the loadout for a specific unit seems to work fine but I get the following rpt error:

Error in expression <nit};       
switch (_unit) do {
case p_0: { 
comment "Food Stall Owner";
com>
Error position: <p_0: { 
comment "Food Stall Owner";
com>
Error Undefined variable in expression: p_0

After this I tried googling and finding an example of a loadout script where someone use entity Variable name rather than the traditional typeOf _unit but couldnt find anything.

Now what I suspect is when spawning into p_1, as in the case of the error above, because entity p_0 has not been created, it gives an error.

As I said, the actual loadout actually works (to my surprise) but I cant figure out what I am missing or doing wrong. Seems I  have a misunderstanding how case works in practice.

 

Thanks

Twak.

Share this post


Link to post
Share on other sites

If player or AI is not present, the variable you called that unit will also be not present, hence undefined variable error. You can for example make sure that variable exists before comparing it:

//--- instead of
case p_0: {

//--- write 
case (if (isNil "p_0") then {-1} else {p_0}): { 

But this will slow switch down. Ideally you should reconsider the whole approach to loadout selection.

Share this post


Link to post
Share on other sites

If player or AI is not present, the variable you called that unit will also be not present, hence undefined variable error. You can for example make sure that variable exists before comparing it:

 

Thanks for replying KK. I figured that much eventually. Will try a different approach.

In the meantime can you suggest something I can look into?

I am looking for a holistic approach other than executing the loadout from the init of each unit and not using the typeOf approach.

 

Thanks.

Share this post


Link to post
Share on other sites

Or check against their vehicleVarName.

//Loadout.sqf
params[ "_unit" ];
_name = vehicleVarName _unit;

switch (_name) do {
        case "p_0": { 
		comment "Food Stall Owner";

		comment "Remove existing items";
		removeAllWeapons _unit;
		removeAllItems _unit;
		//etc.
	};
  • Like 1

Share this post


Link to post
Share on other sites

or simply against the str.

params[ "_unit" ];
_name = str _unit;

switch (toUpper _name) do {
        case "P_0": {
        comment "Food Stall Owner";

        comment "Remove existing items";
        removeAllWeapons _unit;
        removeAllItems _unit;
        //etc.
    };
  • Like 1

Share this post


Link to post
Share on other sites

i just use the classname. there are usually enough classnames and that way you can easily reuse a loadout in case i want to have a military unit in which 2 soldiers of the same class have the same equipment.

i also have the loadouts in separate scripts to make management easier.

_class = typeof player;

switch (_class) do {
/*-------------------
Blufor Regular
-------------------*/
case "B_Soldier_SL_F": {
execvm "Gear\blue_SL.sqf";
};
case "B_Soldier_TL_F": {
execVM "Gear\blue_TL.sqf"
};
case "B_Soldier_F": {
execVM "Gear\blue_RF.sqf"
};
 };
The Script is executed in the initplayerlocal.sqf

Share this post


Link to post
Share on other sites

Thanks everyone.

Got it sorted now.

End up using KK suggestion and worked liked a charm before revisiting the forums and seeing Larrows' vehicleVarName suggestion.

Is actually exactly what I am looking for! Thanks man.

Definitely learned allot!

Cheers.

Edit: Belbo thanks for that easy solution. Didnt even come close to thinking of something so simple. Cheers boet!

Share this post


Link to post
Share on other sites

I'd recommend using speaking unitnames though, especially if you want to use this system in other missions. You could easily set it up like this:

params[ "_unit" ];
_name = str _unit;
_prefix = [_name,0,4] call BIS_fnc_trimString;    //cuts first five letters of _name. If a unit is named LEADER_1 it will return LEADE. That way you can have many units with this nomenclature (LEADER_123, LEADER_COMMAND etc.)

switch (toUpper _prefix) do {
    case "OWNER": {
        //owner script
    };
    case "LEADE" {
        //leader script
    };
};

Share this post


Link to post
Share on other sites

×