Making a dice roller BBCode plugin

So I'm making a dice roller BBCode plugin and am running into a bit of a problem, every time the page is reloaded the value is reloaded.

Is there a way to have the code only rendered once and not re-evaluated each time?
    protected $_tags;
    public function getTags()
        $this->_tags = parent::getTags();
        $this->_tags['dice'] = array(
            'hasOption' => true,
            'optionRegex' => '/^[0-9]+?$/i',
            'callback' => array($this, 'renderTagDice')
        return $this->_tags;
    public function renderTagDice(array $tag, array $rendererStates)
        $output = "A ".$tag['option']." sided die was rolled it came up: ".rand(1,$tag['option']);
        return $output;
I'm following the guide at and while its a great start I know I'm missing something


Well-known member
Posts are rendered on page view. The only way would be able to do this was to create a multiple option system takes an ID for the post or actual roll and store it in a database. This could be really tricky to accomplish for you.
Is there a way to take the result after the output has been define and replace the [dice] tags with the resulting text? So when they type in the post it will look like [dice=20][/dice] and after its posted the database would only see "A 20 sided die was rolled and it came up: 19" removing the BBCode so it doesn't get generated again on reload.


Well-known member
That would require determining the post ID that its being rendered from. This also results in multiple implications:
  1. Direct post editing inside of the database
  2. Could possibly overrun your maximum post size, and would need to be handled.
  3. Not easy to modify the dice size if needed at a later date.
  4. Where to handle the dice roll & save point
This could be a very, very difficult task. I'm not even sure if the post id is passed to the tag being rendered.
If I could get the date of the post somehow I could always use that as a seed value for random number generation. However I think this will have the same problems that you've outlined above as far as passing information.

Does anyone know of a way for the to extend the BbCode model to allow passing of post information to the Formatter?


Well-known member
You'll be modifying the post parser, not BBCode. You would want to modify the contents of the array passed to $tag. I haven't looked into a way to retrieve said information. You could be better served by creating a separate "roll" add-on where the tag option is the ID of a previous roll created via your add-on. It may not be the easiest, but it could allieviate some technical difficulties.


Well-known member
The Backend is nearly done.
What can you modify in the backend:
  • You can define own dice with a couple of options as number of sizes with values.
    eg. d20 normal 20sided die, double 10. 20sided die with two times each side 1 to 10. You can specify own die like: a seven sided die with the values 2,5,6,6,10,11,30
  • The rules, to "win" a roll can be set with PHP Callback functions. Multiple functions are delivered by default like highestValue or rollXTimesOverY.
  • You can build wiresets, that can be rolles. Here you can select 1 to all die, a winning rule and some other options.
  • you can modify the look of the die. Each die can have it's own layout in the frontend. This works with background-images. The dieresult will be text in front of the die.
in work:
  • Icon in Editor, that opens a dialog, to create the bb-code for the roll.
A Code to roll a die may be look like:
(Roll 9 10sided die with a difficulty of 7 in the set of wod. This set contiains information, that a die may explode).
(Roll two d4 a d6 and a d10 in the set of sw. This set contains the rule, that the sum must be 32 to win the roll).
You can set in the options which Usergroup can roll die in wich forum and in conversations.
the moderator right "delete roll" is implemented.
And finally:
  • integrated into AdminSearch
  • full Phrased
  • a roll can only made by creating a post. If you edit a post, there is only a placeholder for the roll, so that it can't be edited.
Any chance you'd be willing to share a glance of the source? We're looking to get something simply implemented soon. I'd be happy to donate to fund some caffeine purchases.


Well-known member
I will release it for free here, when it's done. ATM only the backend really works good, and I am working at the frontend.


Well-known member
You could do some hash trickery with unique information about the post (for example post ID) so the dice roll doesn't change on a per post basis, and you also wouldn't need to muck with storing it in the database. It WOULD make it so multiple dice rolls in a single post would yield the same result though.

An example of what I mean could be something like this:

	$postId = 1;
	$sides = 20;

	echo (hexdec(substr(md5($postId), 0, 6)) % $sides);
Obviously not super advanced, but if you want to be able to generate a random number on a per post basis WITHOUT needing to store the result in a backend database/table, it could be useful...