PDA

View Full Version : Run script on character creation



PhoeNox
December 7th, 2020, 21:51
Hello everyone,

I'm just getting into scripting my own extension and need some help with a (hopefully) fairly simple question.

The goal is to provide a "character template", so that when you create a new character and open the character sheet for the first time, it already has some meaningful values at some places.
However, I cannot find an event or handler that gets executed on exactly this trigger.

I found that by extending the charsheet windowclass and hooking into onInit(), I can run code the first time a charsheet is created in a session. However, that code would also be executed when opening the character in the next session.

<?xml version="1.0" encoding="iso-8859-1"?>
<root>
<windowclass name="charsheet" merge="join">
<script>
function onInit()
super.onInit();
Debug.console("This works");
end
</script>
</windowclass>
</root>

So my question is: What event/handler/hook/trigger can I use to run code exactly when a character is created?
Or am I going about this all wrong and should do it in a completely different way?

Cheers
PhoeNox

Moon Wizard
December 8th, 2020, 02:10
It depends on what you mean about "when a character is created".

Ultimately, a PC is just a database node with a bunch of child nodes stored under the campaign "charsheet" database node. (i.e. charsheet.id-00001, charsheet.id-00002, ...)

If you want the data to be updated when the sheet is opened, then you would do something like you are doing now. This has the caveat that you will need to check to see whether the changes have been made before, since the script will run every time that the PC sheet is opened on either the GM or the player client.

If you want to trigger some sort of event on the GM side, you could use a global script to add a database handler check for when a child node is added to "charsheet"; and then run a script function based on that event to make modifications. This will allow the changes to occur, even if neither GM nor player open the sheet.

If you provided detail on what you are trying to do, that might help give a framework to help answer.

Regards,
JPG

PhoeNox
December 9th, 2020, 20:21
Thank you for your reply, it sounds like I'll have to use the "sheet has been opened" hook.
The changes I'd like to make are simple enough that I can check every time whether I have to make the changes, just like you suggested.

One caveat I ran into was that I could not access the "window" variable, because during the execution of "onInit()", the window doesn't exist yet.
For that reason, I added the scripts to the corresponding "frame_char" items in the charsheet. This also feels better, since I can define the scripts next to the places where they do their work.


For completion, here are the two (very simple) scripts I created, for an extension that fiddles around a bit with the FATE Core RPG character sheet:

The first one creates three empty aspect slots in the "Other Aspects" frame:


function onInit()
local aspectList = window.getDatabaseNode().getChild("aspectlist");
local numberOfAspects = aspectList.getChildCount();
if numberOfAspects == 0 then
for i=1,3 do aspectList.createChild() end
end
end


The second one creates the default consequences - one each for mild, moderate and severe:


function onInit()
local consequenceList = window.getDatabaseNode().getChild("consequencelist");
local numberOfConsequences = consequenceList.getChildCount();
if numberOfConsequences == 0 then
local consequenceLabels = {"MILD", "MODERATE", "SEVERE"};
for i=1,3 do
local consequence = consequenceList.createChild();
local label = consequence.createChild("label", "string")
label.setValue(consequenceLabels[i])
end
end
end

Moon Wizard
December 9th, 2020, 22:59
If you're running a script at the window level, then "window" is not relevant. Just use getDatabaseNode().

Regards,
JPG