• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Solved] Set custom variable

Status
Not open for further replies.
Level 11
Joined
Jun 2, 2004
Messages
849
I'm not sure converting a string to a variable reference is possible. A general purpose trigger than converts any "string number" chat message to "variable with name of string = number" probably isn't possible; this isn't python, unfortunately.

You'll have to write up a case for each variable you're using.
 

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,191
It is not possible to set a dynamic variable. All variable names, except some trigger events, require being declared at compile time.

One can simulate the mechanic using a switch of sorts. For example say you want to set the integer variables GoldIncome, WaveNumber or LivesLeft. The following function could be used to set the integer variable specified by a string to a specified value.
JASS:
function SetIntegerVariable takes string varname, integer value returns nothing
    if varname = "GoldIncome" then
        set GoldIncome = value
    elseif varname = "WaveNumber"
        set WaveNumber = value
    elseif varname = "LivesLeft"
        set LivesLeft = value
    else
        // Cannot resolve variable.
        call BJDebugMsg("Invalid variable name!")
    endif
endfunction
Since this is a linear search, the more variables added to it the longer it will take to run. It could easily support hundreds of variables as long as it is called very infrequently. Different such functions would be needed for each type of variable, such as one for strings, one for reals etc. Through use of StringHash it should be possible to use a binary search instead of a linear search for better scaling, however for productive use some form of third party script would need to be created which generates the necessary code.
 
Last edited:
Status
Not open for further replies.
Top