Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Dice roll/card draw examples?

A topic by MisterZNatural created Jul 23, 2023 Views: 211 Replies: 3
Viewing posts 1 to 3
(+1)

I've wanted to make a dice roll/card draw generator for the journaling game Alone Among the Stars but I haven't figured out how to get the scripting to work, are these any examples of similar decks that anyone has made for me to look at?

Developer (2 edits)

The Koboldly Go! deck might be looking at. It rolls up randomized kobold NPCs for an adventure game. For each characteristic, there is an invisible grid widget containing a table of options, and the main card script picks a random row. The meat of the thing looks something like this:

on generate do
 gender   .text:random[genders   .value.value]
 eyecolor .text:random[eyecolors .value.value]
 skincolor.text:random[skincolors.value.value]
 voice    .text:random[voices    .value.value]
 mark     .text:random[marks     .value.value]
 friend   .text:random[friends   .value.value]
 bonus    .text:"\n" fuse random[bonuses.value.value -2]
 quirk    .text:"\n" fuse random[quirks .value.value -2]
 item     .text:"\n" fuse random[items  .value.value -2]
end

For eye colors, we retrieve a random element from a column in the table stored in a grid widget "eyecolors" and store it in the text content of the field "eyecolor".

eyecolor.text:random[eyecolors.value.value]

Breaking it down,

  • "eyecolors" is the name of the grid widget containing eye color options.
  • (a dot (".") is one way to index into things in lil scripts; retrieving a part of the whole, or referring to an attribute.)
  • "eyecolors.value" is the table of options contained in that grid; these all contain a single column which (perhaps somewhat confusingly) is named "value".
  • "eyecolors.value.value" is a single column from the above table; a list of strings.
  • "random[eyecolors.value.value]" picks a single string from the above list at random.
  • "eyecolor" is a field widget which will be used to display our random choice.
  • "eyecolor.text" refers to the text content of the field "eyecolor".
  • (a colon (":") is how you do assignment in lil scripts; you can read it as "gets" or "becomes".)

For inventory items, we use a slightly different approach to pick two items without replacement (that is, two distinct items) and display each on its own line:

item.text:"\n" fuse random[items.value.value -2]

Breaking it down,

  • "items" is the name of the grid widget containing inventory item options.
  • "items.value" is the table of options contained in the grid.
  • "items.value.value" is a single column from the above table; a list of strings.
  • "random[items.value.value -2]" picks two strings from the above list at random, without replacement, and returns them as a list of strings. If that was "2" instead of "-2", we'd still get a list of two items, but repeats would be allowed.
  • " "\n" fuse ... " joins the items of a list with a newline character.
  • "item.text" refers to the text content of the field "item".

When you're only dealing with a handful of options, it might be even easier to skip the grid, and use a list of strings directly, like so:

on click do
  result.text:random[
    "it is arduous to get to.",
    "you come upon it suddenly.",
    "you spot it as you are resting."
  ]
end

Does that help clarify things at all?

Depending on how "automated" you want your adaptation to be, you might be able to use the die-roller contraption included in the Guided Tour deck; it doesn't require any scripting to use, just a D&D-style "dice notation" formula like "1d4+2" or "2d10+3d6":


For convenience, the contraption definition is here- it can be copied to your clipboard and pasted directly into a deck:

%%WGT0{"w":[{"name":"roller","type":"contraption","size":[137,23],"pos":[118,101],"def":"dieRoller","widgets":{"result":{"value":"1+5 = 6"},"button":{},"formula":{}}}],"d":{"dieRoller":{"name":"dieRoller","size":[137,23],"resizable":1,"margin":[90,8,8,9],"description":"roll dice using \"1d6+5\" notation.","script":"on roll x do\n r:() i:0\n x:\"\" fuse \" \" split x\n while (count x)>i\n  if x[i]=\"+\"\n   i:i+1\n  else\n   p:\"%[n]id%[die]i%[match]m%[i]n\" parse i drop x\n   r:r,if p.match 1+random[p.die p.n] else p.n end\n   i:i+p.i\n  end\n end\n r\nend\n\non view do button.text:formula.text end\non set_formula x do formula.text:x view[] end\non get_formula   do formula.text          end","attributes":{"name":["formula"],"label":["Formula"],"type":["string"]},"widgets":{"result":{"type":"field","size":[51,19],"pos":[84,2],"style":"plain"},"button":{"type":"button","size":[79,19],"pos":[2,2],"script":"on click do\n v:roll[me.text]\n result.text:if 1=count v\n  v\n else\n  v:\"%s = %s\" format (\"+\" fuse v),(sum v)\n end\nend","text":"2d6","style":"rect"},"formula":{"type":"field","size":[33,20],"pos":[-8,-29],"locked":1,"show":"none","style":"plain","value":"2d6"}}}}}
(1 edit)

I copied the dice roller widget into Decker and tried to open the script to look at it, but I can't find where it is. Nothing in the block of text that I copied and pasted into Decker makes much sense to me.

Developer(+1)

The text representation shown above is just for clipboard interchange purposes; it isn't really intended for editing by hand.

If you want to view/edit the scripts comprising a contraption, open its Prototype  by either choosing "Prototype..." from the properties panel of an instance of the contraption or choosing "File -> Prototypes...", selecting the prototype, and clicking "Edit...". While in the Prototype editor you can then choose "Prototype -> Script..." to view the main script.

For additional information, see the custom widgets tutorial.