View Full Version : Access window object via dynamic var naming
MadBeardMan
October 6th, 2019, 13:14
Afternoon Folks,
Another one that's getting to me.
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
Andraax
October 6th, 2019, 13:54
Create a table and index into it.
MadBeardMan
October 6th, 2019, 14:47
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
Andraax
October 6th, 2019, 15:47
local myTable = { dice1, dice2, dice3, dice4, dice5 };
local diceToSet = myTable[i];
setDiceIcon(diceToSet, v);
MadBeardMan
October 6th, 2019, 16:34
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!
29303
Now onto allow the Dice to be re-rolled.
Cheers,
MBM
celestian
October 6th, 2019, 17:52
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.
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.
MadBeardMan
October 6th, 2019, 18:04
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.
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).
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
Ikael
October 6th, 2019, 20:03
Just to let you know, you could just do it with
for _,v in pairs(savedDiceRoll) do
i = i + 1;
setDiceIcons(window["dice" .. i], v)
end
no need to prepare the myTable.
MadBeardMan
October 6th, 2019, 20:13
Just to let you know, you could just do it with
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
Ikael
October 6th, 2019, 20:28
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
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
celestian
October 6th, 2019, 20:33
Just to let you know, you could just do it with
for _,v in pairs(savedDiceRoll) do
i = i + 1;
setDiceIcons(window["dice" .. i], v)
end
no need to prepare the myTable.
Yea. That was what I thought seemed like the easiest path. Self or window easier to manage.
MadBeardMan
October 6th, 2019, 20:47
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
Excellent, that did the trick.
Cool, now I understand I can use self if need-be.
Cheers,
MBM
Andraax
October 6th, 2019, 22:35
Amazing! Thanks so very much. Not quite what I'd do in other languages but works a treat!
I do stuff with tables like that in Perl all the time.
Powered by vBulletin® Version 4.2.1 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.