- Joined
- Oct 16, 2008
- Messages
- 10,454
JASS:
/*
Attribute v1.0
by Adiktuz
Basically it's a template/wrapper of sorts which allows you to easily
create custom attributes/data
Well, it just allows you to make around unlimited amount of custom data/attribute that can be tied with objects, like units (via their HandleId or UnitTypeId) etc, without making variables
For now it only supports integer datatype
How to use:
First we create an instance of Attribute by using
Attribute.new(integer id)
where: id = a certain pointer that becomes linked with the created instance
of Attribute (an example would be the handle id of a unit)
Then we can start adding/modifying custom attributes/data from an instance
.modify(string attribute, integer value, boolean toSet)
where:
string attribute = the name of the "attribute/data" that you want to change/set
integer value = the value to be added/set
boolean toSet = whether the value will be added to the current value of the data or
if the data should be set into that value
To get the value of a data/attribute, we use:
.get(string attribute)
To clear an instance, we use:
.clear()
*/
library Attribute requires Table
struct Attribute extends array
Table dataTable
private static integer instanceCount = 0
private static thistype recycle = 0
private thistype recycleNext
method clear takes nothing returns nothing
call this.dataTable.flush()
set .recycleNext=recycle
set recycle=this
endmethod
method modify takes string attribute, integer value, boolean toSet returns integer
local integer intkey = StringHash(attribute)
local integer att = this.dataTable[intkey]
if toSet then
set this.dataTable[intkey] = value
else
set this.dataTable[intkey] = att + value
endif
return this.dataTable[intkey]
endmethod
method get takes string attribute returns integer
return this.dataTable[StringHash(attribute)]
endmethod
static method new takes integer id returns thistype
local thistype this = recycle
if (this == 0) then
set instanceCount = instanceCount + 1
return instanceCount
endif
set recycle = recycle.recycleNext
if this.dataTable < 0 then
set this.dataTable = Table.create()
endif
return this
endmethod
implement init
endstruct
endlibrary
Note: I didn't get to test it so I ain't sure if it's working, but at the very least, it compiles without problems.
Last edited: