Jump to content
BlacKnightBK

Enums, are they possible

Recommended Posts

Hello guys, I wanted to create some global variables I need some special data. I thought of doing those as an array but remembered from java that Enums would be cleaner. Is that possible with SQF or not and I should just create my variables as Arrays instead??

CHeers

Share this post


Link to post
Share on other sites
15 minutes ago, Muzzleflash said:

You can just do:

 

MyEnum_ValA = 10;

MyEnum_ValB = 20;

MyEnum_ValC = 99;

 

I do not see anything but just integer variables, how do I treat those as Enums.

back in java an Enum called DLC looked something like this:

b816d75fd8e3e86c4b403d78b63003cd.png

 

 

Except in sqf it is important to me because for each of my enums I want a String, an Integer and an Array of 2 integers

 

Share this post


Link to post
Share on other sites

As far as I know enums are variables represented by numbers (at least in c++).

Enums usually ascend in value, with the ability of having custom values and the next enum in line will continue to ascend its value from the previous one, again at least that's how I know it from c++.

What you show is an array of variables that hold strings.

It's called eNUM after all.

 

Cheers

Share this post


Link to post
Share on other sites

Use defines:

 

#define APEX "apex"

 

 

Share this post


Link to post
Share on other sites

In program terms an enum is an enumerated type. That is you enumerate (specify exactly) the values that belong to the type. They don't have to integers, although it is common in C-like languages. Since we can't make custom types in SQF, we will have to make do with what we got. Basically the current values possible, e.g. number, string or array. And since SQF is not object oriented, we do not have methods, on however, we make our enum.

 

35 minutes ago, BlacKnightBK said:

how do I treat those as Enums.

What exactly you you mean by this? Do you mean you wan't to able to type something like:  Dlc.Apex   ,  if so you can't. How do you wan't to treat your values?

 

Maybe something like this is closer to what you had in mind:
 

Enum_fnc_MakeEnum = {
    params ["_name", "_values"];
    private _result = [];
    {
        _x params ["_key", "_value"];
        _result pushBack _value;
        private _varName = format ["%1_%2", _name, _key];
        missionNamespace setVariable [_varName, _forEachIndex];
    } forEach _values;
    missionNamespace setVariable [_name, _result];
    _result
};

["DLC", [
    ["VANILLA", "Vanilla"],
    ["LAWS_OF_WAR", "Laws of War"]
]] call Enum_fnc_MakeEnum;

// Now you have all values in:
{
    systemChat str _x; //Will output Vanilla, then "Laws of War"
} forEach DLC;

// Or pick a specific value:

private _LoW = DLC select DLC_LAWS_OF_WAR;
systemChat str _LoW; //Will output "Laws Of War"

 

  • Like 1

Share this post


Link to post
Share on other sites

@Muzzleflash Thanks. 

I guess this is more complicated than Java but it looks like this is the closest I can get to Enums as I can.

Cheers mate

Share this post


Link to post
Share on other sites
On 12/2/2017 at 1:01 PM, BlacKnightBK said:

Except in sqf it is important to me because for each of my enums I want a String, an Integer and an Array of 2 integers

You can always use the missionConfig (Description.ext) to store your data

//description.ext

class MyData {
	class Vanilla {
		name = "Vanilla";	//name
		integer = 0;		//integer
		array[] = { 0, 1 };	//Array of integers
	};
	class Marksman {
		name = "Marksman";
		integer = 1;
		array[] = { 0, 1 };
	};
	class Helicopters {
		name = "Helicopters";
		integer = 2;
		array[] = { 0, 1 };
	};
	class Jets {
		name = "Jets";
		integer = 3;
		array[] = { 0, 1 };
	};
	class Apex {
		name = "Apex";
		integer = 4;
		array[] = { 0, 1 };
	};
	class Karts {
		name = "Karts";
		integer = 5;
		array[] = { 0, 1 };
	};
	class LawsOfWar {
		name = "Laws Of War";
		integer = 6;
		array[] = { 0, 1 };
	};
};
//initPlayerLocal.sqf

TAG_fnc_getData = {
	params[ "_EnumName" ];

	_name = getText( missionConfigFile >> "MyData" >> _EnumName >> "name" );
	_integer = getNumber( missionConfigFile >> "MyData" >> _EnumName >> "integer" );
	_array = getArray( missionConfigFile >> "MyData" >> _EnumName >> "array" );

	[ _name, _integer, _array ]
};

[ "Vanilla" ] call TAG_fnc_getData params[ "_name", "_integer", "_array" ];

hint format[ "Data:\nName: %1\nInteger: %2\nArray: %3", _name, _integer, _array ];

 

  • Like 2

Share this post


Link to post
Share on other sites

@Larrow - Why do you use missionConfigFile command instead of newer getMissionConfig command? Just wondering is all, is there a specific reason or anything?

Share this post


Link to post
Share on other sites
10 hours ago, HazJ said:

Why do you use missionConfigFile command instead of newer getMissionConfig command?

GetMissionConfig and getMissionConfigValue only return entries from the root. These classes/properties could have possibly been set in the editor, like things that you see on the description.ext  wiki page, which most of now have some for of attributes setting from within the editor which means they are no longer stored in description.ext but in the .sqm.

 

Take with a pinch of salt the comment on the commands page about "(missionConfig) should not be used anymore", unless you are creating something where you are also providing a way from within the editor to set the values then they are never going to be in the .sqm so you might as well use missionConfigFile.

I have not done any test to see if there are any speed advantages from using one over the other.

_name = getText( missionConfigFile >> "MyData" >> _EnumName >> "name" );
_name = getText( getMissionConfig "MyData" >> _EnumName >> "name" );

These two lines do exactly the same thing other than the second will also query the .sqm after the description.ext to see if the class "MyData" exists.

  • Thanks 1

Share this post


Link to post
Share on other sites

See also getDLCs (vanilla) or getMissionDLCs (non-vanilla) commands and remarks.

 

Do you own DLC kart?

getNumber (configFile >> "CfgMods" >> "KART" >> "appId") in (getDLCs 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

×