PDA

View Full Version : Noobish question about windowclass, windowinstance, and making a list of skills



cscase
July 27th, 2013, 21:37
Hey all,
This is probably a really noobish question, but I'm hoping you can give me a hand here.

I'm working on making a custom character sheet, and I want to make a list of skills. For each skill, there will be three columns/values: The name, the skill rating or max value, and the pool.

Because what I want to do is really simple, I thought I would learn more and have an easier time just starting with an empty frame and building my own skill list, instead of trying to puzzle through the more complex code in existing rulesets. I have some code that makes one line on the skill list. Below I'm pasting the first couple of pieces as an example.

I had initially thought to just keep it as simple as possible by just hand coding each individual skill like this. But repeating all this code for every skill in the list just seems like SUCH an inefficient way to go about it.

So, I thought maybe the thing to do would be to use a windowclass, then create windowinstances for each skill. But in the reference material, there is no example code for windowinstance, and the rulesets I'm looking at don't use it either. I feel like I'm just kinda flailing around and trying different syntaxes randomly to see if I hit on something that works, and I'm not really sure if I'm even moving in the right direction. That's where I'm hoping to get some advice.

What do you think is the best approach for generating a skill list with simple objects? I'm thinking each line an explicitly instantiated class object, but if you think I'm going the wrong way to go about it, I am all ears. I really feel lost. It seems like what I struggle with most is figuring out what tags to use for what. The Lua I've seen so far makes sense.

Thanks all!


<stringcontrol name="skillname">
<anchored>
<to>skillframe</to>
<position>insidetopleft</position>
<offset>10,38</offset>
</anchored>
<sizelimits>
<width>5</width>
</sizelimits>
<font>sheetlabel</font>
<static>Athletics</static> <!-- Need to somehow make this label a variable??? -->
<script>
function onDoubleClick(x,y)
ChatManager.throwDice("dice",{"d6"},0," ("..window.name..")");
return true;
end
</script>
</stringcontrol>

<numberfield name="skillrating">
<min>0</min>
<max>15</max>
<anchored>
<to>athleticslabel</to>
<position>right</position>
<offset>62</offset>
<size>
<width>30</width>
</size>
</anchored>
<font>sheettextnumval</font>
<stateframe>
<keyedit>
<name>sheetfocus</name>
<offset>5,5,5,5</offset>
</keyedit>
</stateframe>
<delaykeyupdate />
<script>
function onWheel(notches)
if Input.isControlPressed() then
setValue(getValue() + notches);
if window.skillpool.getValue() &gt; getValue() then
window.skillpool.setValue(getValue());
end
end
return true;
end

function onDoubleClick(x,y)
ChatManager.throwDice("dice",{"d6"},0," ("..window.name..")");
return true;
end

function onValueChanged()
if window.skillpool.getValue() &gt; getValue() then
window.skillpool.setValue(getValue());
end
return true;
end
</script>
<nodrag />
</numberfield>

Dakadin
July 27th, 2013, 22:57
The windowinstance is actually a windowclass that is being used within the ruleset. For example, the charsheet windowclass just defines the characteristics of the character sheet. When you open an actual character sheet it create the windowinstance of the charsheet windowclass. It is actually a working copy that you can interact with, update, etc.

For a skill list you will want a windowclass to represent an individual skill entry and a windowclass that will contain a windowlist of the individual skill windowclass. Do a search for windowlist and you should be able to see examples of how it is done. Hopefully that gets you going in the right direction.

cscase
July 28th, 2013, 02:33
Nevermind! I may have figured it out... working...

Thanks Dakadin!

Dakadin
July 28th, 2013, 06:09
Glad to hear it. I saw the question on my phone when I received the email notification. I was just going to answer it but it is always more satisfying when you figure it out yourself. :D

cscase
July 29th, 2013, 05:54
Still hacking away at this, going through the familiar cycle:
"AHA!"
...
"Hrmmm"
...
"What are you trying to tell me here, computer?"
...
"%@(!!!!!!!!"
...
repeat

I'm not getting any error messages now, but I'm also not seeing my windowlist show up.

I'm looking at this piece of code which I took from another ruleset's example, and I don't fully understand how it works:

function onInit()
-- create default skills
local skilldata = SkillManager.getSkills(general);
local winlist = {};

for k, w in ipairs(getWindows()) do
local label = w.label.getValue();
if skilldata[label] then
if winlist[label] then
table.insert(winlist[label], w);
else
winlist[label] = { w };
end
end
end

-- Set properties and create missing entries for all known skills
for k, t in ipairs(skilldata) do
local matches = winlist[k];

if not matches then
local newwin = createWindow();
newwin.label.setValue(k);
matches = { newwin };
end


I think this code is reading skills from another script that returns them, and then iteratively doing... something... with them. Potential trouble spot is that originally, the other script provided a table, like this:

local skills = {
["Accounting"] = { base=10},etc. etc.

But I set up mine to provide only an array, like this:

local genskills = {"Athletics","Armed Robbery",etc. etc.,}

So, I changed the above code to use ipairs, instead of pairs, which it had before. I just mention that because I'm not sure it was the right thing to do and it might be the source of this problem.

The SkillManager.getSkills(general) returns my array of skills that is mentioned above.

Questions:

Was changing from pairs to ipairs good thing to do? It cleared up an error message, but I'm not entirely sure I grasp the implications of doing that.
What is going on in this function? Can you guys tell, or is it hard to say without more context?
What are k and w that Lua uses in its "for"? Are those fixed symbols that refer to specific things, or might I just as well have used x and y for those?
Since I used an array for skill names instead of a table, is it a problem that the above code uses table.insert? It seems like it's taking data from my array and feeding it into a table, though I'm not sure what for.


Any insight anyone might be able to offer is greatly appreciated! At this point I feel like cussing this thing out and giving up, but I also feel like I have too much time invested to quit so easily :P

Thanks!
Scott

Dakadin
July 29th, 2013, 08:19
1. Unfortunately, I am not an expert on pairs and ipairs. I did find this that explains it though: https://www.luafaq.org/#T1.10.

2. The first for loop is actually going through the existing entries in the windowlist and storing them in the winlist variable. Then the next for loop looks like it is checking if the existing skilldata entry is already in the list. If it isn't then it is creating a window in the windowlist.

3. k is just the index of the current element. w is the actual element.

4. I would recommend using the same table structure as the original table structure. Once you get it working with the original table structure, it will be make it easier to see if it is actually the table structure causing the issues. If it works with the original table structure than you likely need to track down where to change the code to handle your new table structure.

It is a bit overwhelming at times but it does get better. I've had some frustrating sessions and sometimes I just needed a break to clear my head or had to ask someone a question. Keep posting the questions. Someone will likely answer them fairly quickly.