View Full Version : Forcing text into a labeledstring?
nezzir
May 14th, 2007, 23:39
I've bumped into a problem. I had to add/change some items in the Combat Tracker for my ruleset. Part of this process is dragging some text form labeledstrings to the tracker. I've found that if there is nothing in the labeledstring (that is to say, it hasn't been filled out yet), then I get a nil error message. I'm sure there's a correct way to do it, but the quick and dirty way would be to force some default text into the string.
Question:
How to you force some default text into a labeledstring? I checked the documentation and couldn't find an entry for that function.
<labeledstring name="combatweapons">
<anchored>
<to>combatminiweaponframe</to>
<position>insidetopleft</position>
<offset>15,22</offset>
<size>
<width>190</width>
</size>
</anchored>
<anchorto>overviewframe</anchorto>
<label>Weapons</label>
</labeledstring>
Dachannien
May 15th, 2007, 00:31
You might be able to set the text in the object's onInit function. "labeledstring" is a template defined in the default ruleset's "common_templates.xml" file, and it already has an onInit function defined there. "labeledstring" also inherits from the "textbasecontrol" object, which provides the "setValue()" function for setting the contents of the object.
This is pure hypothesis, by the way, so it may not work ;)
nezzir
May 15th, 2007, 01:33
You might be able to set the text in the object's onInit function. "labeledstring" is a template defined in the default ruleset's "common_templates.xml" file, and it already has an onInit function defined there. "labeledstring" also inherits from the "textbasecontrol" object, which provides the "setValue()" function for setting the contents of the object.
This is pure hypothesis, by the way, so it may not work ;)
That's two I owe you. I'll see if I can work my way through this. Thanks again.
Edit: Yeah, I just looked at that. It's a bit beyond me at the moment. My mind says there should be a way to do like an "if nil then" kind of thing, but I don't really know how to do that. I'll poke around in some of the scripts and see if I can reverse engineer something. I'm not really a script writer yet.
TarynWinterblade
May 15th, 2007, 07:39
That's two I owe you. I'll see if I can work my way through this. Thanks again.
Edit: Yeah, I just looked at that. It's a bit beyond me at the moment. My mind says there should be a way to do like an "if nil then" kind of thing, but I don't really know how to do that. I'll poke around in some of the scripts and see if I can reverse engineer something. I'm not really a script writer yet.
I can't even drag a labeledstring when it's empty... o.0
However, what will probably work for you is:
<template name="labeledstring">
<stringfield>
<font>sheettext</font>
<frame>
<name>textline</name>
</frame>
<script>
labelwidget = nil;
function onInit()
if not getValue() then
setvalue("")
end
labelwidget = addTextWidget("sheetlabelinline", string.upper(label[1]));
local w,h = labelwidget.getSize();
labelwidget.setPosition("bottomleft", w/2, h/2-5);
end
</script>
</stringfield>
</template>
Essentially, what that does is:
If getvalue() returns nil, false, or otherwise something that doesn't equal true, it will then call setValue and pass it an empty string.
Should fix the problem, but it's not the best way to do it.
A better way to do it:
The better bet for dealing with the problem is to deal with nil errors on the receiving control's side. In the control's onDrop handler, just specify that if it's passed a nil value then set it to something there.
The reason why this is better is because that way it handles if the labeledstring field is somehow not initialized.
(if you have problems, just give me the error message and the snippet of code that it relates to)
nezzir
May 15th, 2007, 12:43
A better way to do it:
The better bet for dealing with the problem is to deal with nil errors on the receiving control's side (I tried it as is and with a value between the quotes). In the control's onDrop handler, just specify that if it's passed a nil value then set it to something there.
The reason why this is better is because that way it handles if the labeledstring field is somehow not initialized.
(if you have problems, just give me the error message and the snippet of code that it relates to)
I tried the
if not getValue() then
setvalue("")
end
(tried it as is and with a value in the quotes) and it didn't work. It blew up all my labeledstrings sheet-wide. I've attached all my combat_tracker scripts in a zip (because I really wasn't sure which one was responsible). If you get a few minutes to browse through them, I'd value your opinion.
I really gotta stop and teach myself some lua...
Thanks,
Nez
TarynWinterblade
May 15th, 2007, 21:47
I tried the
if not getValue() then
setvalue("")
end
(tried it as is and with a value in the quotes) and it didn't work. It blew up all my labeledstrings sheet-wide.
That's... strange. That means that it's returning false for some reason.
I've attached all my combat_tracker scripts in a zip (because I really wasn't sure which one was responsible). If you get a few minutes to browse through them, I'd value your opinion.
What does the error message you're getting say?
i.e.: "Script error: [scripts/somefile.lua] Tried to do something wrong, somewhere..."
nezzir
May 15th, 2007, 22:11
What does the error message you're getting say?
i.e.: "Script error: [scripts/somefile.lua] Tried to do something wrong, somewhere..."
Script Error: [string "scripts/combattracker.lua"]:68: attempt to index a nil value
Line 68 of combattracker.lua is:
newentry.ac.setValue(source.getChild("combatarmor").getValue())
The labeledstring that sets that says:
<labeledstring name="combatarmor">
<anchored>
<to>combatminiarmorframe</to>
<position>insidetopleft</position>
<offset>15,22</offset>
<size>
<width>190</width>
</size>
</anchored>
<anchorto>overviewframe</anchorto>
<label>Armor</label>
</labeledstring>
I get the error until I put something in that string field on the combat page of the character sheet.
TarynWinterblade
May 15th, 2007, 22:13
So far, just from looking at things (I'm missing a few files - namely the windowclass for the combat tracker... can you attach your utility_combattracker.xml file?)...
In combattracker.lua:
function onDrop(x, y, draginfo)
if draginfo.isType("playercharacter") then
local name = draginfo.getStringData();
local source = draginfo.getDatabaseNode();
local token = draginfo.getTokenData();
if source then
local newentry = addPc(name, source, token);
end
return true;
elseif draginfo.isType("shortcut") then
-- NPC code...
end
end
You took out the two lines in red. Don't do that :D Those lines make sure that you have a valid source node in the DB so that you don't get more errors in addPc().
Also, you added the parts in blue there, thus passing an extra variable to addPc. Why do you want the name passed like that? The d20 code handles setting the name from the PC's database node (source)... what's different about your ruleset's code that necessitates having the name passed separately?
TarynWinterblade
May 15th, 2007, 22:17
Script Error: [string "scripts/combattracker.lua"]:68: attempt to index a nil value
Line 68 of combattracker.lua is:
newentry.ac.setValue(source.getChild("combatarmor").getValue())
The labeledstring that sets that says:
<labeledstring name="combatarmor">
<anchored>
<to>combatminiarmorframe</to>
<position>insidetopleft</position>
<offset>15,22</offset>
<size>
<width>190</width>
</size>
</anchored>
<anchorto>overviewframe</anchorto>
<label>Armor</label>
</labeledstring>
I get the error until I put something in that string field on the combat page of the character sheet.
Ahh. That's not because the string is empty, but rather because source.getChild("combatarmor") returns nil.
Change your code from:
newentry.ac.setValue(source.getChild("combatarmor").getValue())
... to:
if source.getChild("combatarmor") then
newentry.ac.setValue(source.getChild("combatarmor").getValue())
else
newentry.ac.setValue("")
end
It's odd though, that it would only complain about that particular field.
TarynWinterblade
May 15th, 2007, 22:22
Also, something to keep in mind:
In the d20 combat tracker, SmiteWorks coded the fields to keep track of changes in the database node they linked to. Hence, if you changed something with the character they were referencing, it would update on the sheet as well.
However, by just calling setValue, instead of copying the setLink functionality, you don't trigger the automatic updates.
Edit: Also, using SW's setLink code would have gotten rid of the error, since their code, by default, checks to see if the node it got passed was nil before attempting anything with it. :D
Oberoten
May 15th, 2007, 22:57
*eyes the Signature*
LFG so rocks my world. So much love for Richard. :)
nezzir
May 15th, 2007, 22:57
Why do you want the name passed like that?
Why did I do that? Because I don't know what I'm doing :)
I kind of just fiddled around with it till it works and then slapped a coat of wax on it and moved on to the next thing. Honestly, it's amazing I finished the ruleset at all - lol. I am the proverbial 1,000 monkeys.
It's odd though, that it would only complain about that particular field.
It just complains about that field till I put a string in it, then it complains about the next one :/
So, do I need to remove the blue, add the red, and add the if/then/else statement - all three?
I'll start messing with it and see what I get.
TarynWinterblade
May 15th, 2007, 23:02
*eyes the Signature*
LFG so rocks my world. So much love for Richard. :)
<.<... >.>... I wondered if anybody would catch that :)
Quote:
Originally Posted by TarynWinterblade
Why do you want the name passed like that?
Why did I do that? Because I don't know what I'm doing
I kind of just fiddled around with it till it works and then slapped a coat of wax on it and moved on to the next thing. Honestly, it's amazing I finished the ruleset at all - lol. I am the proverbial 1,000 monkeys.
Quote:
It's odd though, that it would only complain about that particular field.
It just complains about that field till I put a string in it, then it complains about the next one :/
So, do I need to remove the blue, add the red, and add the if/then/else statement - all three?
I'll start messing with it and see what I get.
Probably just add the if/then/else. Though the code that's in the red would probably save a possible error later on down the road.
nezzir
May 15th, 2007, 23:52
Probably just add the if/then/else. Though the code that's in the red would probably save a possible error later on down the road.
I think that got me where I wanted to be. I had to duplicate those lines for the numeric entries and actually assign a default value to them in the event of nil ("0"), but I figured that out.
Thank you so much for taking time to mess with my obesssio... I mean project.
TarynWinterblade
May 16th, 2007, 00:37
I think that got me where I wanted to be. I had to duplicate those lines for the numeric entries and actually assign a default value to them in the event of nil ("0"), but I figured that out.
Thank you so much for taking time to mess with my obesssio... I mean project.
No problem. The more I spend looking at the code, the less I'll have to work later on when I'm sitting there tinkering with the combat tracker for my ruleset. :square:
Insanity
May 17th, 2007, 20:43
you can add a static value, i.e.
<labeledstring name="name">
<anchored>
<to>overviewframe</to>
<position>insidetopleft</position>
<offset>15,10</offset>
<size>
<width>150</width>
<height>20</height>
</size>
</anchored>
<anchorto>overviewframe</anchorto>
<height>20</height>
<label>name</label>
<static>Billy Bob</static>
<tabtarget>
<next>race</next>
<prev>appearance</prev>
</tabtarget>
</labeledstring>
nezzir
May 17th, 2007, 22:06
you can add a static value, i.e.
Doh! Well, that was still very informative, and helped me do things a better way.
Powered by vBulletin® Version 4.2.1 Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.