PDA

View Full Version : Text Cycler



Maspalio
October 17th, 2016, 17:43
Hey,

Me again. Do someone know if it would be possible to create a text cycler in a way the button allows to cycle a text somewhere else? The CoreRPG already has an icon cycler and a string cycler. But both of them cycle things only where you click : label, or icon. I want it to apply the cycle elsewhere.

15736

Trenloe
October 17th, 2016, 17:56
It's possible, but it is going to require some coding to do - with some gotchas (mentioned below).

You could tie the control "somewhere else" directly to the button in question and have the button onValueChanged (https://www.fantasygrounds.com/refdoc/buttoncontrol.xcp#onValueChanged)event trigger changes in the "elsewhere" control.

Or, you could create some code (template) for a button whose onButtonPress (https://www.fantasygrounds.com/refdoc/buttoncontrol.xcp#onButtonPress) event runs the relevant code to update another control. You'd have to come up with a flexible (i.e. not hard coded) way to specify the control somewhere else you wanted to cycle - which would probably be the difficult bit as FG control references can't be easily referenced from string references. So, you'd need to basically write a control template (definition) that would create the button and the associated "other" control, or hard code the linked control reference in the onInit function of the control itself (when you create the control in XML).

Both of these options would require the "elsewhere" control to be in the same GUI window (PC sheet, NPC sheet, etc.) as it's virtually impossible to do this reliably across different stand-alone FG windows.

How much coding you did would depend on if this was a one-off (hard coded) or if you wanted to create a control template that you wanted to re-use again and again.

Maspalio
October 17th, 2016, 18:21
Thanks Trenloe, it's perfect. I'll try that. Actually i'll need only two occurrences on the same GUI window.

Maspalio
October 18th, 2016, 16:18
It's possible, but it is going to require some coding to do


Well, i'm sorry, i have to ask for some help again if one of the gurus around can look at the code. I'm close, but not done yet. What i'm trying to do is : click on a button, then swap the word next to it, and apply another color.

It's ok for the button to change the color - though it's actually set on another word, but it works. However, i can't swap the text. Console keeps spamming this error message :
script error : [string "instinctcycler"]:1 : attempt to call field 'setText' (a nil value)

I know i'm doing this wrong, due to a lack of programming skill. But it may be obvious for you if you look at it?

Thanks by advance.


The result expected
15751
15752




The button cycler's code


<button_iconcycler name="instinctcycler"> -- using the CoreRPG template and lua attached
<anchored to="selfcontrollist" position="insidetopright" offset="30,-2" width="14" height="14" />
<font>sheetlabel</font>
<center />
<tooltip text="Sef-Control or Instinct" />
<parameters>
<defaulticon>button_blank</defaulticon>
<icons>button_blank_down</icons>
<defaultvalue>0</defaultvalue>
<values>1</values>
<defaultlabel>Self_control</defaultlabel>
<labels>Instinct</labels> -- kept the parameters as i didn't know how to change the text : function setText, or benefit from the CoreRPG cycling functionality
</parameters>
<script>
function onInit()
if not window.getDatabaseNode().getChild("instinctcycler") then
window.getDatabaseNode().createChild("instinctcycler","string")
end
super.onInit();
changeColor();
end

function onValueChanged()
changeColor();
changeText();
end

function changeColor()
local a = window.getDatabaseNode().getChild("instinctcycler").getValue();
if a == "0" or a == "" then
window.selfcontrol.setColor("ff000000");
window.courage.setColor("ffff0000"); -- test on another strincontrol, works fine
elseif a == "1" then
window.selfcontrol.setColor("ffff0000");
window.instinct.setColor("ff000000"); -- should apply on word "instinct", though i have no stringcontrol associated at the moment
end
end

function changeText() -- the function to swap texts. Tried with setStateText too.
local a = window.getDatabaseNode().getChild("instinctcycler").getValue();
if a == "0" or a == "" then
window.selfcontrol.setText(defaultval); -- the default word
else
window.selfcontrol.setText("Instinct"); -- the word i want to be applied instead of the default one
end
end
</script>
</button_iconcycler>



The static word ("self-control") code


<stringcontrol name="selfcontrol">
<anchored to="conscience" position="insidetopleft" offset="0,15" />
<center />
<font>sheettext</font>
<static textres="char_label_self-control" />
</stringcontrol>

Trenloe
October 18th, 2016, 16:27
script error : [string "instinctcycler"]:1 : attempt to call field 'setText' (a nil value)
The error gives the clue. "setText" is not valid.

Are you referring to the FG scripting reference documentation? found here: script error : https://www.fantasygrounds.com/refdoc/

As you're using a string control, the documentation is found here: https://www.fantasygrounds.com/refdoc/stringcontrol.xcp This inherits the textbasecontrol: https://www.fantasygrounds.com/refdoc/textbasecontrol.xcp Which has setColor and setValue - but not setText, hence why you get the error above. setValue is what you need and is found here: https://www.fantasygrounds.com/refdoc/textbasecontrol.xcp#setValue and states "Set the text in the control."

I'd strongly recommend you get familiar with this reference documentation and the different functions available to the various controls. This documents what you can do with a control via LUA (and the XML definition).

Trenloe
October 18th, 2016, 16:28
Other details of where to look for info/documentation is found here: https://www.fantasygrounds.com/forums/showthread.php?20651-Modifying-the-CoreRPG-ruleset

Maspalio
October 18th, 2016, 17:28
Thanks Trenloe. I misunderstood the setText ref. With setValue, it's working.