Jump to content

Recommended Posts

Hi all,

 

You'll not find too much topics about hashmap (see Halek's post) and I thought that could be useful to open this specific topic on this command group.

Hashmap is a very powerful tool, not so difficult to script. You have full explanation on links but roughly, you're creating a table (hashmap) in which any call (get) for entry (key) is almost immediate and doesn't depend on the rank of what you're searching.

The hashmap creation can take some times in process, as any other table creation. There is an added value if once in game you need to grab lines repetitively.

I give you a little example:

 

Let's say you want to grab compatibles mags for some primary weapons. There are different way for that. The first good question is to determine if you need these info repeatedly (for multiple units, events... as example):

So, as practical example, for Arma vanilla (there are 376* primary weapons):

 

1st method (no need to create anything) use BIS_fnc_compatibleMagazines (or similar ones):

Let's test some primary Weapons in debug console performance button:

"srifle_DMR_01_F" call BIS_fnc_compatibleMagazines;  //   found in 0,0243 ms: ["10rnd_762x54_mag"]

"sgun_HunterShotgun_01_sawedoff_F" call BIS_fnc_compatibleMagazines; // found in 0,0316 ms: ["2rnd_12gauge_pellets","2rnd_12gauge_slug"]

"arifle_MXM_Hamr_LP_BI_F" call BIS_fnc_compatibleMagazines //   found in 0.0716ms: ["30rnd_65x39_caseless_mag","30rnd_65x39_caseless_khaki_mag","30rnd_65x39_caseless_black_mag","30rnd_65x39_caseless_mag_tracer","30rnd_65x39_caseless_khaki_mag_tracer","30rnd_65x39_caseless_black_mag_tracer","100rnd_65x39_caseless_mag","100rnd_65x39_caseless_khaki_mag","100rnd_65x39_caseless_black_mag","100rnd_65x39_caseless_mag_tracer","100rnd_65x39_caseless_khaki_mag_tracer","100rnd_65x39_caseless_black_mag_tracer"]

 

So, each time you need these mags, you must call the function then wait for the result. The delay depends on multiple factors: rank in cfgWeapons, muzzles, mags... I guess

 

Now, let's "prepare tables":

First of all, primary weapons:

primWpns = ("getNumber (_x/'type') == 1 && {getNumber (_x/'scope') == 2}" configClasses (configfile /"cfgWeapons")) apply {configName _x};

Note*: some weapons don't have picture in gear (inventory) but are playable. Scope =1. You can add them if needed.

then:

CompatibleMagsTable = primWpns apply {[_x, _x call BIS_fnc_compatibleMagazines]};

As result, you'll obtain something like:

[["srifle_DMR_01_F",["10rnd_762x54_mag"]], ["srifle_DMR_01_ACO_F",["10rnd_762x54_mag"]], ["srifle_DMR_01_MRCO_F",["10rnd_762x54_mag"]], ["srifle_DMR_01_SOS_F",["10rnd_762x54_mag"]], ["srifle_DMR_01_DMS_F",["10rnd_762x54_mag"]], ["srifle_DMR_01_DMS_snds_F",["10rnd_762x54_mag"]], ["srifle_DMR_01_ARCO_F",["10rnd_762x54_mag"]],...and so on]

Of course, it takes a long time (27 ms without any mod) to draw this table but you need to do that once, at mission start. So, that doesn't really matter in game. Then, during game:

 

2nd method - use the ready table finding the elements for the entry (weapon):

CompatibleMagsTable select ( primWpns find "srifle_DMR_01_F") select 1 // found in 0.0009 ms

CompatibleMagsTable select ( primWpns find "arifle_MXM_Hamr_LP_BI_F") select 1  // found in 0.038 ms

CompatibleMagsTable select ( primWpns find "sgun_HunterShotgun_01_sawedoff_F") select 1 // found in 0.059 ms

 

Truly, the result depends on the rank of the tested primary weapon inside the result for configClasses (primWpns). The code exits as soon as the weapon is found and there is no difficulty for grabbing the mags.

BUT, if you compare the results for 1st and 2nd methods, is there a great added value in term of average performance ?

Probably yes if your table is more complex, (with multiple things to grab for an entry)...

Anyway, forget this 2nd method.

 

Now, the core of the topic. Hashmaps are introduced since Arma 2.01.

3rd method - Let's create a hashMap, as we created a table above:

Starting with the primary weapon classes as reference:

primWpns = ("getNumber (_x/'type') == 1 && {getNumber (_x/'scope') == 2}" configClasses (configfile /"cfgWeapons")) apply {configName _x};

Now the hashmap where all these classes will be keys of entry:

MGI_Hash_Mags = primWpns createHashMapFromArray (primWpns apply {_x call BIS_fnc_compatibleMagazines});

 

It's very similar to the former table and it takes also 27 ms for preparing it. But...

Now you have a hashmap and you can call what you need like this:

MGI_Hash_Mags get "srifle_DMR_01_F" // found in 0.0007 ms

MGI_Hash_Mags get "arifle_MXM_Hamr_LP_BI_F" // found in 0.0007 ms

MGI_Hash_Mags get "sgun_HunterShotgun_01_sawedoff_F" // found in 0.0011 ms

 

Performance friendly isn't it?

Have fun.

 

 

 

  • Like 11
  • Thanks 5

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

×