Log in

View Full Version : Setting XML tags from script difficulty



GrimmSpector
March 10th, 2016, 21:49
So I have this bit of code, attached to a windowlist, instancing the class below, gives me this error:

Script Error: [string "charsheet_main:armor"]:1: attempt to set a value for an invalid handler 'labelres'


This code gets called when the windowlist initializes, being passed several entries to setup the order of the list elements, and the label value of one of the controls within it, "itemname", and I want to set it's xml property, "labelres" to a given string value, from what I've read on the scripting guide, it should simple be the object (with hierarchy as necessary) being set equal to a table statement, i.e. labelres = { [1] = "somestringhere" }
Seems that it's not working out, though it's clearly findind the "itemname" control in the window instance.


Window Class:



<windowclass name="charsheet_armor">
<sizelimits>
<maximum>
<height>28</height>
</maximum>
<minimum>
<height>28</height>
</minimum>
</sizelimits>
<sheetdata>
<numbercontrol name="order">
<bounds>0,0,0,0</bounds>
<invisible />
</numbercontrol>
<string_labeled name="itemname">
<anchored position="insidetopleft" offset="0,0" width="70" height="20" />
<labelres>char_label_armor_location</labelres>
</string_labeled>
<string_labeled name="armortype">
<anchored to="itemname" position="right" width="45" offset="5,0" />
<labelres>char_label_armor_type</labelres>
<readonly />
<script>
function onInit()
setValue("Temp.");
end
</script>
</string_labeled>
<numberHolder name="melee" />
<numberHolder name="ballistic" />
<numberHolder name="energy" />
<numberHolder name="explosive" />
<stringcontrol>
<anchored to="armortype" position="rightlow" offset="5,-1" width="60" />
<font>sheetnumber</font>
<readonly />
<center />
<script>

function onInit()
updateArmor();
end

function updateArmor()
local melee = tostring(window.melee.getValue());
local ballistic = tostring(window.ballistic.getValue());
local energy = tostring(window.energy.getValue());
local explosive = tostring(window.explosive.getValue());
setValue(melee .. "/" .. ballistic .. "/" .. energy .. "/" .. explosive);
end
</script>
</stringcontrol>
</sheetdata>
</windowclass>


Windowlist:


<windowlist name="armor">
<anchored to="combatdataframe" position="insidetopleft" offset="10,70" />
<class>charsheet_armor</class>
<datasource>.armor</datasource>
<noscroll />
<skipempty />
<script>
torso = nil;
head = nil;
legs = nil;
feet = nil;
arms = nil;
hands = nil;

local order = 1;

function onInit()
torso = addEntry("torso");
head = addEntry("head");
legs = addEntry("legs");
feet = addEntry("feet");
arms = addEntry("arms");
hands = addEntry("hands");
applySort();
end

function addEntry(name)
local node = getDatabaseNode().getChild(name);
local win = nil;
if not node then
node = getDatabaseNode().createChild(name);
end
for i,w in ipairs(getWindows()) do
if w.getDatabaseNode().getName()==name then
win = w;
end
end
if win then
win.order.setValue(order);
win.itemname.labelres = {
[1] = "char_label_armor_" .. name
}
order = order + 1;
end
return win;
end

function onSortCompare(w1, w2)
return (w1.order.getValue() > w2.order.getValue());
end
</script>
</windowlist>


Maybe someone can help me out here, as the reference must be correct, since the "order" value is being set accurately, and "labelres" is a top level property inside of "itemname"

You can ignore the labelres definition value, as it's just a placeholder string.

The string in the code should workout for the first window to be a string defined in my string values that reads "TORSO", and should thus attach that, instead of throwing the error, and leaving the text as the placeholder.

Moon Wizard
March 10th, 2016, 23:41
You can't use the script to edit the XML tags. Any XML tags (not attributes) in a control template will be represented in variables in the global space for that control, and will be represented by tables. You can not set a variable in a control global space (which is what the error is telling you). You would have to run a script within the space (i.e. win.itemname.setTextRes("char_label_armor_" .. name));

In this case, you should just be setting the text label directly. (i.e. win.itemname.setValue(Interface.getString("char_label_armor_" .. name)); )

Regards,
JPG

GrimmSpector
March 11th, 2016, 00:00
I can't set it directly, it's a labeled string, so the label resource is being used to put a text widget below the stringfield; doing win.itemname.setValue(Interface.getString("char_la bel_armor_" .. name)); would set the value of the stringfield, which is not what we want.

I suppose I'll have to go the script route then. I assume by in the space, you mean a script that is within the control I want to effect directly. I'll try that.

GrimmSpector
March 11th, 2016, 00:07
Ok, I tried, and failed, no errors or console messages, I've changed the object I'm trying to set the labelres for to this:



<string_labeled name="itemname">
<anchored position="insidetopleft" offset="0,-2" width="70" height="20" />
<labelres>char_label_armor_location</labelres>
<script>
function setTextRes(name)
labelres = { [1] = name}
end
</script>
</string_labeled>


And took out the
if win then
win.order.setValue(order);
win.itemname.labelres = {
[1] = "char_label_armor_" .. name
}
order = order + 1;
end

Block and replaced it with:

[CODE]
if win then
win.order.setValue(order);
win.itemname.setTextRes("char_label_armor_" .. name);
order = order + 1;
end
[CODE]

But the label stays as what it was set to initially in the control definition...

Moon Wizard
March 11th, 2016, 07:27
The labelres variable is a specific value that is only looked at when the control is initialized. (Look at the string_labeled template in CoreRPG.) If you want to change the label like that, you'll need to extend or replace the "string_labeled" control with your own template that does what you want.

Regards,
JPG

GrimmSpector
March 11th, 2016, 18:07
The labelres variable is a specific value that is only looked at when the control is initialized. (Look at the string_labeled template in CoreRPG.) If you want to change the label like that, you'll need to extend or replace the "string_labeled" control with your own template that does what you want.

Regards,
JPG

Ah I see, so setup a function call that will adjust the text widget, ok, thanks Moon!