How do I set a variable by asking the player a question in Quest?

0 votes
228 views
asked Mar 30 in Authoring by 12pepjam (1 point)
edited Mar 31 by Alex

for asking someone's name, then printing ("hello"+name)

2 Answers

+1 vote
answered Mar 31 by The Pixie (121 points)

If using the GUI:

Look for "Get input" from the list of scripts (in the Output section,confusingly). You should now see you have two "Add new script" buttons. The one a little to the right and above the other is the important one - that will add stuff that happens when the player has typed his name. Click on it, and select "Print a message".

Where it says "Print [message]" change that to "Print [expression]". Then you can type in what you want to be displayed. The value the player has typed will now be in a variable called "result", so that should be:

"Hello " + result

If you want Quest to remember the name, you need to assign it to an object; the player object in this case (the value of result will get lost as soon as Quest has done this bit). Quest uses names to handle everything, and would get confused if you change a name during a game so will not let you, so set it to be the alias instead.

Click on the same "Add new script" button, and select "Set an objects attribute (named as an expression)". Fill it in as:

Set object [name] [player]
   Attribute: ["alias"]
   Value [result]

Now anywhere in the game you can use:

"Hello " + player.alias

If using code:

If you are happy to use code, just paste this in:

get input {
  msg ("Hello" + result)
  player.alias = result
}
0 votes
answered Mar 30 by hegemonkhan (161 points)
edited Apr 1 by hegemonkhan

direct answer:

http://docs.textadventures.co.uk/quest/guides/character_creation.html


long answer (all of the below):

explanation and then at bottom, answer (in code, as the above link shows how to do it via the GUI~Editor)


As best as I understand Quest (I'm still a newb coder ~ so I don't know how the engine is actually structured+works), so take this bit of my explanation of its structure and terms, with a 'grain of salt', but it might help, even if not correct... lol


quest's terminology:

VARIABLES:

-> Variables: local~temporary: can only be used within its own script block, can't be used in any other script blocks anywhere in the game.

-> Attributes: global~'permanent' (so long as the Object that holds the Attribute, exists, of course): it is 'save-able', and thus 'load-able' (reference-able ~ call upon-able), can be used in any scripting anywhere in the game.

-> Parameters: these deal with Functions, Dictionaries, Commands, and etc. They're like Variables, except that they can transfer (and optionally re-label) between scriptings


ELEMENTS:

http://docs.textadventures.co.uk/quest/elements/

Elements are the OBJECTS of~for its Object-Oriented Structure, which not to be confused with the 'Object' Element (ya, it gets a bit befuddled).


my own terminology:

'scriptings' are the action code lines which set, re-set, and~or alter Attributes and~or do conditional (if) code lines and etc, which are able to be held by (done in) these Elements: Verbs ~ Object's Script Attributes, Commands, Functions, Turnscripts, Timers, and etc


Quest is Object-Oriented Programming structure, so:

in scripting code:

(look up how to write~syntax the Attribute Types: http://docs.textadventures.co.uk/quest/types/ )

Attribute VARIABLE:

Object_name.Attribute_name = Value_or_Expression

examples:

player.strength = 100
game.event_flag = 0
orc.dead = false
player.y = x

player.y = x + 5
player.strength = player.weight + player.energy
player.damage = player.sword.damage + player.sword.damage * player.strength / 100

Variable VARIABLE:

variable_string_name = Value_or_Expression

examples:

result = "blah"
handled = false
you_go_first = true
strength = 100
y = x

damage = player.sword.damage + player.sword.damage * player.strength / 100
y = x + 5

using the GUI~Editor (for initial creation~setting of Attribute VARIABLES):

(whatever, for example: player) Object -> Attributes Tab -> Add -> (see below example)

(Object Name: player)
Attribute Name: strength
Attribute Type: int (integer)
Attribute Value: 100

this is the same as, in scripting:

player.strength = 100

as this is the same as, in code tag blocks:

<object name="player">
  <attr name="strength" type="int">100</attr>
</object>

as for the scriptings via using the GUI~Editor:

I'm not that familiar with it, as I do most of the stuff in code, but figure out its options, to get the same results as I do in code... someone else can maybe help you with it if you need help


anyways..... now to your question:

get input {
  msg ("What is your name?")
  // I type in: HK
  // the quest engine automatically (hidden from you) sets the variable: result = your_typed_in_input
  // conceptually: player.alias = result = "HK"
  player.alias = result
  msg ("Hello" + player.alias + ".")
  // outputs: Hello HK.
}

the most challenging part is getting the syntax for the 'msg' Script's [EXPRESSION] correct

a trick to it, is to break it into two chunks:

1. " text~strings " (including spaces as text~string characters: space bar keyboard key)
2. + VARIABLE +

for example:

player.alias = "HK"
player.sex = "male"
player.race = "human"
player.class = "warrior"

HK is a male human warrior.

msg (player.alias + " is a " + player.sex + " " + player.race + " " + player.class + ".")

its chunks:

(8 of them)

1. player.alias +
2. " is a " ~~~~~~~~~~ " (space) is (space) a (space) "
3. + player.sex +
4. " " ~~~~~~~~~~~~~ " (space) "
5. + player.race +
6. " " ~~~~~~~~~~~~~ " (space) "
7. + player.class +
8. "." ~~~~~~~~~~~~~ " (dot~period) "

now, it's easy to write it correctly:

msg (player.alias + " is a " + player.sex + " " + player.race + " " + player.class + ".")
outputs: HK is a male human warrior.
This site is now closed.
As of 1st November 2015, this site is a read-only archive. For more information see the intfiction forum post

Welcome to IF Answers, a site for questions and answers about Interactive Fiction.

Technical questions about interactive fiction development tools such as Inform, Twine, Quest, QuestKit, Squiffy, Adrift, TADS etc. are all on-topic here.

Non-technical questions about interactive fiction are also on-topic. These questions could be about general IF design, specific games, entering the IF Comp etc.
...