Page 1 of 2 12 Last
  1. #1

    Access window object via dynamic var naming

    Afternoon Folks,

    Another one that's getting to me.

    Code:
        for _,v in pairs(savedDiceRoll) do        
            i = i + 1;
            if i == 1 then
                setDiceIcons(["dice" .. 1], v)
            elseif i == 2 then
                setDiceIcons(dice2, v)
            elseif i == 3 then
                setDiceIcons(dice3, v)
            elseif i == 4 then
                setDiceIcons(dice4, v)
            elseif i == 5 then
                setDiceIcons(dice5, v)
            elseif i == 6 then
                setDiceIcons(dice6, v)
            elseif i == 7 then
                setDiceIcons(dice7, v)
            elseif i == 8 then
                setDiceIcons(dice8, v)
            elseif i == 9 then
                setDiceIcons(dice9, v)
            elseif i == 10 then
                setDiceIcons(dice10, v)
            else
            end
    
        end
    That code is quite crude, but I cannot figure out a way to dynamically access an in window control. This window has '10' dice controls and I'm trying to change their icons (to match the original dice rolls). It's all working if I do it long-handed, but there must be an easier way (like in the setDiceIcons(["dice" .. 1], v) which'd work in JS etc).

    Any thoughts on how to do this would be ace!

    Cheers,
    MBM

  2. #2

    Join Date
    Jun 2013
    Location
    Isanti, MN
    Posts
    2,814
    Create a table and index into it.

  3. #3
    Quote Originally Posted by Andraax View Post
    Create a table and index into it.
    Hi Chap,

    So to understand you. Iterate through the XML and create a table of the dice to update and the icon state?

    That can't work as the dice needs to be an object, ie dice1 and not 'dice1'.

    Are you able to explain further?

    Cheers,
    MBM

  4. #4

    Join Date
    Jun 2013
    Location
    Isanti, MN
    Posts
    2,814
    Code:
    local myTable = { dice1, dice2, dice3, dice4, dice5 };
    local diceToSet = myTable[i];
    setDiceIcon(diceToSet, v);

  5. #5
    Quote Originally Posted by Andraax View Post
    Code:
    local myTable = { dice1, dice2, dice3, dice4, dice5 };
    local diceToSet = myTable[i];
    setDiceIcon(diceToSet, v);
    Amazing! Thanks so very much. Not quite what I'd do in other languages but works a treat!

    WillpowerRolls.jpg

    Now onto allow the Dice to be re-rolled.

    Cheers,
    MBM

  6. #6
    celestian's Avatar
    Join Date
    Jan 2017
    Location
    DFW, Texas
    Posts
    2,011
    Sounds like you already have a solution but I thought I'd throw out the "getControls()" option for at least review. I found it quiet useful. You can grab all the controls within a window/scope and match them how you like.

    You can also use something like this perhaps.

    Code:
        local sMatrixNumberName = "thac" .. i; -- control name for the THACO label
        local cnt = self[sMatrixNumberName];   -- get the control for this, stringcontrol named thac-10 .. thac10
        cnt.setValue(nTHAC); -- set new to hit AC value

    I've a hard time following exactly what you're trying to go for so it might be off in left field but I thought I would mention it.
    ---
    Coding the Official AD&D Ruleset
    My Twitch Channel for AD&D and FG related streams (See schedule for live days)
    My YouTube for FG related Tutorials and AD&D Actual Plays

  7. #7
    Quote Originally Posted by celestian View Post
    Sounds like you already have a solution but I thought I'd throw out the "getControls()" option for at least review. I found it quiet useful. You can grab all the controls within a window/scope and match them how you like.

    You can also use something like this perhaps.

    Code:
        local sMatrixNumberName = "thac" .. i; -- control name for the THACO label
        local cnt = self[sMatrixNumberName];   -- get the control for this, stringcontrol named thac-10 .. thac10
        cnt.setValue(nTHAC); -- set new to hit AC value

    I've a hard time following exactly what you're trying to go for so it might be off in left field but I thought I would mention it.
    That's actually what I was after, the 'self' part returns the var as an object, which is what I was wanting.

    I went with Andraax's suggestion and it's just as good, thanks though, it's great to know what I want to do, can be done.

    Here's my final code, this populates a window with 10 dice and sets them to the correct dice graphic (as shown in my last post).

    Code:
    function onInit()
    
        -- let's read the saved 
        nodeWin = getDatabaseNode()
    
        local savedDiceRoll = DB.getChildren(nodeWin, 'savedroll')
        local i = 0;
        local myTable = { dice1, dice2, dice3, dice4, dice5, dice6, dice7, dice8, dice9, dice10 };
        
        for _,v in pairs(savedDiceRoll) do
            i = i + 1;
            local diceToSet = myTable[i];
            setDiceIcons(diceToSet, v);
        end
    end
    
    function setDiceIcons(dice, v)
    
        local result = DB.getValue(v, 'result', 0)
    
        if result == 10 then
            dice.setStateIcons(0, "wp_spend_critical")
            dice.setStateIcons(1, "wp_spend_critical_check")
        elseif result >= 6 then
            dice.setStateIcons(0, "wp_spend_success")
            dice.setStateIcons(1, "wp_spend_success_check")
        else   
            dice.setStateIcons(0, "wp_spend")
            dice.setStateIcons(1, "wp_spend_check")
        end
        dice.setVisible(true)
    end
    Cheers,
    MBM

  8. #8
    Ikael's Avatar
    Join Date
    Jan 2008
    Location
    Finland/Joensuu
    Posts
    2,025
    Just to let you know, you could just do it with

    Code:
    for _,v in pairs(savedDiceRoll) do        
        i = i + 1;
        setDiceIcons(window["dice" .. i], v)
    end
    no need to prepare the myTable.
    "Alright, you primitive screwheads, listen up: THIS... is my BOOMSTICK!" -- Ash Williams, Army of Darkness

    Post your SavageWorlds ruleset feature requests and issue reports here!

    Enhance your core Savage Worlds ruleset with SW Enhancement Extensions, get more to savage!
    Want to create SW NPCs quickly? Use SW NPC Maker extension!
    Want to prove your dices are cursed or blessed? Try out SW Roll Statistics

  9. #9
    Quote Originally Posted by Ikael View Post
    Just to let you know, you could just do it with

    Code:
    for _,v in pairs(savedDiceRoll) do        
        i = i + 1;
        setDiceIcons(window["dice" .. i], v)
    end
    no need to prepare the myTable.
    Hi Ikael,

    Thanks for that suggestion, I tried something similar before and got the error:

    Script Error: [string "campaign/scripts/charsheet_willpower_spend...."]:18: attempt to index global 'window' (a nil value)

    Which I don't understand as we are inside a window....

    Cheers,
    MBM

  10. #10
    Ikael's Avatar
    Join Date
    Jan 2008
    Location
    Finland/Joensuu
    Posts
    2,025
    It seems the script run in window-level in which case there is no such thing as "window" which is used reference parent of controller. Try replacing that window word with self. It should do the trick

    Quote Originally Posted by MadBeardMan View Post
    Hi Ikael,

    Thanks for that suggestion, I tried something similar before and got the error:

    Script Error: [string "campaign/scripts/charsheet_willpower_spend...."]:18: attempt to index global 'window' (a nil value)

    Which I don't understand as we are inside a window....

    Cheers,
    MBM
    "Alright, you primitive screwheads, listen up: THIS... is my BOOMSTICK!" -- Ash Williams, Army of Darkness

    Post your SavageWorlds ruleset feature requests and issue reports here!

    Enhance your core Savage Worlds ruleset with SW Enhancement Extensions, get more to savage!
    Want to create SW NPCs quickly? Use SW NPC Maker extension!
    Want to prove your dices are cursed or blessed? Try out SW Roll Statistics

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Log in

Log in