PDA

View Full Version : Help With button_stringcycler



dulux-oz
April 26th, 2014, 08:50
Hi Guys,

Stupid question, but does the button_stringcycler.setStringValue() function take a textres as its input, or only a string?

If it takes a textres, what is the exact syntax, please.

If it only takes a string, how do you set the cycle back to the default when the defaultlabelres is used - if we have to hard-code the value of the defaultlabelres's textres into the button_stringcycler's user code to set it back to the default, then doesn't that defeat the purpose of having the textres in the first place (the idea being that someone else can override the value of the textres in a Language Extension (for example) and therefore break the (user) code)?

Or have I missed something somewhere? :p

Cheers

Bidmaron
April 26th, 2014, 13:49
Probably the easiest thing to do for your specific case is to do this:

button_stringcycler.setStringValue(Interface.getSt ring(defaultlabelres));

The other thing you could do is always keep the default as, say, defaultValue, in which case you could just:

button_stringcycler.setValue(defaultValue);

This is more workable than it seems because you can't use string values in your code to take action without building in language-dependency with the control as written. Thus, to stay language-independent, you have to use the string ordinal values in your code to take action anyway (unless the action only involves transferring or saving the text of the control somewhere).

The more comprehensive solution is to create a button_stringcycler2 or something that does something like this (with the new merge rules this should work, but I don't have time to test it right now):


<template name="button_stringcycler2">
<button_stringcycler>
function setStringValueRes(sInRes))
setStringValue(Interface.getString(sInRes));
end

function getStringValueRes()
if not parameters[1].labelres[1] then
return "Error";
end
local labels = StringManager.split(labelres[1], "|");
return labels[getValue()];
end
</button_stringcycler>
</template>

When you use the new button_stringcycler2, you can then work strictly with the string resources, setting the control value using setStringValueRes (passing the string resource name as the parameter) and obtaining the value using getStringValueRes.

Trenloe
April 29th, 2014, 06:19
Hi Guys,

Stupid question, but does the button_stringcycler.setStringValue() function take a textres as its input, or only a string?

If it takes a textres, what is the exact syntax, please.

If it only takes a string, how do you set the cycle back to the default when the defaultlabelres is used - if we have to hard-code the value of the defaultlabelres's textres into the button_stringcycler's user code to set it back to the default, then doesn't that defeat the purpose of having the textres in the first place (the idea being that someone else can override the value of the textres in a Language Extension (for example) and therefore break the (user) code)?
There isn't actually an issue with this, it is just a case of understanding how it works and what the syntax is.

Take the following as an example - this is a stringcylcer in the 3.5e ruleset used to specify how a weapon is being used to determine how the strength modifier will be applied based off the selection Melee one handed, Melee 2 handed or Melee offhand.

<button_stringcycler name="damagemeleestatadj">
<anchored to="damageadjanchor" position="insidetopleft" width="130" />
<parameters>
<defaultlabelres>char_label_weaponmelee1h</defaultlabelres>
<labelsres>char_label_weaponmelee2h|char_label_weaponmeleeoh</labelsres>
<values>2h|oh</values>
</parameters>
<script>
function onValueChanged()
window.onDamageChanged();
end
</script>
</button_stringcycler>
The key things are the parameters here:

<defaultlabelres>char_label_weaponmelee1h</defaultlabelres> specifies the default to be used and uses the textres "char_label_weaponmelee1h". This will be used if the control stringValue is blank, i.e. "".
<labelsres>char_label_weaponmelee2h|char_label_weaponmeleeoh</labelsres> specifies the other 2 textres strings to use for the string values that are specified in the next section <values>
<values>2h|oh</values> these are the actual values that are returned using the <control>.getStringValue() command - i.e."2h" will be returned if the control displays the textres "char_label_weaponmelee2h" and "oh" will be returned if the control is showing textres "char_label_weaponmeleeoh".


The control will initially show the textres "char_label_weaponmelee1h" (the default) and will cycle through "char_label_weaponmelee2h", then "char_label_weaponmeleeoh" and then back to "char_label_weaponmelee1h" and so on.

The corresponding values returned from <control>.getStringValue() would be: "", "2h" then "oh" as the control is cycled through.

The reverse is true for <control>.setStringValue(value) - value = "" if you want to set the default.

These values are always the same - "", "2h" and "oh". They correspond to the <defaultlabelres>, then the <labelsres> list in order.

This fully supports textres and therefore translations. And code just uses <control>.getStringValue() to return "", "2h" or "oh" no matter what the textres string is.

dulux-oz
April 30th, 2014, 11:05
Hmmm, so why didn't that work (setting <control>.setStringValue(""))?

More investigation required, I believe - thanks Tren.

Trenloe
April 30th, 2014, 12:05
Hmmm, so why didn't that work (setting <control>.setStringValue(""))?
What is your control XML definition?