Newbie Question: DiceRoll Function? (SOLVED)

EmmaCreative
04 Nov 2016, 03:35Ok, I'm still very new to coding and Quest and am probably jumping in way to deep for my skill level at the moment. BUT, that being said, I am very ambitious: how do I get the DiceRoll Function to work in my game? I copied it from the library into my game, and the scripts and commands I've used to try to get it to work have all created errors. I can't even get it to return a simple number as a result of rolling.
I've tried following the Creating Functions Which Return a Value page in the tutorial, using DiceRoll as the function instead of the adder, and created: Command:test - Print expression: "You roll " +1d6
I have also tried print expression: "You roll " +DiceRoll(1d6) and "You roll " +DiceRoll
I have added 1d6 and 2d6 as parameters for the DiceRoll function.
I get error messages like:
--Error running script: Error compiling expression '"You roll " +DiceRoll': Unknown object or variable 'DiceRoll'
--Error running script: Error compiling expression '"You roll " (1d6)': SyntaxError: Unexpected token "("; expected one of
--Error running script: Error compiling expression '"You roll " +DiceRoll(1d6)': SyntaxError: Unexpected token "d6"
I've also looked at the DiceRoll core library page, but that doesn't really make any sense to me except that I have to format the dice type. I do understand that 1d6 should give a result for rolling one dice with 6 sides, 2d6 = two dice with 6 sides, etc.
Please help. I know nothing about coding, so please explain in the most basic detail, even if something seems as if it should be common, basic knowledge. Thank you in advance for your help and time...
hegemonkhan
04 Nov 2016, 06:13in the GUI/Editor:
(run as script -> add new script -> ) find/select the 'call function' Script (I think it's found in the 'output' section category, but maybe it's within the 'script' section/category) to put in your desired Function for it to be used there, see below:
Function Name: DiceRoll // you type it into the Function 'name' text box // the 'name' String Attribute is the 'ID' for quest, so a Function's 'name' in coding (or through this scripting means in the GUI/Editor) will call/invoke/do/use/activate that named Function when this scripting occurs during game play.
then, click on the Add Parameter button (add your input argument which will be used within the Function's scriping/script-s) which has to be in this form: "NUMBER_OF_DIE_d_NUMBER_OF_SIDES_PER_DIE"
// in code: NAME_OF_FUNCTION
// and if it has/uses Parameters (inputs): (param/arg_1, param/arg_2, param/arg_3, etc more or less params/args)
// so:
// NAME_OF_FUNCTION
// or:
// NAME_OF_FUNCTION (params/args - however many - separated by commas)
// example of a custom made Function:
// example of 'calling' (using) the 'add_function' Function:
returned_value = add_function (4, 6) // using 'literals' (direct values) as your arguments for your parameters
// returned_value = 10 // using a Variable VARIABLE to store the returned value
val_1 = 4
val_2 = 6
returned_value = add_function (val_1, val_2) // using (Variable) VARIABLES as your arguments for your parameters
// returned_value = 10 // using a Variable VARIABLE to store the returned value
game.val_1 = 4
game.val_2 = 6
returned_value = add_function (game.val_1, game.val_2) // using (Attribute) VARIABLES as your arguments for your parameters
// game.returned_value = 10 // using an Attribute VARIABLE to store the returned value
for examples:
"1d6" // a normal 6 sided single die
"2d6" // two normal six sided dice
"3d12" // three 12 sided dice
etc etc etc
Lastly, the 'DiceRoll' Function returns a Value, so its returned value must be set to be stored into a VARIABLE or used within another Script/Function, for examples (in code):
// setting/storing the returned value into a VARIABLE (an Attribute VARIABLE):
game.dice_roll_value = DiceRoll("1d6") // in the GUI/Editor, you would use the 'set a varable or attribute' Script -> set variable game.dice_roll_value [EXPRESSION] DiceRoll("1d6")
// game.dice_roll_value = {some randomly choosen number within 1-6}
// you can then use this Attribute (anywhere in your game which is why using Objects+Attributes are so useful for game making), for example:
msg ("You rolled: " + game.dice_roll_value)
// outputs: You rolled: {some randomly choosen number within 1-6}
// or:
// using the 'DiceRoll' Function within another Script/Function:
// (You almost got it right!, just slightly incorrect syntax/format - coding makes you a very good spell/grammer/typo checker- lol - as one tiny typo and even a huge amount of code won't work - argh!, you need the ' #d# ' to be within double quotes: "#d#")
// (You tried every syntax combination, except the right one, hehe. This is good, as you did understand what you're suppose to do, which you demonstrated by trying your combinations of syntax! Good job!, you're already learning to code! hehe)
msg ("You rolled: " + DiceRoll("1d6"))
// outputs: You rolled: {some randomly choosen number within 1-6}
// if you don't do something with a returned value from a Function/Script (aka, like: set it to a VARIABLE or use within another Script/Function) , you get an error!
http://docs.textadventures.co.uk/quest/functions/corelibrary/diceroll.html
let me know if this was confusing, and I'll try to explain/walk you through it better... (I'm not good at explaining... sighs)

EmmaCreative
04 Nov 2016, 06:40Thank you so much for your reply! Yes, okay, I'm very confused. =( Isn't the DiceRoll function as copied from the core library ready to use as is? How do I do that? And what do the "//" in front of some items mean? How do I use the information in the link about DiceRoll that you gave me?

EmmaCreative
04 Nov 2016, 06:52HOLD EVERYTHING!! I think I finally got it! The 1d6 needs to be in quotes! Parameter = "1d6"
omg why was that so hard for me to understand?? I guess I'm not used to looking at stuff with such specific detail...
Thank you Hegemon! =)
hegemonkhan
04 Nov 2016, 07:13yes, the 'DiceRoll' Function is ready to be used, you just have to: run as script -> add new script -> 'call function' Script, where you want to use it, type in 'DiceRoll' for the Function's 'name' text box, and click on the 'add parameter' button to give it your "#d#" parameter value, for example: "1d6"
for example:
'orc' Object -> 'attack' Verb -> (see below)
run as script -> add new script -> 'output' section/category -> 'print a message' Script -> (see below)
print [EXPRESSION] "You attack the " + this.name + " for " + DiceRoll("1d6") + " damage.")
// outputs, for example: You attack the orc for 6 damage.
simple (text/string) 'print/msg' Scripts:
// in GUI/Editor:
print [MESSAGE] Hi, how are you?
// and the same thing:
print [EXPRESSION] "Hi, how are you?"
// again, same thing, but in code:
msg ("Hi, how are you?")
the [MESSAGE] option only allows for text/strings (and you don't need the double quotes, as quest GUI/Editor handles the double quotes for you underneath/hiddenly, this makes some slight variations in the syntax... which gets confusing as you can see. However, if you do put in double quotes, they will be displayed, so if you want to narrate like someone is saying something, those double quotes will be displayed if you put them in here)
the [EXPRESSION] allows for both text/strings and VARIABLES (you're doing coding, so you need the double quotes on your text/strings to tell quest that they're text/strings. If you want double quotes to be displayed, you need to use the special 'escape' character: \, and then with no space in-between, the double quote, so like this: \", see below for example)
print [MESSAGE] "Hi, how are you?," asks HK.
// outputs: "Hi, how are you?," asks HK.
print [EXPRESSION] "\"Hi, how are you?,\" asks HK."
// outputs: "Hi, how are you?," asks HK.
if the '\' isn't working... then it's suppose to be the '/', I get confused as to what is the escape character in quest (if it's the backslash or forwardslash), lol.
// in code:
msg (""Hi, how are you?," asks HK.")
// outputs: "Hi, how are you?," asks HK.
complex (text/string and VARIABLE/S) 'print/msg' Scripts are not easy to do... but, fortunately, there's a trick to doing them:
http://textadventures.co.uk/forum/samples/topic/5559/attributes-and-if-script-guide-by-hk#39951
player.alias = "HK"
msg ("Hi, how are you? My name is " + player.alias + ".")
// outputs: Hi, how are you? My name is HK.
3 chunks:
1. "Hi, how are you? My name is " // string/text chunk
+ // concatenation (literally putting together / next to each other) operation of string/text and Attribute Variable
2. player.alias // Attribute VARIABLE chunk
+ // concatenation (literally putting together / next to each other) operation of Attribute Variable and string/text
3. "." // string/text chunk
does this help, or did I just make you even more confused? (a habit I have, as I'm not good at explaining...sighs)
hegemonkhan
04 Nov 2016, 07:21my use of '//' is quest's coding for 'comments' (for people/humans looking at code, to read; anything on the same line after the // is NOT to be used as a code line by quest), so I use them in posts, to also signify my comments.
So, do not write/type in the '//' and whatever follows it (on the same one line/long multi-line-wrapped one line). Those are jsut my comments to you!
comments are very important, to explain what a code chunk is doing; aka "documentation", as even the writer of code will forget what it does after some time passes, lol, let alone anyone else trying to understand what your code is doing/does.
hegemonkhan
04 Nov 2016, 07:43Addition operation vs Concatenation operation:
Addition:
5 + 5 = 10
55 + 55 = 110
Concatenation:
"5" + "5" = "55"
"55" + "55" = "5555"
"mama" + "mia" = "mamamia"
"mama" + " " + "mia" = "mama mia"
"mama" + "5" = "mama5"
"mama" + "5" + "mia" = "mama5mia"
"mama" + " " + "5" + " " + "mia" = "mama 5 mia"
"5" + 5 = ERROR! Both computers and humans have no idea how to combine a string/text and an integer amount // differing data types:
// "5" : string/text data type
// and/vs
// 5 : integer data type
hegemonkhan
04 Nov 2016, 07:49"HOLD EVERYTHING!! I think I finally got it! The 1d6 needs to be in quotes! Parameter = "1d6"
omg why was that so hard for me to understand?? I guess I'm not used to looking at stuff with such specific detail...
Thank you Hegemon! =) (EmmaCreative)"
Even more baffling......
why did HK write such a huge long post, instead of just posting:
you just need to put your '1d6' into double quotes: "1d6"
for example (in code):
game.returned_value = DiceRoll("1d6"))
or
msg ("Dice rolled: " + DiceRoll("1d6"))
???? lol, hehe
HK loves being verbose, confusing, and overwhelming his post readers with coding information and vocabulary/terms, when they don't know any coding at all (and loves talking in 3rd person about himself), scaring them all away! sighs.

EmmaCreative
04 Nov 2016, 07:53Thank you, HK, I appreciate your help. =)
So now the next step of my problem is, how do I use a dice roll to set a starting Stamina attribute score? eg. Roll one die and add 6, this is your starting Stamina score. Keeping in mind that the Stamina score will be changed as the game continues on.

EmmaCreative
04 Nov 2016, 08:10Maybe it would be easier to just have the player get a DiceRoll, then add 6 to that themselves and then Get Input "Starting Skill Score"?
hegemonkhan
04 Nov 2016, 08:27you can't just create/add an Attribute through the GUI/Editor to do this, as this method doesn't allow for you to use Functions (as far as I know anyways, lol); you can only give it a direct value (again, as far as I know):
'player' Player Object -> 'Attributes' Tab -> Attributes -> Add -> (see below)
(Object Name: player)
Attribute Name: stamina
Attribute Type: int // since you have to select a data type...
Attribute Value: 0 // or whatever you want it to start as // ...you're stuck with giving it a value, so, no DiceRoll's random value
so, instead, you need to use the GUI/Editor scripting to create/add your Attribute:
(scripting can not only also add/create Attributes, but do everything else, manipulation/changing/altering, as well, such as addition/subraction --- increasing/decreasing your attribute's value)
(you're using the Text Adventure, right? If using Game Book, let me know)
'game' Game Object -> 'Scripts' Tab -> 'start' Script (the script/s you add to this built-in 'game.start' Script Attribute is/are the first thing/s done/run after you start playing your game --- good for doing, character creation / initial value, stuff) -> (see below)
run as script -> add new script -> 'variables' section/category -> 'set a variable or attribute' Script -> (see below)
(I don't know the GUI/Editor scripts/options, so I'm going to use the [EXPRESSION] option, so I can type in the function call)
// this scripting line creates/adds the 'stamina' Attribute to ( actually/technically within) your 'player' Player Object (just as the above adding/creating method does; you can create/add Attributes either way within the GUI~Editor):
set variable player.stamina = [EXPRESSION] DiceRoll ("#d#") // replace my #s with whatever values/numbers you want, of course
and that's one way (using the 'DiceRoll' Randomization Function) you have a Random starting Attribute value.
there's also the 3 other built-in Randomization Functions too:
VARIABLE = GetRandomInt (min, max)
// example: player.stamina = GetRandomInt (1,6) // exact same (well the randomization algorithms of the two Functions might not be the same, to get technical, lol))as using: DiceRoll("1d6")
VARIABLE = GetRandomDouble // this randomly selects a decimal value between: 0.0 and 1.0, so no parameters (no input value by you is needed/used), usually this is more implemented with having this returned decimal value be multiplied to some value, such as to get a percent value (VARIABLE = 100 * {GetRandomDouble --> 0.7} = 70), or to make something random, or even more random...
and this one's a bit different, dealing with the percent change of something being true (or false):
if (RandomChance (70)) {
msg ("hi")
} else {
msg ("bye")
}
// there's a 70% change of 'if', of it'll output: hi, and thus only a 30% change of 'else', of it'll output: bye
(this is a bit more advanced stuff, as it involves using list/dictionary Attributes --- which are a bit of a step up from your basic Attributes of strings, integers/doubles, and booleans)
Using the 'GetRandomInt(min, max)' (or you could use the DiceRoll instead) and the 'RandomChance(value)' together, works well for if you want to do 'item drops / treasure chest random items'. The 'GetRandomInt' is used with a List Attribute to select what/which item, and the 'RandomChance' is then used to determine whether you actually get that item or not, for example:
<object name="room">
</object>
<object name="player">
<attr name="parent" type="object">room</attr>
</object>
<object name="candy">
</object>
<object name="chocolate">
<object>
<object name="taffee">
</object>
<object name="gum">
</object>
<object name="chest">
<attr name="parent" type="object">room</attr>
<attr name="item_list" type="objectlist">candy;chocolate;taffee;gum</attr>
// ListCount (this.item_list) = 4 // (candy, chocolate, taffee, and gum)
//
// List's index referencing starts at ZERO, not one (ya it takes awhile to get used to):
//
// 0: candy (1/4 items) = first item in your list
// 1: chocolate (2/4 items)
// 2: taffee (3/4 items)
// 3: gum (4/4 items) = ListCount (this.item_list) - 1 = 4 - 1 = 3 = last item in your list
//
// 4: 'out of bounds/range' ERROR (there is no 5th item in our list / we've went outside of our list)
<attr name="search" type="script">
selected_item = ObjectListItem (this.item_list, GetRandomInt (0, ListCount (this.item_list) - 1))
switch (selected_item) {
case (candy) {
if (RandomChance (90)) {
selected_item.parent = player
msg ("In the " + this.name + " you find a " + selected_item.name + " item.")
}
}
case (chocolate) {
if (RandomChance (75)) {
selected_item.parent = player
msg ("In the " + this.name + " you find a " + selected_item.name + " item.")
}
}
case (taffee) {
if (RandomChance (25)) {
selected_item.parent = player
msg ("In the " + this.name + " you find a " + selected_item.name + " item.")
}
}
case (gum) {
if (RandomChance (10)) {
selected_item.parent = player
msg ("In the " + this.name + " you find a " + selected_item.name + " item.")
}
}
default {
msg ("Too bad, the " + this.name + " was empty, no item for you.")
}
}
</attr>
</object>
<verb>
<property>search</property>
<pattern>search</pattern>
<defaultexpression>You can't search that!</defaultexpression>
</verb>
The Pixie
04 Nov 2016, 09:36Try this:
player.stamina = 6 + DiceRoll("1d6")
msg ("Stamina: " + player.stamina)
The Pixie
04 Nov 2016, 09:43Or create a dialogue panel where the player can spend points on different attributes...
https://github.com/ThePix/quest/wiki/Advanced-UI-Part-04:-Dialogue-Panels
hegemonkhan
04 Nov 2016, 09:45oh wow, looks like you've expanded/improved your previous 'levelib', YESSS!!! :D

EmmaCreative
04 Nov 2016, 22:31I will definitely give those a try, thank you HK, Pixie! I stayed up waaaaay to late last night and my brain isn't working right now. I'll post again when I do have the brains to try those.