Troll-Brain Ri1kamoua
Inscrit le: 23 Aoû 2007 Messages: 7143 Sujets: 147 Spécialité en worldedit: le troll, le flood, la vulgarité, mon coeur balance Médailles: 2 (En savoir plus...)
|
Posté le: 04/01/10 16:46 Sujet du message: [vJass] IsUnitTypeId |
|
|
Nom de la fonction : IsUnitTypeId
Créateur : Troll-Brain
Fonctions requises : Table
Code : Jass: | library IsUnitTypeId requires Table
/* The point of this library is to bypass a nasty bug, but it's only present
with loaded game, if you don't care about them then you would just inline
UnitId2String(yourUnitTypeId) != null instead of using this library */
private keyword s_dummy
private function GameLoaded takes nothing returns boolean
set s_dummy.is_game_loaded = true
call DestroyBoolExpr(Condition(function GameLoaded))
call DestroyTrigger(GetTriggeringTrigger())
return false
endfunction
// coz of vJass madness initializer module priority, even if i hardly see the usefulness of registring a such event on a module initializer ...
// but in some case this kind of initializer can be "handy", here we avoid an unecessary init
private module onInit
static method onInit takes nothing returns nothing
local trigger trig = CreateTrigger()
call TriggerRegisterGameEvent(trig,EVENT_GAME_LOADED)
call TriggerAddCondition(trig,Condition(function GameLoaded))
set trig = null
set s_dummy.tab = Table.create()
endmethod
endmodule
private struct s_dummy extends array // the extends array avoid some unneeded stuff
static Table tab
static boolean is_game_loaded = false
implement onInit
endstruct
function IsGameLoaded takes nothing returns boolean
return s_dummy.is_game_loaded
endfunction
function IsUnitTypeId takes integer unitid returns boolean
local unit u
if IsGameLoaded() then
if s_dummy.tab.exists(unitid) then
return s_dummy.tab[unitid] != 0
endif
// IsUnitIdType is not enough , IsUnitHeroId only check if the first letter of the id is an uppercase and not if the id is a valid hero id
// so i've figured only this way, or you can use a third party tool instead, like GMSI, but imho it's overskilled here
set u = CreateUnit(Player(13),unitid,0.,0.,0.)
if u != null then
call RemoveUnit(u)
set u = null
set s_dummy.tab[unitid] = 1
return true
else
set s_dummy.tab[unitid] = 0
return false
endif
endif
return UnitId2String(unitid) != null // always returns null when the game was saved and loaded, and yes it's a bug
endfunction
endlibrary |
Copyright : Libre
Remarques : Blizzard ne nous a pas fournit une fonction native permettant de savoir si un entier est un type d'unité valide.
On pourrait en théorie simplement se servir de UnitId2String et vérifier que le résultat est différent de null, mais c'est sans compter un evil bug.
UnitId2String renvoie toujours null quand il s'agit d'une partie sauvegardée et chargée.
Uniquement dans le cas d'une partie sauvegardée, la première fois que l'entier sera vérifié, une unité sera créé, mais instantanément remove.
Je me sers de cette librairie pour une autre bien plus utile, que je n'ai pas encore achevée.
Mais globalement je m'en sert pour savoir si un ordre reçu est un ordre de construction ou non. _________________
Le violet, c'est moche. |
|