PDA

View Full Version : Comm.throwDice Question



dulux-oz
August 22nd, 2014, 10:22
Hi Guys,

Can't seem to work this one out:

If I have a command Comm.throwDice() in a script, how do I access the die results within that script?

Cheers

Trenloe
August 22nd, 2014, 14:44
The process is asynchronous in order to allow all of the dice to roll and land before looking at the result.

The process is described in the header of manager_actions.lua:

-- ACTION FLOW
--
-- 1. INITIATE ACTION (DRAG OR DOUBLE-CLICK)
-- 2. DETERMINE TARGETS (DROP OR TARGETING SUBSYSTEM)
-- 3. APPLY MODIFIERS
-- 4. PERFORM ROLLS (IF ANY)
-- 5. RESOLVE ACTION

-- ROLL
-- .sType
-- .sDesc
-- .aDice
-- .nMod
-- (Any other fields added as string -> string map, if possible)

I've been through this very process with damned recently. Take a look at the 3.5e manager_action_ability.lua file - this is for making ability rolls (Strength, Dexterity, etc.). There are a few steps you need to do:

Decide on an action type to use for your rolls - this needs to be a valid action type for the ruleset defined in the "actions" table at the top of the manager_gamesystem.lua file in 3.5e.
Write 2 event handlers for the Modifier Handler and the Result Handler, then register these with the event handlers registerModHandler and registerResultHandler in the onInit() function of your action script file - see this at the top of manager_action_ability.lua for an example and the 2 event handlers in the same file: modRoll and onRoll.
If you need to calculate any other modifiers beyond that incorporated into the modifier box, work these out and apply them in the modifier event handler modRoll. For the 3.5e ability roll the mod handler adds the ability bonus, affects from conditions, negative levels, etc..
Then in the onRoll event you get access to the result of the roll and can do what you want with it.

Two things to remember:

You have to do this all attached to an rRoll.sType action type which has to be a valid action defined in manager_gamesystem.lua in your layered ruleset.
The standard to pass info from the place where the Comm.throwDice method occurred is to enclose it in square brackets in the rRoll.sDesc description string and then parse this out in the modifier or result event handlers. The example 3.5e ability roll adds [ABILITY] then the ability name so that this can be extracted in the modifier handler and the related ability modifier calculated and added to the roll modifier. This method can also be used to pass target number info to calculate success/failure in the result handler and lots of other info - a complex 3.5e roll can have lots of data in separate square brackets for effects, conditions, etc..

damned
August 22nd, 2014, 14:59
I've been through this very process with damned recently.

I bet he picks it up way faster than me...

Trenloe
August 22nd, 2014, 16:37
I bet he picks it up way faster than me...
I really hope so. ;)

damned
August 22nd, 2014, 22:42
I really hope so. ;)

Me too...

dulux-oz
August 23rd, 2014, 06:12
OK, so what do the arrays rSource and rTarget contain?

Ie:

onRoll(rSource, rTarget, rRoll)

Actually, more importantly, why isn't the following code working:



function onRoll(rSource, rTarget, rRoll)
print(rRoll.aDice[1].result);
window.nDie.setValue(rRoll.aDice[1].result);
end


I'm getting an "attempt to index global 'window' (a nil value)" error, but the print function is working fine, so I know the onRoll() is being called correctly.

BTW onRoll() is defined in a "Manager" script and is being called by a Button's onButtonPress().

Thanks

Trenloe
August 23rd, 2014, 07:45
OK, so what do the arrays rSource and rTarget contain?

Ie:

onRoll(rSource, rTarget, rRoll)
Look in manager_actor.lua for info in the header on the data structure - and more info can be gleaned by looking in the rest of that script. This is essentially the source of the roll - and the data is essentially the same for sTarget which will usually only be present for a drag/drop event as there is a target (where the roll was dropped).


I'm getting an "attempt to index global 'window' (a nil value)" error, but the print function is working fine, so I know the onRoll() is being called correctly.
The error tells you the reason - "window" is nil, it doesn't exist. As I mentioned above the dice roll event handlers are asynchronous - once the Comm.throwDice() function is used there is no longer any connection to the GUI window where the roll was initiated. The modifier handler and result handler are events that are triggered without a window to reference.


BTW onRoll() is defined in a "Manager" script and is being called by a Button's onButtonPress().
onRoll is an event - it is the result handler of a roll - the very last step in the process listed in post #2: 5 - Resolve Action. You should not be calling it from pressing a button in the GUI. Use the button in the GUI to kick off the Comm.throwDice() operation - making sure to set the action type (rRoll.sType) to a valid action type. You should be using the performRoll function from a global script to initiate the roll. Refer to manager_action_ability.lua in the 3.5e ruleset. The roll is kicked off from code in a control that calls ActionAbility.performRoll - look at the <template name="number_charabilitybonus"> template in template_char.xml for the code that kicks off ActionAbility.performRoll:


function action(draginfo)
local rActor = ActorManager.getActor("pc", window.getDatabaseNode());
ActionAbility.performRoll(draginfo, rActor, self.target[1]);

return true;
end

function onDragStart(button, x, y, draginfo)
return action(draginfo);
end

function onDoubleClick(x,y)
return action();
end
The action(draginfo) function sets up the actor data and then performs the roll with ActionAbility.performRoll. Draginfo will be available if the roll is started via a drag - the onDragStart function, or there will be no draginfo if the roll was initiated by a double click - onDoubleClick.