PDA

View Full Version : Return result to a database field from a throwdice function



Ardem
August 1st, 2013, 13:39
I am sure this is an easy one for the genius in here but for the life of me I cannot figure it out.

I want to return the 'total' from a dice roll to the initiative total field.

charsheet_mail.xml


<number_charabilitybonus name="initiativetot">
<anchored>
<to>initiative</to>
<position>insidetopleft</position>
<offset>90,0</offset>
<size>
<width>30</width>
<height>20</height>
</size>
</anchored>
<frame>
<name>modifier</name>
<offset>4,4,4,4</offset>
</frame>
<script>
function onInit()
super.onInit();
end

function onDoubleClick(x,y)
local result=1;
local mydata = window.getDatabaseNode();
ChatManager.d10Init(1,window.initiative.getValue() ,"Initiative",mydata);
end
</script>
</number_charabilitybonus>

chatmanager.lua

function d10Init(number, modifer, description, customData)
if control then
local result = 1;
local aDice = {}; -- Specifying Dice is an Arrary
local mydata = customData
for i = 1, number, 1 do -- Counting the number of dice to use
table.insert(aDice, "d10"); -- Using d10 in the Array
end
control.throwDice("initiative", aDice, modifer, description, customData); -- Throwing the dice in chatwindow
print(mydata)
end
end

chat_chat.lua

function onDiceLanded(draginfo)
if draginfo.getType() == "tncheck" then
targetcheck(draginfo); -- Function to check to see number of successes on a Target Number
elseif draginfo.getType() == "initiative" then
totalinitiative(draginfo); -- Function to total initiative
end

if ChatManager.getDieRevealFlag() then
draginfo.revealDice(true);
end

local handler = ChatManager.getDiceHandler(draginfo);

if handler then
return handler(draginfo);
elseif draginfo.isType("dice") then
applyModifierStackToRoll(draginfo);
end
end

function totalinitiative(draginfo)
nTotal=0;
nMod=draginfo.getNumberData()
mydata=draginfo.customData()
local aDisplaydata = {}; -- Create Message Array
for i = 1, #(draginfo.getDieList()) do -- Gets the total number of dice in the list
nTotal = nTotal + draginfo.getDieList()[i].result; -- Adds the number of successes over the Target number
end
nTotal=nTotal + nMod;


DB.setValue(mydata,"number", nTotal) -- Displays Total in Field

aDisplaydata.text = "Initative";
aDisplaydata.font = "systemfont";
aDisplaydata.dice = draginfo.getDieList();
aDisplaydata.dicedisplay = 1;
aDisplaydata.diemodifier = draginfo.getNumberData()

if User.isHost() then -- Adds whos is sending the Message, GM or Player
--if ChatManager.getDieRevealFlag() then
-- aDisplaydata.dicesecret = false;
-- end
aDisplaydata.sender = GmIdentityManager.getCurrent();
else
aDisplaydata.sender = User.getIdentityLabel();
end
deliverMessage(aDisplaydata);
end

Ardem
August 1st, 2013, 13:55
Sorry going to answer my own question however if there is a better way i am all ears.

I needed getDatabaseNode().getNodeName(); not just the DBnode, however I am not sure 100% the difference between the two.

charsheet_main.xml

<script>
function onInit()
super.onInit();
end

function onDoubleClick(x,y)
local result=2;
local mydata={};
mydata.nodename = getDatabaseNode().getNodeName();
ChatManager.d10Init(1,window.initiative.getValue() ,"Initiative",mydata);
end
</script>

chat_chat.lua (note: the change DB.setValue(mydata.nodename,"number", nTotal)

function totalinitiative(draginfo)
nTotal=0;
nMod=draginfo.getNumberData();
local mydata = draginfo.getCustomData();
local aDisplaydata = {}; -- Create Message Array
for i = 1, #(draginfo.getDieList()) do -- Gets the total number of dice in the list
nTotal = nTotal + draginfo.getDieList()[i].result; -- Adds the number of successes over the Target number
end
nTotal=nTotal + nMod;

DB.setValue(mydata.nodename,"number", nTotal) -- Displays Total in Field

aDisplaydata.text = "Initative";
aDisplaydata.font = "systemfont";
aDisplaydata.dice = draginfo.getDieList();
aDisplaydata.dicedisplay = 1;
aDisplaydata.diemodifier = draginfo.getNumberData()

if User.isHost() then -- Adds whos is sending the Message, GM or Player
--if ChatManager.getDieRevealFlag() then
-- aDisplaydata.dicesecret = false;
-- end
aDisplaydata.sender = GmIdentityManager.getCurrent();
else
aDisplaydata.sender = User.getIdentityLabel();
end
deliverMessage(aDisplaydata);
end

Zeus
August 4th, 2013, 10:05
getDatabaseNode() returns a database node object whilst getNodeName() returns the name (or path) of a database node object. DB.setValue() expects to be passed either a database node or a path to a node to alter.

Ardem
August 8th, 2013, 13:32
Interesting as it was passed the database node bur did not like it, perhaps because customdata may not handle that sort of data field. I am not sure.

So in theory it should of worked passing the database node, which I tried on the first occasions.

Moon Wizard
August 8th, 2013, 23:49
The custom data field is saved as a Lua variable within the FG sandbox. Custom data can be set via the dragdata.setCustomData or Comm.throwDice functions; and can be retrieved with the dragdata.getCustomData function.

One note is that the scope of the custom data is for a single session, and will not persist on hot keys or between sessions.

You might try posting the functions that are setting and getting the custom data.

Regards,
JPG

Ardem
August 9th, 2013, 02:37
Progression of parsing from the top code

local mydata = window.getDatabaseNode();
ChatManager.d10Init(1,window.initiative.getValue() ,"Initiative",mydata);
>>>
function d10Init(number, modifer, description, customData)
control.throwDice("initiative", aDice, modifer, description, customData);
>>
onDiceLanded(draginfo)
totalinitiative(draginfo)
>>
mydata=draginfo.customData()
DB.setValue(mydata,"number", nTotal)

As you can see the parsing is not complex, so confused why it works for nodename and not databasenode().

-----------------

Not a huge deal it works, and this was in 2.8 which I am now needing to recode for 3.0, but it might help me understanding if there is a reason why databasenode() would not parse for future reference

Moon Wizard
August 9th, 2013, 03:28
There is no "customData" function, though there is a "getCustomData" function. Perhaps that is the issue? (Assuming that you cut and paste code above)

I don't see anything in the FG code to limit what can be passed as custom date. To FG, it's just a Lua variable, whether it's a string, table, number, etc. The only thing that I can think my be different is that databasenode is actually a special FG created Lua type. I'd have to run tests to figure out more, but just use your workaround for now.

Regards,
JPG

Ardem
August 9th, 2013, 10:33
Well I put the code into my ruleset "Core RPG 3.0 base" and it works using the databasenode().

I just noticed that typo between the two codes, when I deleted and reenter the line I must of put back in the correct syntax. Since I wasn't getting an error message about it and it was just failing I thought my syntax's were correct.

I say the issue is solved. As we say in my world ..... Pibkac - Problem Is Between Keyboard And Chair.