• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Snippet/Template]Attribute

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:
I get the idea, but I'm also using Table for the actual data so I cannot really make it optional, unless I'll convert it to a hashtable...

oh wait, I'm thinking why I even have that getInstance on the first place... I'll just let the user decide on how to handle the linking of an instance... after all, they will be using this in tandem with some other system (it's really useless on it's own), so they can just handle things from there...
 
@Nes - as written, its just a template/wrapper...

It was just meant to centralize the custom data/attribute thingy...

anyway, I could just turn it into a tutorial (which was my original plan)... :)

or hmmmm... I was thinking of making it into a module so that the structs that will use it will be actually running things inside their own...

PS:

we can use the implement keyword inside methods right?
 
Last edited:
Level 16
Joined
Aug 7, 2009
Messages
1,406
I can't really imagine why anyone would want to remove an attribute; why would you want to remove, for example, Integer?

I was also thinking of suggesting a simple AttributeType struct, similar to the one Magtheridon wrote. Using them is quite simple, and faster than StringHash. You just create them and store them in globals. Simple as that.
 
Top