How do "if" statements work?
WayTooCasualGamer
30 Nov 2016, 00:21Hi, I just started and I know this is a really basic question, but i've been having trouble with if statements and variables in text quests, and the tutorial hasn't given me any answers... any chance someone could help?
Deckrect
30 Nov 2016, 00:28In case you are using the GUI, it is pretty simple. Set an "If" statement, select the condition, and on "Then", add the result or results, as they may be stacked. On "Else If", add other additional conditions. On "Else", add a different effect if the condition does not match. Remember you do not need adding extra "Ifs" nor an "Else". You may create an statement to check "If player is wizard" and then do something. But if it is not a wizard, the script may do nothing by not adding any "Else" or additional condition.
WayTooCasualGamer
30 Nov 2016, 02:27but... idk how to set and "If" statement
Deckrect
30 Nov 2016, 02:41Are you using web browser or offline version? Are you aware an If statement is a script?
hegemonkhan
30 Nov 2016, 02:43there's 4 types of 'if' blocks:
if (CONDTION)
{
// ACTION
}
// if the 'if' chekcing fails, action: (NO action / nothing happens at all)
// ---------------------
if (CONDITON)
{
// ACTION_1
}
else
{
// ACTION_2
}
// ----------------------------
if (CONDITON)
{
// ACTION_1
}
else if (CONDITION)
{
// ACTION_2
}
// optionally as many more 'else ifs' as you want
// -------------------------------
if (CONDITON)
{
// ACTION_1
}
else if (CONDITION)
{
// ACTION_2
}
// optionally as many more 'else ifs' as you want
else
{
// ACTION_3
}
and here's a link on Attributes and the 'if' Script usage:
http://textadventures.co.uk/forum/samples/topic/5559/attributes-and-if-script-guide-by-hk
basically, it's like this:
// (just showing/using a 'if/else if/else' if block for an example)
if (NAME_OF_OBJECT_1.NAME_OF_ATTRIBUTE_1 = VALUE_OR_EXPRESSION_1) {
// ACTION_1
} else if if (NAME_OF_OBJECT_2.NAME_OF_ATTRIBUTE_2 = VALUE_OR_EXPRESSION_2) {
// ACTION_2
} else {
// ACTION_3
}
// or with Boolean Attributes, an example of its syntax (you can mix whatever combinations you want of conditions):
if (NAME_OF_OBJECT_1.NAME_OF_BOOLEAN_ATTRIBUTE_1) { // if (Boolean Attribute = true)
// ACTION_1
} else if if (NAME_OF_OBJECT_2.NAME_OF_BOOLEAN_ATTRIBUTE_2) { // if (Boolean Attribute = true)
// ACTION_2
} else { // if all above Boolean Attribute conditions fail to be TRUE, then do the nested script (ACTION_3) below
// ACTION_3
}
also note that each if block (again 4 types of an if block: if, if/else, if/if else, if/else if/else) are independent from other if blocks, in other words:
if (CONDITON_1) {
// ACTION_1
} else if (CONDITION_2) {
// ACTION_2
}
// is very different from:
if (CONDITION_1) {
// ACTION_1
}
if (CONDTION_2) {
// ACTION_2
}
// because in the upper example (a single 'if' block --- as an 'if/else if' if block):
the 'else if' checking/scripting ONLY occurs if the 'if' checking fails
// whereas in the lower example (2 'if' blocks --- both as an 'if' if block):
the 2nd 'if' checking/scripting/if-block WILL ALWAYS OCCUR, regardless of what happens in the 1st 'if' block, as it's a separate if block.

Anonynn
30 Nov 2016, 03:23I'll explain how it works :)
If statements are basically logic saying --- If this then this, else if this then this. Does that make sense?
If
then
else if
then
else
So if you put this in a script, it would be something like...
If this
then do this
else if this
then do this.
else (if neither of the above)
then do this.
In code it would be...
if (player.variable) {
then msg ("")
}
else if (player.variable) {
then msg ("")
}
else {
msg ("")
}
Do you understand how they work now?
WayTooCasualGamer
01 Dec 2016, 03:08Yes, thank you haha
WayTooCasualGamer
01 Dec 2016, 03:11I still don't know how to add a variable tho..
hegemonkhan
01 Dec 2016, 07:27here's a guide on Attributes and the 'if' Script usage:
http://textadventures.co.uk/forum/samples/topic/5559/attributes-and-if-script-guide-by-hk
in the GUI/Editor, to do Attribute usage (adding/creating/setting/re-setting/changing/altering/manipulating/etc) via scripting:
run as script -> add new script -> 'variables' section/category -> 'set a variable or attribute' Script (set up your Attribute VARIABLE)
and I don't know the script option via using the 'if' Script for putting in your Attribute for your condition, but you can do this (my way of cheating past not knowing the GUI/Editor's script options, lol):
run as script -> add new script -> 'scripts' section/category -> 'if' Script -> (see below)
if [EXPRESSION] NAME_OF_OBJECT.NAME_OF_ATTRIBUTE = VALUE_OR_EXPRESSION
or
if [EXPRESSION] not NAME_OF_OBJECT.NAME_OF_ATTRIBUTE = VALUE_OR_EXPRESSION
or
if [EXPRESSION] NAME_OF_OBJECT.NAME_OF_BOOLEAN_ATTRIBUTE // if Boolean Attribute's Value is 'true'
or
if [EXPRESSION] not NAME_OF_OBJECT.NAME_OF_BOOLEAN_ATTRIBUTE // if Boolean Attribute's Value is NOT 'true' (aka: if 'false')
or
if [EXPRESSION] NAME_OF_OBJECT.NAME_OF_ATTRIBUTE <> VALUE_OR_EXPRESSION // the 'xxx <> xxx' is the same as 'not xxx = xxx'
of course replace my capitalized stuff with your stuff
WayTooCasualGamer
02 Dec 2016, 00:54I want to do something that might look like this (I know this isn't the real way to code with this program, but it might be similar)
If
Player_Object_Sword = 1
and
Player_Room = Shop
Then
msg "Want to sell your sword?"<
If
Answer = Yes
Then
Set Player_Object_Sword to 1
msg "You sold your sword"

Anonynn
02 Dec 2016, 04:24I think I know what you're saying. I have a few simple ways but there are probably better ways to go about it.
1st
msg("Do you want to sell your super awesome deluxe mega blade from hell?")
get input {
switch (LCase(result)) {
case ("yes", "y") {
msg ("You get rid of that limp biddy sword.")
default {
msg ("You decide against selling your super awesome deluxe mega blade from hell!")
}
}
}
That's how it would look in code.
All you do is create a message leading up to prompt where the player has to place input --- like saying "yes" and anything else will be automatic "no".
2nd
msg ("Would you like to sell dat swordz?")
menulist = Split("Yes;No", ";")
ShowMenu ("Question Here If wanted instead of the message above.", menulist, true) {
if (result = "Yes") {
msg ("Sure. I hate that stupid sword. It smells funny!")
}
else if (result = "No") {
msg ("Nah. I got this, you hang onto your dough.")
}
}
The "true" in there is indicating whether you want the question skippable. If so, put "false".
Do you understand it a little better? Did that solve the issue?
hegemonkhan
02 Dec 2016, 04:45the built-in 'ask' Function is a 'yes:true/no/false' questioning design, so you don't have to create one via your own 'yes/no' Stringlist Attribute (unless you want an in-line design and there's no in-line 'Ask' Function):
(also, you don't need the 'cancel' option of 'show menu', as you have the choice of only 'yes or no', and anwering 'no', is directly canceling out of it)
http://docs.textadventures.co.uk/quest/scripts/ask.html
ask ("Sell?") {
// this function automatically does this:
// options/choices: yes:true/no/false
// result = YOUR_SELECTION/CHOICE // (YES:true/NO:false)
if (result) { // if (result = true) // if yes
// do your selling scripting
} else {
// do NOT do your selling scripting / do your non-selling scripting
}
}
I'm not sure if there's also an in-line (as hyperlinks in the big text box on the left side) 'Ask' Function or not.
The 'ask' Function is a popup window menu.
WayTooCasualGamer
03 Dec 2016, 01:50Here's my first game that I made: http://play.textadventures.co.uk/Play.aspx?id=editor/33b9b7bb-3818-4207-9836-0d630b2470cf%2fDevistation.aslx
I didn't know how to use variables/if statements, so I literally just made 6 copies of every room for each scenario. Lol, it took forever, and I don't want to do it again. Maybe playing my game through really fast will give u an idea of what i want to do.
hegemonkhan
03 Dec 2016, 07:52try to learn Attributes and the 'if' Scripts, as they're the two super scripts ("bread and butter") of being able to do 90% of everything that you want to do in your game. Let us know if you need help and with what you need help with, if you get stuck with learning to use Attributes and the 'if' Script.