PDA

View Full Version : Context for database events



Hamish
November 5th, 2007, 15:39
Okay, after trying to get solutions to part of my problems and figuring out the rest myself, I'll try posting my entire idea, and see if anyone can see an easier way around it, or tell me what I should do next.

Here's the idea:
Every portrait in the character list (above the chat box) gets a new widget indicating that PCs initiative
Whenever the PC rolls an initiative check the GM can drag the results to the PCs portrait and the value is displayed in the new widget
As a nice to have, I would like to sort the portraits in the characterlist by their initiative in descending order.

To accomplish this I have made the following changes:
In character_entry.lua I added the following lines to the createWidgets function;

initwidget = addTextWidget("sheetlabelsmall", "0");
initwidget.setPosition("center", 20, -20);
initwidget.setFrame("mini_name", 5, 2, 5, 2);
This creates the new widget.

I modified part of the onDrop function as follows;

if draginfo.isType("number") then
if string.lower(draginfo.getDescription()) == "initiative" then
sNode = "charsheet." .. identityname .. ".initvalue"
DB.findNode(sNode).setValue(draginfo.getNumberData ());
else
local msg = {};
msg.text = draginfo.getDescription();
msg.font = "systemfont";
msg.icon = "portrait_" .. identityname .. "_targetportrait";
msg.dice = {};
msg.diemodifier = draginfo.getNumberData();
msg.dicesecret = false;
ChatManager.deliverMessage(msg);
end
return true;
end
This modifies the field initvalue for the corresponding character in the campaign database.

To create this field in the database I added the following lines to the onIdentityActivation function in characterlist.lua;

userctrl.createWidgets(identity);

layoutControls();
end
if DB.findNode("charsheet." .. identity .. ".initvalue") == nil then
initnode = DB.createNode("charsheet." .. identity .. ".initvalue", "number");
else
initnode = DB.findNode("charsheet." .. identity .. ".initvalue");
end
initnode.onUpdate = initUpdate;
end
else
findControlForIdentity(identity).destroy();

This all seems to work so far. But here is where I run into problems; the initUpdate function. I've tried several things, but so far no luck.


function initUpdate()
initwidget.setText(DB.findNode("charsheet." .. identity .. ".initvalue").getValue());
end
This fails because initwidget is not a global object, and the script is (apparently) not run in the context of the characterlist_entry object. I tried putting the function in characterlist_entry.lua instead but I guess that means I have to move the definition of initnode.onUpdate there as well because I get errors if I don't.

Next try:

function initUpdate()
initwidget = findControlForIdentity(identity);
initwidget.setText(DB.findNode("charsheet." .. identity .. ".initvalue").getValue());
end
This fails because identity is not set when the script runs.

For my last attempt I changed the definition of onUpdate to;

initnode.onUpdate = initUpdate(identity);
initUpdate is now:

function initUpdate(identity)
initwidget = findControlForIdentity(identity);
initwidget.setText(DB.findNode("charsheet." .. identity .. ".initvalue").getValue());
end
This fails because apparently you cannot use arguments when defining a function to handle the onUpdate event.

I thank you for taking the time to read all of this, and can only hope that someone understands what I'm doing wrong here, because I'm stuck! I think it's because the trigger for the onUpdate event is in the database and I don't understand what context the initUpdate function runs in.
All help appreciated!