PDA

View Full Version : Linking a control to a DB node with a script



Tabulazero
July 12th, 2019, 09:21
Hi there,

I have created a window which needs to display some player info stored in the DB. Right now, I am using an OnInit() script that populates the controls with the DB data and an OnClose() script that update the DB when the window close.

It works but I was wondering if there was not a more efficient way of doing this by using something closer to the source tag which basically link the control directly to the tag and would avoid having to run an OnClose script to update the DB ?

Any tip ?

damned
July 12th, 2019, 10:05
Screenshot?
If its only displaying that players data to that player - just set the source to be the charsheet data.
Otherwise look at DB.addHandler

Trenloe
July 12th, 2019, 17:34
The FG "field" controls are specifically designed to be sourced on database fields. It sounds like you're completely re-inventing functionality that is actually there already. When you open a window, there are 2 things it usually needs - a windowclass (required) and a database source (optional, but usually necessary when using database fields) - I'd say over 99% of FG windows have a database source.

Then, by default, the field controls are matched to a database field of the same name - so a string control called "name" would source it's data from the database field "name" at the window datasource level. If you don't want to use the control name as the database fieldname to use, you can add the database source to the control using the "source" property in the control's definition: source="<DB path realtive to the window datasource>l"

For example, in the 3.5E character sheet, the field that displays the current AC total for that character record is: <number_chartotalac name="ac" source="ac.totals.general">

Tabulazero
July 12th, 2019, 18:01
That is how I run my character sheet but the window I am creating is meant to be a template used by multiple players simultaneously. Let's assume that this window needs to display the name of the character which is lineked to the "name" node of the said player in the DB.
If I write something like <Stringfield name="name" source="name"> how does FG know which player I am talking about ?

Trenloe
July 12th, 2019, 18:09
That is how I run my character sheet but the window I am creating is meant to be a template used by multiple players simultaneously. Let's assume that this window needs to display the name of the character which is lineked to the "name" node of the said player in the DB.
If I write something like <Stringfield name="name" source="name"> how does FG know which player I am talking about ?
Open the window with a different "datasource" node for each player.

Character sheets are (usually, depending on which ruleset you're using) stored in the database charsheet.id-XXXXX database node. The first PC sheet would be charsheet.id-00001 the second charsheet.id-00002, etc.. When the window is opened, set the relevant database node as the window datasource. Then the "name" control will look at charsheet.id-00001.name for the window opened with the first character sheet datasource, charsheet.id-00002.name for the second, etc..

Tabulazero
July 12th, 2019, 20:49
Is there a way to pass chargen.id-00001.name as a source via a script ?

I want to create only one window class and be able to pass it chargen.id-00001.name and then if a second player also launches the window to pass it chargen.id-00002.name ?

Trenloe
July 12th, 2019, 21:03
Is there a way to pass chargen.id-00001.name as a source via a script ?
Yes, absolutely.


I want to create only one window class and be able to pass it chargen.id-00001.name and then if a second player also launches the window to pass it chargen.id-00002.name ?
You have control over what the window datasource is when it's launched. How are you opening the window?

Tabulazero
July 12th, 2019, 21:26
A quick screen capture to illustrate what I am trying to do.

27895

I open the diceroller sheet by doublecliking on the character sheet. Do I just need to pass the chargen.id-00001 to the window I am opening via a Interface.openWindow("rollerwindow") command and FG will know which child node to pick ?

Trenloe
July 12th, 2019, 22:47
Do I just need to pass the chargen.id-00001 to the window I am opening via a Interface.openWindow("rollerwindow") command and FG will know which child node to pick ?
Yup.

Just pass in charsheet.id-00001 (or whatever id-XXXXX node you need) to Interface.openWindow and then if you have a field control called "name" it will populate that field with the data from charsheet.id-00001.name

Sorry, earlier I mentioned chargen as the character sheet node, it's actually charsheet - sorry about that. I'm deep in chargen ruleset code at the moment and have it on the brain!