• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[JASS] Store values to a unit

Status
Not open for further replies.
Level 15
Joined
Oct 16, 2010
Messages
941
Alright I updated tesh and its highlighted now but this still crashes my map for some reason:

JASS:
globals
    hashtable Hash_dmg = InitHashtable()
endglobals

If I comment it out it works fine, if I leave it as is the map won't load

I don't reference the hashtable anywhere just trying to declare it right now
 
Level 15
Joined
Oct 16, 2010
Messages
941
Well its not in a library, I want it to be accessible by all libraries and functions since i'll be referring to it in multiple aspects of the game


EDIT - Units have custom stats in my map (magic: increases damage done by spells, magic defense: decreases damage taken from spells, etc.). So what i'm trying to do is build a function that I can send a casting unit, target unit, and real damage amount to and it will factor in the casting units magic and the targeted units magic defense and any other factors that need to be calculated and then deal the end-amount of damage to the targeted unit.

So i'm creating a hashtable that will allow me to save each units magic/magic defense to their ID. I need to be able to access the data in the hashtable in the damage function i'm trying to make as well as modify the data in the hashtable during the course of certain abilities and as a result of gear and/or items that give magic/magic defense bonuses.

So the hashtable needs to be universal - if that made any sense at all (i hope)?
 
Last edited:
Level 18
Joined
Jan 21, 2006
Messages
2,552
If you're just trying to store data to a particular unit you could do it yourself fairly easily.

JASS:
struct unitdata
    real spellMultiplier
    real spellDefense
    static method operator [] takes unit u returns thistype
        return GetUnitUserData(u)
    endmethod
endstruct

... // when a unit enters the map (and the unit's user-data value is 0)
    call SetUnitUserData(GetTriggerUnit(), unitdata.create())
    set unitdata[GetTriggerUnit()].spellDefense= 0.1 // 10%
    set unitdata[GetTriggerUnit()].spellMultiplier = 0.2 // 20%
...

An alternative to this is actually using a unit indexer, which uses the same principal but is probably a lot more developed. The best one I can think of is AutoIndex.

Well its not in a library, I want it to be accessible by all libraries and functions since i'll be referring to it in multiple aspects of the game

You should still put it in a library and explicitly make it a requirement of any other library which you may use it in. Remember that libraries are always placed above scopes, so in the lowest level of your map script it is wiser to use scopes instead of libraries.

EDIT - Units have custom stats in my map (magic: increases damage done by spells, magic defense: decreases damage taken from spells, etc.). So what i'm trying to do is build a function that I can send a casting unit, target unit, and real damage amount to and it will factor in the casting units magic and the targeted units magic defense and any other factors that need to be calculated and then deal the end-amount of damage to the targeted unit.

So i'm creating a hashtable that will allow me to save each units magic/magic defense to their ID. I need to be able to access the data in the hashtable in the damage function i'm trying to make as well as modify the data in the hashtable during the course of certain abilities and as a result of gear and/or items that give magic/magic defense bonuses.

So the hashtable needs to be universal - if that made any sense at all (i hope)?

Using my above example (you could also use a hash-table instead of the user-data):

JASS:
function CasterDamageTarget takes unit caster, unit target, real damage returns nothing
    local real bonusDamage = damage * unitdata[caster].spellMultiplier
    local real negDamage = damage * unitdata[target].spellDefense

    call UnitDamageTarget(caster, target, damage+bonusDamage-negDamage, false, false, /*
        */ ATTACK_TYPE_SPELL, DAMAGE_TYPE_NORMAL, null)
endfunction
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
Well there are three ways I could think of doing this.
  • Use GetUnitUserData / SetUnitUserData natives.
  • Use a hashtable in conjunction with GetHandleId.
  • Use an array and offset GetHandleId by GetHandleId(null) (I think the constant is 0x100008 or 0x100000.

If you're not going to use a unit indexer then it really doesn't matter whether or not you use GetUnitUserData / SetUnitUserData - I believe however that this is one of the fastest ways of storing integer data to a unit.

An array may be faster than the method described above, but it has the limitation of not being able to exceed 8191, without vJass array bounds modifications.

The hashtable would be the slowest, but this option allows compatibility with systems that use the first method described without the size-limit that comes with the second method described.
 
Status
Not open for further replies.
Top