@Meaglyn: So you're saying that 2(or more) keys could have the same associated value? I guess I misunderstood the explanations on the wiki then.(Hash Table topic, workarounds proposed in the example associating person names(key) to their phone number(value))
@ShaDoOoW: I'd like to build a cache holding some damage-related data. The cache will be queried very often, most of the time in loops, so, for instance, querying 2das or a DB is dubious considering the required data access speed(on-hit events etc...). The cached data comes from iprp_damagecost.2da(CEP version), associating damage rank to its average. So I had thought of doing this, dunno if it makes sense:
// Meant to be called once, in module load event
void CacheDmg()
{
int nLast = 158; // last index in the 2DA file
int nNth, nNum, nDie, nToDivide;
object oMod = GetModule();
string sKey;
HashSetCreate(oMod, "DMG", ++nLast);
for(nNth; nNth < nLast; nNth++)
{
sKey = Get2DAString("iprp_damagecost", "Rank", nNth); // using the rank as the key
nNum = StringToInt(Get2DAString("iprp_damagecost", "NumDice", nNth));
nDie = StringToInt(Get2DAString("iprp_damagecost", "Die", nNth));
if(!nNum) nToDivide = nDie*2;
else nToDivide = nNum+nNum*nDie;
HashSetSetLocalInt(oMod, "DMG", sKey, nToDivide); // floats cannot be stored and integers are fast
}
}
Then, to retrieve, say, avg damage from 2da rank 8:
float fAvgDmg = HashSetGetLocalInt(GetModule(), "DMG", "8")/2.0; // fAvgDmg = 5.0
Or retrieve avg damage from 2da index 8: (assuming that the key order does not change)
float fAvgDmg = HashSetGetLocalInt(GetModule(), "DMG", HashSetGetNthKey(9))/2.0; // fAvgDmg = 4.5
Kato
Modifié par Kato_Yang, 04 septembre 2012 - 08:33 .