PDA

View Full Version : Probelms accessing subwindows



Brenn
May 8th, 2008, 16:31
I'm having a problem accessing a subwindow I've created- more specifically calling a function declared in a script on one of the components in the subwindow.

The windowclass:

<windowclass name="charsheet_newskilldialog">
<sheetdata>
<genericcontrol name="newskillframe">
<bounds>15,20,480,580</bounds>
<frame>
<name>sheetgroup</name>
</frame>
<script file="scripts/charsheet_newskilldialog.lua" />
</genericcontrol>
</sheetdata>
</windowclass>


The subwindow declaration:

<windowclass name="charsheet_skills">
<placement>
<size>
<width>252</width>
<height>611</height>
</size>
</placement>
<!--<nodelete />-->
<sheetdata>

................................................
.other controls removed for brevity.
................................................

<subwindow name="newskilldialog">
<anchored>
<position>over</position>
</anchored>
<class>charsheet_newskilldialog</class>
<active />
</subwindow>
</sheetdata>
</windowclass>


The call to the subwindow:

function createSkill()
window.newskilldialog.setVisible(true);
window.newskilldialog.subwindow.newskillframe.setP arent(self);
end


the set visible call works, but the setParent call does not (setParent is a defined function in charsheet_newskilldialog.lua).
With the previous code, subwindow is nil. I've tried moving the script to be a part of the windowclass definition and using window.newskilldialog.setParent(self)- setParent was nil. I've tried window.newskilldialog.newskillframe.setParent(self )- setParent was nil.

I had this working fine when I delcared newskillframe as a template and created it dynamically, but I decided to learn something about subwindows since I've done next to nothing with them yet and I want to do some stuff with the radial menu, and for the life of my I can't get them to work.

Any suggestions? I've had problems this whole time on how to reference items, and just when I thought I had everything down this pops up...

Foen
May 8th, 2008, 18:10
Subwindows don't follow the normal instancing order: normally the containing window has its onInit called after the contained control onInit. With subwindows, the order isn't guaranteed, and happens (seemingly) haphazardly.

A good event to use is onInstanceCreated ... the handler needs to be put in the containing control's subwindow.script element (I think).

Stuart

Brenn
May 9th, 2008, 01:58
Thanks for the advice. The problem I appear to be having, though, has something to do with the inheritance of scripts. I haven't had a problem before calling functions defined in the parent class ( I create stuff dynamically quite a bit), however when I try to do this in the subwindow sometimes it works and sometimes it doesn't. I am able to call functions defined in a particular instance of a class though.

Don't know if that made a bit of sense, but I'm a bit tired and am not thinking clearly enough right now to even attempt to figure this out. I'm going to have to delve further into this later.

If anyone has any insight into this let me know...

Foen
May 9th, 2008, 06:01
I'd be happy to take a look if you post some snippets of code, and a brief explanation of the window/subwindow/control hierarchy.

Stuart

Brenn
May 9th, 2008, 23:32
Ok here goes- the hierarchy in question isn't that different from the D20 stock stuff.

Desktop - Character Sheet window (with tabs)- Skills Window with a windowlist for the skills.

User right clicks in the skills window and selects "add new skill" off of the radial, the subwindow dialog for adding a skill is displayed. All of this I got working (I solved the problem I was having above by moving the newskilldialog script declaration from the window class xml, to the subwindow xml (which leads me to believe I'm not understanding something fully about how to address subwindows). Now on to the current problem. Code first:

The declaration of the two templates in question from charsheet_templates.xml:


<template name="newskill_entry">
<stringcontrol>
<bounds>50,60,100,74</bounds>
<font>sheetlabel</font>
<frame>
<name>textline</name>
<offset>0,2,0,0</offset>
</frame>
</stringcontrol>
<script file="scripts/charsheet_newskillentry.lua" />
</template>

<template name="newskill_statlabel">
<radialstringcontrol>
<bounds>50,60,100,30</bounds>
<font>titlefont</font>
<frame>
<name>bonus</name>
<offset>5,11,5,-4</offset>
</frame>
<font>smallcontrol</font>
<static />
<nodrag />
<nodrop />
<center />
<script file="scripts/charsheet_newskillstatlabel.lua" />
</radialstringcontrol>
</template>

Two functions that create instances of the previous templates and call inherited functions (ignore my goofy widget positioning math in createSkillLabel, I was messing around with something):


function createSkillLabel()
local xbound = 135;
local ybound = 60;
local boxWidth = 200;
local boxHeight = 30;
local labelwidgetOffset = math.floor((17*boxWidth)/60);
local warnwidgetOffset = math.floor((23*boxWidth)/60);
newEntryBox = window.createControl("newskill_entry", "newSkillEntry");
newEntryBox.setStaticBounds(xbound, ybound, boxWidth, boxHeight);
newEntryBox.setParent(self);
newEntryLabel = newEntryBox.addTextWidget(newSkillFont, "New Skill Name:");
newEntryLabel.setPosition("topleft", -labelwidgetOffset, 8);
entryExistsLabel = newEntryBox.addTextWidget("sheetlabelred", "Skill Already Exists!");
entryExistsLabel.setPosition("topright", warnwidgetOffset, 8);
entryExistsLabel.setVisible(false);
newEntryBox.setEnabled(true);
newEntryBox.setFocus(true);
end

function createStatList()
local dlgxpos, dlgypos = getPosition();
local dlgxsize, dlgysize = getSize();
local centerx = (dlgxpos+math.floor((dlgxsize/2)));
local centery = (dlgypos+math.floor((dlgysize/2)))
attributeSelectionItems["none"] = window.createControl("newskill_statlabel", "newstatlabelnone");
attributeSelectionItems["none"].setStaticBounds(centerx-50, centery-15, 100, 30);
attributeSelectionItems["none"].setValue("None");
attributeSelectionItems["none"].setParent(self);
attributeSelectionItems["none"].setSelected(true);
selectedStat = "None";
local abilitiesnode = window.getDatabaseNode().getChild("abilities");
local numStats = abilitiesnode.getChildCount();
local radialStep = 100/numStats;
local radialInc = 0;
local statLabel = "";
local stattable = {};
stattable = abilitiesnode.getChildren();
for k, v in pairs(stattable) do
radialInc = radialInc + radialStep;
statLabel = string.upper(string.sub(k, 1, 1)) .. string.sub(k, 2)
attributeSelectionItems[k] = window.createControl("newskill_statlabel", "newstatlabel" .. k);
attributeSelectionItems[k].setValue(statLabel);
attributeSelectionItems[k].setRadialPosition(centerx, centery, 150, math.floor(radialInc));
attributeSelectionItems[k].setParent(self);
end;
end

newSkillEntry.setParent(self) fails saying setParent is nil (the line prior processes just fine.

charsheet.newskillentry.lua:


function setParent(somecontrol)
myParent = somecontrol;
end

function onValueChanged()
if myParent.entryChanged then
myParent.entryChanged();
end
end


On the flip side attributeSelectionItems calls it's setParent and it works like a charm...

newskillstatlabel.lua setParent function:

function setParent(parentControl)
myParent = parentControl;
end

Both are instances of templates declared in the same file. To go one futher, attributeSelectionItems is a template containing a template I declared in commontemplates.xml, and it's the one that is working.

I might be missing something obvious, but I've been putting in some long hours at work the past few days and have really been too tired to be really sharp.

Foen
May 10th, 2008, 06:19
Well there doesn't seem to be anything obvious (and we are slightly off the subwindow topic, but I'm happy to help with the new problem).

Have you opened a console window when running FG to see if there are any errors when loading or parsing the script files? If there is a syntax error in newskillentry.lua before the setParent function, that function won't be parsed and will therefore be nil.

Stuart

Brenn
May 10th, 2008, 10:18
Darn, I was hoping it would be :( .

Yeah, I've been through the console log. The only thing in there is:


[10.05.2008 04:08] Script Error: [string "scripts/charsheet_newskilldialog.lua"]:74: attempt to call field 'setParent' (a nil value)

This has me befuddled. Maybe I'll get some time to hit it tomorrow, but for now it's off to work.

Thanks for the help.

Somehow I think that when I figure this one out I'll be able to move the script definition back from the instance call to where I originally had it in the first problem...

Brenn
May 11th, 2008, 09:08
Get a little sleep and it all becomes clear... On the second problem I was having at least. It helps to access the scripts for a control if you actually declare the script within the tags for that control... I feel like an idiot ;)

The subwindow problem I still haven't figured out but I think I'll just let it lie for now and move on.

Foen
May 11th, 2008, 09:22
Hehe ... it happens. The important thing is that the problem is fixed.

Happy to follow up on the subwindow subject, if you want to explore it further.

Stuart