Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[Solved] Set custom variable

Discussion in 'World Editor Help Zone' started by DatnessX, Aug 10, 2016.

  1. DatnessX

    DatnessX

    Joined:
    Dec 6, 2008
    Messages:
    280
    Resources:
    8
    Maps:
    7
    Spells:
    1
    Resources:
    8
    Hi, please help, I am new in W3 Scripting.

    I wanna: set (variable of name(Content of Variable1)) = Variable2
     
  2. KILLCIDE

    KILLCIDE

    Administrator

    Joined:
    Jul 22, 2015
    Messages:
    3,503
    Resources:
    20
    Models:
    2
    Icons:
    10
    Spells:
    7
    Tutorials:
    1
    Resources:
    20
    Use the GUI "Set Variable" function?
     
  3. Avahor

    Avahor

    Joined:
    Sep 29, 2008
    Messages:
    288
    Resources:
    8
    Skins:
    4
    Maps:
    4
    Resources:
    8
    A long journey awaits you...
     
  4. DatnessX

    DatnessX

    Joined:
    Dec 6, 2008
    Messages:
    280
    Resources:
    8
    Maps:
    7
    Spells:
    1
    Resources:
    8
    For example if I write "Goldperscale 4" in chat, then Variable1 = "Goldperscale" and Variable2 = 4

    Then: set Goldperscale = 4
     
  5. KILLCIDE

    KILLCIDE

    Administrator

    Joined:
    Jul 22, 2015
    Messages:
    3,503
    Resources:
    20
    Models:
    2
    Icons:
    10
    Spells:
    7
    Tutorials:
    1
    Resources:
    20
    Oh you need to make use of the Substring function then.

    • Set EnteredChatString = (Entered chat string)
    • Set GoldPerScale = (Substring(EnteredChatString, 14, (Length of EnteredChatString)))


    I chose 14 because there are 13 characters in "Goldperscale "
     
  6. DatnessX

    DatnessX

    Joined:
    Dec 6, 2008
    Messages:
    280
    Resources:
    8
    Maps:
    7
    Spells:
    1
    Resources:
    8
    How to explain this...

    If I write in chat: "lumber 10" then it sets variable with name lumber to 10
    If I write in chat: "gold 200" then it sets variable with name gold to 200
    If there is no variable with name gold or lumber and still I write gold 200 or lumber 10, the variable cant be set.
     
  7. JAKEZINC

    JAKEZINC

    Joined:
    Aug 13, 2013
    Messages:
    1,615
    Resources:
    9
    Spells:
    9
    Resources:
    9
    ^ exactly what KILLCIDE wrote,

    You need a player chat event ( entered as substring ) for lumber and for gold.
     
  8. Kaijyuu

    Kaijyuu

    Joined:
    Jun 2, 2004
    Messages:
    848
    Resources:
    0
    Resources:
    0
    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.
     
  9. Dr Super Good

    Dr Super Good

    Spell Reviewer

    Joined:
    Jan 18, 2005
    Messages:
    26,123
    Resources:
    3
    Maps:
    1
    Spells:
    2
    Resources:
    3
    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.
    Code (vJASS):

    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: Aug 11, 2016