library test initializer Init
globals
private hashtable hash = InitHashtable()
endglobals
function AddComment takes integer unitType, string stringKey, string whichString returns nothing
local integer stringId = StringHash(stringKey)
local integer commentTypeAmount // How many categories/types of a comment does a unitType currently have
local integer commentIdAmount = LoadInteger(hash, unitType, stringId) + 1 // How many same types does it have
if(commentIdAmount == 1) then
// That means a new category/type of comment has been added
set commentTypeAmount = LoadInteger(hash, unitType, 0) + 1
call SaveInteger(hash, stringId, unitType, commentTypeAmount)
call SaveInteger(hash, unitType, 0, commentTypeAmount)
else
set commentTypeAmount = LoadInteger(hash, unitType, 0)
endif
call SaveStr(hash, unitType, stringId*commentTypeAmount + commentIdAmount, whichString)
call SaveInteger(hash, unitType, stringId, commentIdAmount)
endfunction
function GetComment takes integer unitType, string stringKey, integer index returns string
local integer stringId = StringHash(stringKey)
local integer i
if (index != 0) then
return LoadStr(hash, unitType, stringId*LoadInteger(hash, stringId, unitType) + index)
else
set i = GetRandomInt(1, LoadInteger(hash, unitType, stringId))
return LoadStr(hash, unitType, stringId*LoadInteger(hash, stringId, unitType) + i)
endif
endfunction
private function Init takes nothing returns nothing
local string s
call AddComment('hfoo', "Hi", "Hello 1")
call AddComment('hfoo', "Hi", "Hello 2")
call AddComment('hfoo', "Fight", "Fight 1")
call AddComment('hfoo', "Fight", "Fight 2")
call AddComment('hfoo', "Else", "Just something else")
set s = GetComment('hfoo', "Hi", 2)
call BJDebugMsg(s) // Will print "Hello 2"
set s = GetComment('hfoo', "Fight", 1)
call BJDebugMsg(s) // Will print "Fight 1"
set s = GetComment('hfoo', "Else", 0)
call BJDebugMsg(s) // Will print "Just something else"
set s = GetComment('hfoo', "Hello", 0)
call BJDebugMsg(s) // Will print "Hello 1" or "Hello 2"
endfunction
endlibrary