Jump to content
giallustio

From display name to classname

Recommended Posts

How can i check the classname from the display name?

For example i have "6.5mm 30Rnd Tracer (Green) Mag", how could return it:

"30Rnd_65x39_caseless_green_mag_Tracer"

Share this post


Link to post
Share on other sites

Sorry for the necro, but I think the op wants to reference the correct className for an specific item from the display name. Did the OP find a solution? I basically need the same thing. I need to reference the correct classname of an item from the display name. How is this possible?

Share this post


Link to post
Share on other sites

I came up with one possible solution (Documented heavily-ish for anyone who stumbles upon this page):

/*
Syntax:
   [_name] call _fnc_getFromName;
   [_name, _defaultValue] call _fnc_getFromName;

Return Type:
   STRING (Classname of corresponding DisplayName, if found)

Additional Note:
   Simply traverses CfgWeapons and CfgMagazines, and checks every entree to see if its what we're looking for.
   Returns the classname or an optional default value (Default default value is "ERROR: Item not found").

   Features Modifications by Iceman77
*/
_fnc_getFromName = {
   private["_this","_displayName","_default","_out","_cfgWeapons","_cfgMagazines"]; 

   _displayName = _this select 0; 
   _default = [_this, 1, "ERROR: Item not found"] call BIS_fnc_param; 
   _out = _default; 
   _cfgWeapons = configFile >> "CfgWeapons"; 
   _cfgMagazines = configFile >> "CfgMagazines"; 

   for "_i" from 0 to count (_cfgWeapons) - 1 do { 
       if ((isClass (_cfgWeapons select _i)) && {_displayName == getText(_cfgWeapons select _i >> "displayName")}) exitWith { 
           _out = configName (_cfgWeapons select _i); 
       }; 
   }; 
   if (_out != _default) exitWith {_out}; 
   for "_i" from 0 to count (_cfgMagazines) - 1 do { 
       if ((isClass (_cfgMagazines select _i)) && {_displayName == getText(_cfgMagazines select _i >> "displayName")}) exitWith { 
           _out = configName(_cfgMagazines select _i); 
       }; 
   }; 
   _out  
};  

Basically, it'll just iterate through CfgMagazines and CfgWeapons until it finds the displayName it's looking for. It'll take an optional second parameter as a default value, or returns "ERROR: Item not found" as the default default value. It's not perfect, but I think it does its job well enough.

It probably goes without saying, but anyone can feel free to use/modify/etc this as they need!

Edited by Hypnomatic
Updated Code to most recent variant

Share this post


Link to post
Share on other sites

That's a fantastic function and I can't thank you enough. I altered it very slightly to avoid >> Warning Message: 'access/' is not a class ('displayName' accessed)

Hypno_fnc_getName = {
//Modified by Iceman77
   private["_this","_name","_default","_out","_cfgWeapons","_cfgMagazines"];

   _displayName = _this select 0;
   _default = [_this, 1, "ERROR: Item not found"] call BIS_fnc_param;
   _out = _default;
   _cfgWeapons = configFile >> "CfgWeapons";
   _cfgMagazines = configFile >> "CfgMagazines";

   for "_i" from 0 to count (_cfgWeapons) - 1 do {
    if (isClass (_cfgWeapons select _i)) then {
		if (_displayName == getText(_cfgWeapons select _i >> "displayName")) exitWith {
			_out = configName (_cfgWeapons select _i);
		};
	};
   };
   if (_out != _default) exitWith {_out};
   for "_i" from 0 to count (_cfgMagazines) - 1 do {
    if (isClass (_cfgMagazines select _i)) then {
		if (_displayName == getText(_cfgMagazines select _i >> "displayName")) exitWith {
			_out = configName(_cfgMagazines select _i);
		};
	};
   };
   _out 
};  

Edited by Iceman77

Share this post


Link to post
Share on other sites

Glad to be of help! Also good point on that error message; I got it once on my first run, then forgot to restart the mission to see if it was still happening when I finished the rest of the code.

There is one minor modification I would make to your changes however; Basically combine your new check into the old check and use ARMA's clunky Lazy evaluation. It doesn't effect the actual output of this function at all, but I think the exitWiths inside the loop won't properly break the loop if they're nested inside another if statement. Not 100% positive that it actually will be behaving like that, and can't test it now, but it may be worth considering it as:

Hypno_fnc_getName = {
//Modified by Iceman77
   private["_this","_displayName","_default","_out","_cfgWeapons","_cfgMagazines"];

   _displayName = _this select 0;
   _default = [_this, 1, "ERROR: Item not found"] call BIS_fnc_param;
   _out = _default;
   _cfgWeapons = configFile >> "CfgWeapons";
   _cfgMagazines = configFile >> "CfgMagazines";

   for "_i" from 0 to count (_cfgWeapons) - 1 do {
       if ((isClass (_cfgWeapons select _i)) && {_displayName == getText(_cfgWeapons select _i >> "displayName")}) exitWith {
           _out = configName (_cfgWeapons select _i);
       };
   };
   if (_out != _default) exitWith {_out};
   for "_i" from 0 to count (_cfgMagazines) - 1 do {
       if ((isClass (_cfgMagazines select _i)) && {_displayName == getText(_cfgMagazines select _i >> "displayName")}) exitWith {
           _out = configName(_cfgMagazines select _i);
       };
   };
   _out 
};

Also as far as naming conventions go, I tend to use hyp_ instead of Hypno_ just because I'm lazy and like to type as little as possible, but whatever you use is entirely up to you. Heck I wouldn't be offended if you just decided to remove the Hypno_/hyp_ from the name entirely if it's more convenient for you.

EDIT: Just noticed that _name wasn't updated to _displayName in the private[];. Likely wouldn't ever cause issues, but worth fixing to be safe.

Share this post


Link to post
Share on other sites

After I posted I removed the private entirely hehe. Thanks much for the updated version :wave:

Share this post


Link to post
Share on other sites

@ Giallustio - If you need to reference the classname from display name from an listbox or combo selection, you can simply use lbSetData and lbData instead. I was going to use Hypnotics wonderful function for this, but turns out, those two commands are what I needed and ended up using (thanks to Larrow's suggestion).

Share this post


Link to post
Share on other sites

Sorry for the necro, didn't want to create new topic for this:

First of all, thanks guys, that code helped me a lot at first, since lbData only returns magazines and does not return weapons and items from gear menu.

I wrote a faster alternative to script above (Arma3 v1.24 and up), added cfgVehicles following the same easy pattern.

fnc_g_findClassname = {
/*
Note: Feel free to add more classes to watch out for following the pattern

Usage:
(_displayName) call fnc_g_findClassname; //returns className as a string

Example:
"RGO Grenade" call fnc_g_findClassname; //returns "HandGrenade"
*/
private "_findClassname";

_findClassname = "getText (_x >> 'displayName') == _this" configClasses (configFile >> "cfgWeapons");
if (count _findClassname > 0) exitWith {configName (_findClassname select 0)};

_findClassname = "getText (_x >> 'displayName') == _this" configClasses (configFile >> "cfgMagazines");
if (count _findClassname > 0) exitWith {configName (_findClassname select 0)};

_findClassname = "getText (_x >> 'displayName') == _this" configClasses (configFile >> "cfgVehicles");
if (count _findClassname > 0) exitWith {configName (_findClassname select 0)};

_findClassname = format ["Item %1 not found",_this];
_findClassname

};

Hope this helps anyone landing here trough google search.

Share this post


Link to post
Share on other sites

Thanks Raymix and others, this works well for what I was looking for.

Share this post


Link to post
Share on other sites

I have an issue.
The mod rhsusaf adds a Stryker with a tow missile.

The issue is, it has two variants with the same display name, and when I use this function, it return "rhsusf_stryker_m1134_base", but the two variants are "rhsusf_stryker_m1134_d" and "rhsusf_stryker_m1134_wd".

Edit: 
I figured out, by changing the "select 0" to "select 1" on this line it works fine:

if (count _findClassname > 0) exitWith {configName (_findClassname select 1)};

 

Edited by Coucoul38
find the solution

Share this post


Link to post
Share on other sites

These configs can be huge!, especially cfgVehicles without any filter...

It's probably time for creating hashmaps, if you want to use this function multiple times (for whatever reason):

 

private _values = "getNumber (_x >> 'scope') ==2" configClasses (configFile / "cfgVehicles");
private _keys = _values apply {getText (_x /"displayName")};
private _hashMapForVehicles = (_keys createHashMapFromArray (_values apply {configName _x}));

 

will return something like [...["Sniper (Ghillie)","AFMC_sfM21G"],["Medkit [CSLA]","CSLA_MediKit_Z80_wh"],...]

Feel free to add filters for land vehicles or air or whatever you need.

Advantage? See how to use hashmaps here.

 

 

  • Like 1

Share this post


Link to post
Share on other sites

Oh yeah, basically I'm making a gun game, but with vehicles, and I'm storing the displayName of the vehicles I want the players to get in a list.
So each time the player gets a kill, it would go through the whole cfgVehicle to find the configName, so definitely not optimized.

9 hours ago, pierremgi said:

 


private _values = "getNumber (_x >> 'scope') ==2" configClasses (configFile / "cfgVehicles");
private _keys = _values apply {getText (_x /"displayName")};
private _hashMapForVehicles = (_keys createHashMapFromArray (_values apply {configName _x}));

 

I will use a hashMap as you said, but could you please explain to me what these lines you shared here do exactly? 
Thanks for replying 🙏

Share this post


Link to post
Share on other sites

It's just a mean for building an "array" made of elements like [displayName of any vehicle (editable), typeOf this vehicle] . This array is a hashmap, so you can very quickly pick a value (type of) when you enter a key (display name).

NOTE: I think you need the contrary (keys should be made of "type of" and value should be "display name"... but I didn't know what you were doing.

... but this topic stays from display name to class name (type of). So, my answer was consistent with previous posts.

 

As rule, you should open a new topic for your quest if you don't find what you exactly need on forum. To ease your quest, if  you need to display the display name on kill, you don't need any array for that...

Just something like:

addMissionEventHandler ["entityKilled", {
  params ["_unit", "_killer", "_instigator", "_useEffects"];
  if (isNull _instigator) then {
    _instigator = UAVControl vehicle _killer select 0;
  };
  if (isNull _instigator) then { _instigator = _killer };
   hint format [" a %1 has been Killed By %2", getText (configFile / "cfgVehicles" / typeOf vehicle _unit / "displayName"), name _instigator];
}];

You can test it in a simple trigger (paste it in activation field),  set the trigger condition to TRUE (instead of this , so the trigger fires at start and the code runs).

 

 

 

Share this post


Link to post
Share on other sites

Well thanks for the answer, but its not really what I'm looking for, I already have a killfeed displaying the names of the players and the killer's vehicle.
What I'm trying to do is this:

I have a list of vehicles, and I want to change the player's vehicle depending on his score :

vehiclesList = ["M2A3","M1134"];

//CREATE new vehicle
_vehicleDisplayName = vehiclesList select _playerScore;
_vehicleClassName = _vehicleDisplayName call fnc_g_findClassname;
_veh = _vehicleClassName createVehicle _vehPosition;
_veh lock true;

This is working, but it goes through all the config to find the className of the vehicles, so that's why I thought it was a good idea to use a hashMap, I just don't understand how I should set it up.
 

Share this post


Link to post
Share on other sites
On 5/10/2015 at 10:50 PM, raymix said:

Sorry for the necro, didn't want to create new topic for this:

First of all, thanks guys, that code helped me a lot at first, since lbData only returns magazines and does not return weapons and items from gear menu.

I wrote a faster alternative to script above (Arma3 v1.24 and up), added cfgVehicles following the same easy pattern.

 


fnc_g_findClassname = {
/*
Note: Feel free to add more classes to watch out for following the pattern

Usage:
(_displayName) call fnc_g_findClassname; //returns className as a string

Example:
"RGO Grenade" call fnc_g_findClassname; //returns "HandGrenade"
*/
private "_findClassname";

_findClassname = "getText (_x >> 'displayName') == _this" configClasses (configFile >> "cfgWeapons");
if (count _findClassname > 0) exitWith {configName (_findClassname select 0)};

_findClassname = "getText (_x >> 'displayName') == _this" configClasses (configFile >> "cfgMagazines");
if (count _findClassname > 0) exitWith {configName (_findClassname select 0)};

_findClassname = "getText (_x >> 'displayName') == _this" configClasses (configFile >> "cfgVehicles");
if (count _findClassname > 0) exitWith {configName (_findClassname select 0)};

_findClassname = format ["Item %1 not found",_this];
_findClassname

};
For some reason, this doesn't work with vehicles from the Project RACS mod.

 

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

×