Dirac
22710180
- Reaction score
- 147
This can be very useful, and it's quite simple (description at code)
Reasons of why this snippet:
- Let's say you're trying to handle multiple units in a single spell, instead of occupying 1 instance per unit, like AIDS would do, you store all the units in the same instance avoiding creating a doubly linked list and the extra instance creation.
- Allows you to decide when to index the unit
- Allows you to index dead units (corpses)
Changelog:
Reasons of why this snippet:
- Let's say you're trying to handle multiple units in a single spell, instead of occupying 1 instance per unit, like AIDS would do, you store all the units in the same instance avoiding creating a doubly linked list and the extra instance creation.
- Allows you to decide when to index the unit
- Allows you to index dead units (corpses)
JASS:
library MIAS //Multiple Instance Attachment System
//by Dirac
//this snippet alows you to attach multiple units to each instance of each struct.
//you can't attach the same unit to 2 different instances of the same struct.
//it does not modify the UnitUserData
//how to use?:
// Implement MIA at the top of your struct
// -method IndexUnit takes unit u returns integer
// Indexes the unit given to your struct instance, use the operator to return the
// instance. Ex:
// yourstruct[whichUnit]=instance the unit is attached to
globals
private hashtable MIAh=InitHashtable()
private integer COUNT=0
endglobals
module MIA
private static integer struct_index
private static method onInit takes nothing returns nothing
set COUNT=COUNT+1
set thistype.struct_index=COUNT
endmethod
private static method Flush takes nothing returns nothing
call RemoveSavedInteger(MIAh,thistype.struct_index,GetHandleId(GetEnumUnit()))
endmethod
private method onDestroy takes nothing returns nothing
call ForGroup(LoadGroupHandle(MIAh,-thistype.struct_index,this),function thistype.Flush)
call RemoveSavedHandle(MIAh,-thistype.struct_index,this)
endmethod
method IndexUnit takes unit u returns integer
local integer HID=GetHandleId(u)
if HaveSavedHandle(MIAh,-thistype.struct_index,this)==false then
call SaveGroupHandle(MIAh,-thistype.struct_index,this,CreateGroup())
endif
if HaveSavedInteger(MIAh,thistype.struct_index,HID)==false then
call GroupAddUnit(LoadGroupHandle(MIAh,-thistype.struct_index,this),u)
call SaveInteger(MIAh,thistype.struct_index,HID,this)
endif
return HID
endmethod
static method operator[] takes unit u returns thistype
return LoadInteger(MIAh,thistype.struct_index,GetHandleId(u))
endmethod
endmodule
endlibrary
Changelog:
v0.01
-Initial Release
v0.02
-Instead of Unit Groups uses a second hashtable to deattach units to structs
-Has safety methods to prevent indexing the same unit twice
v0.03
-Removed the use of a second hashtable
-This can now be used in array structs