Attaching to unit type ids

Zalinian

New Member
Reaction score
0
Sorry I couldn't be more descriptive in the title, but I wasn't sure how to word it.
edit: thanks for changing the title to a more appropriate wording.

Anyway here's the issue:
I originally had multiple arrays used to store units/heroes, the file path for their custom icons(used for my multiboard), bounty from killing them, and several other bits of data. I wanted to change this into a structure, since it will look more organized and make adding new units/heroes into the system, or even entire new branches of data easier. All of the data is set in the code and doesn't change.

For Example:

Instead of

unit array unit_list[2]
integer array unit_bounty[2]

set unit_list[1] = Peasant
set unit_list[2] = Footman
set unit_bounty[1] = 10
set unit_bounty[2] = 15

I would set up a struct:
JASS:
globals
   unit_data Peasant_Data
   unit_data Footman_Data
endglobals

struct unit_data
   integer unit (the unit's integer code)
   integer bounty
endstruct

Then in a function triggered on map initialization:
JASS:
// Peasant
set Peasant_Data.unit = (Peasant's unit integer code)
set Peasant_Data.bounty = 10
// Footman
set Footman_Data.unit = (Footman's unit integer code)
set Footman_Data.bounty = 15

I just need a way to look up the correct struct to obtain the data once I have the unit in question.

Is there a way to return data from a struct using its name in a variable? I can get the name of struct, but I can't get to the data if that makes any sense.
IE: A unit dies. {say the unit is a peon}
set name = GetUnitName(GetDyingUnit()) {this would return the string "Peon"}
The data I need I would have stored in Peon_Data. Is there a way to make that jump?

If anyone has any better ideas for storing my data, I'm open to suggestions.
 

Sevion

The DIY Ninja
Reaction score
413
Why don't you just store the unit type name in the struct.

JASS:
globals
   unit_data Peasant_Data
   unit_data Footman_Data
endglobals

struct unit_data
   integer unit (the unit's integer code)
   integer bounty
   string name
endstruct


JASS:
// Peasant
set Peasant_Data.unit = (Peasant's unit integer code)
set Peasant_Data.bounty = 10
set Peasant_Data.name = "Peasant"//GetUnitName(someunit)
// Footman
set Footman_Data.unit = (Footman's unit integer code)
set Footman_Data.bounty = 15
set Footman_Data.name = "Footman"//GetUnitName(someunit)
 

Zalinian

New Member
Reaction score
0
Why don't you just store the unit type name in the struct.

JASS:
globals
   unit_data Peasant_Data
   unit_data Footman_Data
endglobals

struct unit_data
   integer unit (the unit's integer code)
   integer bounty
   string name
endstruct


JASS:
// Peasant
set Peasant_Data.unit = (Peasant's unit integer code)
set Peasant_Data.bounty = 10
set Peasant_Data.name = "Peasant"//GetUnitName(someunit)
// Footman
set Footman_Data.unit = (Footman's unit integer code)
set Footman_Data.bounty = 15
set Footman_Data.name = "Footman"//GetUnitName(someunit)

I'm confused at how this is supposed to fix the problem :confused:

Use hashtable. Store the unit's code.
I've never used hashtables, or know anything about them. Would you happen to have a link to a tutorial or something please?
 

Sevion

The DIY Ninja
Reaction score
413
Hold on a second. Let me read again. I was distracted >_>

Edit: Yeah. I have no idea wtf I was thinking. Use a Hashtable with keys or a StringHash of the unit names. [ljass]StoreInteger(HASHTABLE, StringHash(GetUnitName(UNIT)), 0, BOUNTY)[/ljass]

Or maybe I'm just being stupid again. I'm really tired >_> And my contacts are raping my eyes.
 

Jesus4Lyf

Good Idea™
Reaction score
397
Based off this:
JASS:
struct TypeData extends array
    integer bounty
    
    //===================================
    // Jesus4Lyf's Hash function.
    private static constant integer HASH_NEXT=53
    private static constant integer MAX_HASH_VALUE=8191
    private static integer array HashedInt
    static method operator [] takes integer int returns thistype
        local integer hash=int-(int/thistype.MAX_HASH_VALUE)*thistype.MAX_HASH_VALUE
        loop
            exitwhen thistype.HashedInt[hash]==int
            if thistype.HashedInt[hash]==0 then
                set thistype.HashedInt[hash]=int
                return thistype(hash)
            endif
            set hash=hash+thistype.HASH_NEXT
            if hash>=thistype.MAX_HASH_VALUE then
                set hash=hash-thistype.MAX_HASH_VALUE
            endif
        endloop
        return thistype(hash)
    endmethod
endstruct

// Example:
local TypeData d=TypeData['hfoo']
set d.bounty=50

local TypeData d=TypeData[GetUnitTypeId(GetTriggerUnit())]
call BJDebugMsg(I2S(d.bounty))

I don't know if it compiles, but this should be about what you want.
If the extending array thing is a problem, I can solve it. But hopefully, it isn't. :)
 

Zalinian

New Member
Reaction score
0
Based off this:
JASS:
struct TypeData extends array
    integer bounty
    
    //===================================
    // Jesus4Lyf's Hash function.
    private static constant integer HASH_NEXT=53
    private static constant integer MAX_HASH_VALUE=8191
    private static integer array HashedInt
    static method operator [] takes integer type returns thistype
        local integer hash=int-(int/thistype.MAX_HASH_VALUE)*thistype.MAX_HASH_VALUE
        loop
            exitwhen thistype.HashedInt[hash]==int
            if thistype.HashedInt[hash]==0 then
                set thistype.HashedInt[hash]=int
                return thistype(hash)
            endif
            set hash=hash+thistype.HASH_NEXT
            if hash>=thistype.MAX_HASH_VALUE then
                set hash=hash-thistype.MAX_HASH_VALUE
            endif
        endloop
        return thistype(hash)
    endfunction
endstruct

// Example:
local TypeData d=TypeData['hfoo']
set d.bounty=50

local TypeData d=TypeData[GetUnitTypeId(GetTriggerUnit())]
call BJDebugMsg(I2S(d.bounty))

I don't know if it compiles, but this should be about what you want.
If the extending array thing is a problem, I can solve it. But hopefully, it isn't. :)

I'm not totally sure if that does it since I've never dealt with hashtables before. Would someone know a good guide/tutorial for it?
 

Sevion

The DIY Ninja
Reaction score
413
What Jesus is doing is giving you a struct array that is indexed by the unit's ID. So the struct instance for a Footman is [ljass]TypeData['hfoo'][/ljass]
 

Jesus4Lyf

Good Idea™
Reaction score
397
>I'm not totally sure if that does it since I've never dealt with hashtables before
It doesn't use hashtables...
Just refer to the example code I posted?

If you follow this link all your problems should be solved, either way...
It allows you to call [LJASS]Hash(unitID)[/LJASS] to convert it to a valid array index that won't clash, and then you can attach a struct using an array...
You can do it with hashtables if you prefer.

Edit: Don't get me wrong, it is exactly what you're after if it compiles, and the code is well tested and works (if it compiles). You don't need to check it. :)
 

Zalinian

New Member
Reaction score
0
Ah sry, I read it too fast. I'll look into it more.

edit: Alright. I don't totally understand it all just yet, but I'm figuring it out. Thanks for the help. :)

edit:
compile errors. I think I caught them.
JASS:
struct TypeData extends array
    integer bounty
    
    //===================================
    // Jesus4Lyf's Hash function.
    private static constant integer HASH_NEXT=53
    private static constant integer MAX_HASH_VALUE=8191
    private static integer array HashedInt
    static method operator [] takes integer type returns thistype // <-- takes integer int
        local integer hash=int-(int/thistype.MAX_HASH_VALUE)*thistype.MAX_HASH_VALUE
        loop
            exitwhen thistype.HashedInt[hash]==int
            if thistype.HashedInt[hash]==0 then
                set thistype.HashedInt[hash]=int
                return thistype(hash)
            endif
            set hash=hash+thistype.HASH_NEXT
            if hash>=thistype.MAX_HASH_VALUE then
                set hash=hash-thistype.MAX_HASH_VALUE
            endif
        endloop
        return thistype(hash)
    endfunction // <-- endmethod
endstruct

It compiles now, testing to see if it works correctly.

Edit...again: Works like a charm.
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
JASS:

library UnitTypeData

    struct UnitTypeData extends array
        private static hashtable ht = InitHashtable()
        
        //! textmacro UNIT_TYPE_DATA takes TYPE, DATA, FUNCTION
        private static key $DATA$Key
        
        method operator $DATA$= takes $TYPE$ which$DATA$ returns nothing
            call Save$FUNCTION$(thistype.ht,this,$DATA$Key,which$DATA$)
        endmethod
        
        method operator $DATA$ takes nothing returns $TYPE$
            return Load$FUNCTION$(thistype.ht,this,$DATA$Key)
        endmethod
        //! endtextmacro
        
        //! runtextmacro UNIT_TYPE_DATA("string", "Name", "Str")
        //! runtextmacro UNIT_TYPE_DATA("integer", "Bounty", "Integer")

    endstruct
    
endlibrary


This has simpler interface. If you want new type of data, use the textmacro.
This is faster than Hash when Hash meets collision.

JASS:

    function Test takes nothing returns nothing
        set UnitTypeData['hpea'].Name = "I am a peasant."
        set UnitTypeData['hpea'].Bounty = 10
    endfunction
 

eXirrah

New Member
Reaction score
51
When I do things like this I always use 1 array and the units type id's for indexes.

for example

JASS:
globals
    integer array UnitsBounty
endglobals
 //footman = 'u000'
// peasant = 'u001'
// knight  = 'u002'
//.....

function UnitDies takes nothing returns nothing
    local player p = GetOwningPlayer(GetDyingUnit())
    local integer i = GetUnitTypeId(GetDyingUnut()) - 'u000'
    local integer g = GetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD)
    call SetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD,g+UnitsBounty<i>)
endfunction

function OnInit takes nothing returns nothing
    set UnitsBounty[0] = 15 // footman bounty &#039;u000&#039;-&#039;u000&#039; = 0
    set UnitsBounty[1] = 20 // peasant bounty &#039;u001&#039;-&#039;u000&#039; = 1
    set UnitsBounty[2] = 25 // knight bounty &#039;u002&#039;-&#039;u000&#039; = 2 
endfunction
</i>


EDIT: As Jesus4Lyf reminded me in order to use my system of typeid indexing you
need to use Jass NewGen pack with enabled Grimoire -> Enable Object Editor Hack
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top