edited March 2 in ORK Scripting
So after searching the forums for a bit I didn't find anything to do this already. There is something to do it without scripting, but that seemed burdensome to me. I'm sharing this to help others who want to "roll 3d6" or "roll 1d20" in their formulas.


using UnityEngine;
using ORKFramework;
using ORKFramework.Formulas;
using System.Collections.Generic;

namespace ORKFramework.Formulas.Steps
{
[ORKEditorHelp("Roll Dice", "Roll dice and adjust the value", "")]
[ORKNodeInfo("Value")]
public class DiceStep : BaseFormulaStep
{
[ORKEditorHelp("Operator", "The operator decides how this formula step is calculated to the current value of the formula:\n" +
"- Add: Adds this step's result to the current value of the formula.\n" +
"- Sub: Subtracts this step's result from the current value of the formula.\n" +
"- Multiply: Multiplies the current value of the formula with this step's value.\n" +
"- Divide: Divides the current value of the formula by this step's value.\n" +
"- Modulo: Uses the modulo operator, current value of the formula % this step's value.\n" +
"- Power Of: The current formula value to the power of this step's value.\n" +
"- Log: The current formula value is used in a logarithmic calculation with this step's value as base.\n" +
"- Set: Sets the current formula value to this step's result.", "")]
public FormulaOperator formulaOperator = FormulaOperator.Set;

[ORKEditorInfo(separator = true, labelText = "Number of Dice")]
public FormulaFloat numDice = new FormulaFloat();

[ORKEditorInfo(separator = true, labelText = "Dice Type")]
public FormulaFloat diceType = new FormulaFloat();

public DiceStep()
{

}

public override int Calculate(FormulaCall call)
{
int result = 0;
for (int x = 0; x < (int)numDice.GetValue(call); x++)
{
// add one because exclusive max
result += Random.Range(1, (int)(diceType.GetValue(call)+ 1));
}
ValueHelper.UseOperator(ref call.result, result, formulaOperator);
return this.next;
}

public override string GetNodeDetails()
{
return this.formulaOperator.ToString() + " " + this.numDice.GetInfoText() + "d" + this.diceType.GetInfoText();
}
}

}


Hope it helps, if something is wrong please let me know.
Post edited by Warspawn on
  • Hm, since you're only using the value of the numDice and diceType (i.e. basically only use a Value type), you could change these fields to an int instead of the FormulaFloat.
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • but I think then the user will not have the option of filling the value using a Game Variable or some other formula value right?
  • You can use a loop in your dice formula. Start by checking the value. If the value is '0', calculate the dice roll once. If the value is higher than '0,' store the value as a variable--something like 'd_Rolls'. Check the value of 'd_Rolls, and if it's greater than '0', run a dice roll calculation that loops back to your initial variable check after subtracting '1' from 'd_Rolls'.

    You can also invert this to simply run in a loop until the formula value itself reaches '0', subtracting '1' from the value every time it loops and calculating dice rolls as an additive variable change, then either set the final value of your formula to that of your dice variable or simply do that in whichever formula or event step calls the dice dice formula (****REMEMBER TO ALWAYS RESET YOUR VARIABLE'S BACK TO A NEUTRAL VALUE****).

    At the start of my formula list I've got a string of such formulas for d2, d3, d4, d6, d8, d10 & d20, all marked as the same type and it's made my life exponentially simpler.
  • you mean without using the custom step?
  • If you want to get the value from a FormulaFloat, you need to use the GetValue function :)
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • ah, makes sense, thanks! updated OP
Sign In or Register to comment.