PDA

View Full Version : onDiceLanded



MindWorX
December 9th, 2009, 20:39
Hi, I'm trying to get into FGII modding, and I thought I'd start small with a simple extension, but I think I'm doing something wrong. I want to catch the event, "onDiceLanded (https://fantasygrounds.com/refdoc/chatwindow.xcp#onDiceLanded)", found on the chatwindow (https://fantasygrounds.com/refdoc/chatwindow.xcp) control, but it doesn't work.

Here's the code

function onInit()
if ChatManager then
ChatManager.registerSlashHandler("/forage", forage);
end
end

function onDiceLanded( draginfo )
if ChatManager then
ChatManager.addMessage({font = "systemfont", text = "onDiceLanded"});
end
return false;
end

initialize = true;
function forage(params)
if ChatManager then
if initialize then
ChatManager.addMessage({font = "systemfont", text = "Initializing onDiceLanded."});
## ChatManager.control.onDiceLanded = onDiceLanded; -- Script Error: Attempt to set a value for an invalid handler 'onDiceLanded'
initialize = false;
end
local dice = {};
table.insert(dice, "d20");
ChatManager.control.throwDice("dice", dice, 0, "Nature foraging check");
end
end

I've prefixed the line with the error with "##" so it's easier to find. Any help appreciated.

EDIT:
It's the 4E_JPG (https://fantasygrounds.wikia.com/wiki/4E_JPG) ruleset, incase it matters.

Foen
December 9th, 2009, 22:50
It is worth noting that there are two, mutually incompatible, event models at play in FGII, and it isn't obvious at first glance which one applies in which circumstance.

Events

Events (italics and proper case, to identify this type of event mechanism) are script functions, with a prescribed name, which are called by the FG engine when certain events (without italics, and in lower case) occur. To use Events, the script must be written in the context of the event. A typical example is onInit: if a window control has an onInit function, it will be called when the control has been initialised.

Events cannot be captured by scripts sitting outside of the source context (a control cannot respond to the onInit event of another control).

Handlers

A Handler is a piece of code assigned to certain other event names. Merely having a function with the same name isn't enough, and in fact the function name is irrelevant. To use Handlers, the handling function is assigned to the object event name and this can happen within the source context or from outside.

A typical example is onUpdate, which is a Handler that is invoked when a database node value changes. You might create a function in one control called sourceUpdated, and assign it to the onUpdate of a database node from another control.

The important thing is to understand that events are either dealt with using the Event mechanism or they are dealt with using the Handler mechanism. The FG XML and Scripting Reference allocates each event to one of these mechanisms.

Back to your problem...

onDiceLanded is dealt with using the Event mechanism, which means the code to handle it must be written in the context (chat_chat.lua) in which the event arises. You cannot therefore assign an event handler to ChatManager.control.onDiceLanded, and hence you get the script error.

Sorry for the long-winded response, but hopefully it helps explain why it doesn't work as you would expect.

Stuart

MindWorX
December 10th, 2009, 12:31
That's a very informative post, thanks a lot. But does that mean I can't have an onDiceLanded event in an extension? It has to be part of the ruleset?

Moon Wizard
December 11th, 2009, 08:09
You would actually have to replace the entire chat_chat.lua file in order to make any changes to the event handlers in that file.

Since you are using the 4E_JPG ruleset, I'm curious as to what feature you are trying to implement?

Cheers,
JPG

Valarian
December 11th, 2009, 13:52
I believe there are now "Dice Handlers" that can be registered in the same way as slash handlers. Maybe this is what you need. I've not played with them though, so I'm unsure on how they work. Examples are in the Foundation and 3.5e extension. The 3.5e extension registers a dice handler for Full Attacks. This would allow you to add to the existing chat_chat.lua functionality (as long as the 4e_JPG chat_chat.lua contains the same dice handler code that the Foundation one does).

Moon Wizard
December 13th, 2009, 23:14
I've been thinking about these kinds of changes in order to modularize the ruleset code, and allow it to be more easily extensible. I'll add roll type handler functions to the wish list for the JPG rulesets.

Cheers,
JPG

MindWorX
December 15th, 2009, 01:17
Mostly it's just a learning process for me, in extension programming. I was going to make a chat command that throws a d20+Nature, and depending on the roll, either just output 1 or output 1d4+1. It's a simple houserule to the foraging part of Nature.