Producing a SIMPLE random response

XanMag
I would like to have a generic blanket statement like: "You hear some shrieking, look up, and notice the bats have flown off to the"

and would like to randomly choose one of the cardinal directions. Without adding a string list to randomly choose from, is there a way I can simply plop a direction in there?

Thanks.

lightwriter
I'm not entirely sure what you mean but this should work:

 x= GetRandomInt(1, 4)
if (x = 1) {
direction = "north"
}
else if(x=2){
direction = "east"
}
else if (x=3){
direction = "south"
}
else if (x=4){
direction = "west"
}
msg("The bats have flown off to the " + direction + ".")

XanMag
That makes sense, but where would I put that?

Is this an expression in the GUI?
msg("The bats have flown off to the " + direction + ".")

And then, is there an if statement just below that with the 1,2,3,4 options?

Sorry. I'm coding clueless a lot of times! :lol:

lightwriter
XanMag wrote:That makes sense, but where would I put that?

Is this an expression in the GUI?
msg("The bats have flown off to the " + direction + ".")

And then, is there an if statement just below that with the 1,2,3,4 options?

Sorry. I'm coding clueless a lot of times! :lol:

It's fine just go into code view of the script (not the entire project and paste it exactly as I have it...
the IF statement has to go first because it reads the lines in order and won't know what the variable direction is unless it's defined...
Yes it is an expression because it uses both text and variables

Anonynn
What I ended up doing was putting a text processor command in the room description.

So like

{random:{once:You happen to hear some shrieking from the north which echoes down the long hallway before you:You hear something screaming from the east wing:You hear something happening to the south and it makes you feel uneasy.}}

That way the player hears it once, and gets all freaked out :D

XanMag
@lightwriter - worked like a charm! Saving this thread as it will come in good use!

@Neonayon - I've never used the {random:{once: - would that randomly choose one of those statements to print? Like... could I put that script in the 'after entering room' script for some of my rooms and it would pull from those messages? And is the 'once' part of that so it only chooses one from the list rather than it would only be read one time, period? If so, that'll be a nice little tool to have to!

Thanks both!

HegemonKhan
here's the info on text processor commands:

http://docs.textadventures.co.uk/quest/ ... essor.html

I'm not sure on the syntax needed as I've not used these text processor commands yet (and am too lazy to test myself), I'm not sure if you'd do (this is only based upon my own logic with coding, so I myself have no idea which is correct):

(spaces are just to help you see it better)

(this is for if you wanted all of the phrase choices to be only done a single time: aka 'once', lol)

{ random : { once : phrase1 : phrase2 : phrase3 : phraseETC } } ~~~~~ neonayon's syntax structure
~ OR ~
{ once : { random : phrase1 : phrase2 : phrase3 : phraseETC } }
~ OR ~
{ random : { once : phrase1 } : {once : phrase2 } : { once : phrase3 } : { once : phraseETC } }

----

if you wanted to just do one (or less than all) of the choices only once, while the other choices are done repeatedly... the syntax would be either neonayon's or some other syntax not shown above (too lazy to write out these possible syntaxes too, lol)

----

the ' {once:text} ' text processor command is the same as the 'firsttime' Script~Function command

---------------------

you can also (a little more application of it) take lightwriter's code and put~add it into a (your own custom) Function, with a return type, which would allow you to insert that direction word into whatever you want, and~or also have a Parameter, so if you wanted to to select the direction yourself (just one code example of many):

----

Random Selection:

<function name="whatever_function">
random_direction_variable = get_random_direction_function
msg ("You decide to travel " + random_direction_variable + ".")
</function>

<function name="get_random_direction_function" type="string">
random_integer_variable = GetRandomInt (1, 4) // the '4' is just for N-E-S-W, use a larger number and adjust code if you want to do more directions
if (random_integer_variable = 1) {
return ("west")
} else if (random_integer_variable = 2) {
return ("east")
} else if (random_integer_variable = 3) {
return ("north")
} else if (random_integer_variable = 4) {
return ("south")
}
</function>


------------

Human Selection:

<function name="whatever_function">
msg ("(1) west, (2) east, (3) north, or (4) south")
get input {
ClearScreen
if (IsInt (result) and ToInt (result) > 0 and ToInt (result) <= 4) {
direction_variable = get_direction_function (result)
msg ("You decide to travel " + direction_variable + ".")
} else {
whatever_function
}
}
</function>

<function name="get_direction_function" type="string" parameters="choice">
if (choice = "1") {
return ("west")
} else if (choice = "2") {
return ("east")
} else if (choice = "3") {
return ("north")
} else if (choice = "4") {
return ("south")
}
</function>


----------

you could use lists and dictionaries too, to make this even nicer for yourself~humans (not sure which is actually more efficient code-wise though), but this is a bit more dificult to do, if you're not ready for it.

--------------------------

my~this post's content is basically the same as using the text processor commands (maybe they work behind the scenes *somewhat* similarly to my stuff), such as '{random:x:x:x:etc}', so for simple stuff, use it. However, if you want to do more complex stuff, you're going to have to create stuff similar to this post's content, to handle that complexity.

jaynabonne
This gets back to something I'd love to see: a text processor command that invokes a function and inserts that string result in the output.

Perhaps I should get off my behind and make a pull request for it. (I think I hacked one together once.)

XanMag
I have this script nested into a pretty deep bit of code:

if (RandomChance(20)) {
x = GetRandomInt(1, 8)
if (x = 1) {
direction = "north"
}
else if (x=2) {
direction = "east"
}
else if (x=3) {
direction = "south"
}
else if (x=4) {
direction = "west"
}
else if (x=5) {
direction = "northwest"
}
else if (x=6) {
direction = "southwest"
}
else if (x=7) {
direction = "northeast"
}
else if (x=8) {
direction = "southeast"
}
msg (<br/>"The circling bat must have noticed something other than you to pester. You see it fly off to the " + direction + ".")
MoveObject (average bat, Item Warehouse)
average bat.presentin = false
}


I got similar things to work in my test games but I get this error: Error running script: Error compiling expression '<br/>"The circling bat must have noticed something other than you to pester. You see it fly off to the " + direction + "."': SyntaxError: Unexpected token "<"; expected one of "NOT", "-", <INTEGER>, <REAL>, <STRING_LITERAL>, "True", "False", <HEX_LITERAL>, <CHAR_LITERAL>, "null", <DATETIME>, <TIMESPAN>, "(", <IDENTIFIER>, "if", or "cast"Line: 1, Column: 1

If it is a major pain to correct, I'll get rid of it and do something far simpler. It's really just placed in there as a learning thing for me!

Thanks!

HegemonKhan
does the <br/> need to go inside the double quote?

from:

msg (<br/>"The circling bat must have noticed something other than you to pester. You see it fly off to the " + direction + ".")

to:

msg ("<br/>The circling bat must have noticed something other than you to pester. You see it fly off to the " + direction + ".")

----------------

you probably just have one off-character, as the error thinks it's finding a, < , symbol out of place (though it could be any character that is throwing off the syntax):

unexpected token: <

Pertex
And did you surround your script with
<![CDATA[
...
]]>
?

If you use html tags within your ASLX-file Quest tries to interpret these tags as part of the ASLX syntax. So you must comment out every non ASLX tag with CDATA

The Pixie
Quest will add the CDATA bit for you if you are just looking at the code view for a specific script (as opposed to the F9/File - code view).

XanMag
HegemonKhan wrote:does the <br/> need to go inside the double quote?

from:

msg (<br/>"The circling bat must have noticed something other than you to pester. You see it fly off to the " + direction + ".")

to:

msg ("<br/>The circling bat must have noticed something other than you to pester. You see it fly off to the " + direction + ".")



So... if you run an "expression", you can't have an enter/return to add a space unless it is inside the quotes? Hmmm... makes sense I guess?

I totally don't understand it... but that worked! Thanks, HK. Thanks all!

HegemonKhan
you may need to do the 'CDATA' (if you done this directly yourself within the code ~ if using the GUI~Editor it will do the 'CDATA' for you automatically and hiddenly: you don't see it being done), I'm just not familar with how the html formatting (line breaks and etc, like the <br/>) works, if you need the 'CDATA', just enclosing it within the double quotes, or if you need both the double quotes and 'CDATA' tags enclosing the (for example) <br/> (and other html formatting tags).

normally, for like using the greater than and etc operators (<, >, >=, <=, and also the 'not method B' operator of: <>), you need the 'CDATA' tags encasing the scripting (script lines which contain the '<' and~or '>' characters~symbols), but I've no idea if the use of the html formatting stuff (like the <br/>) works in the same way or not.

-----------------------------------

MY EXPLANATION ATTEMPT (lol) of it for you XanMag:

------

in code, these symbols~characters:

< and >

are used for two different things (aka: telling quest what they're being used for):

1. the html 'creation' tags (the creation of Quest's Elements):

<asl version="550">
</asl>

<game name="xxx">
</game>

<object name="xxx">
</object>

<function name="xxx">
</function>

<command name="xxx">
</command>

<turnscript name="xxx">
</turnscript>

<timer name="xxx">
</timer>

<type name="xxx">
</type>

<attr name="xxx" type="xxx">
</attr>

2. scripting operators:

greater than: >
example: if (student.score > 90) { student.grade = "A" }

greater than or equal to: >=
lesser than: <
lesser than or equal to: <=
not (alternative B): <>

-----------

(AGAIN, only when you yourself are directly typing in the code, in code: aka when NOT using the GUI~Editor)

without the 'CDATA' tags, quest sees the '>' in (an example):

if (student.score > 90) { student.grade = "A" }

as an ending 'creation' tag, and so it looks for the beginning creation tag as well as the syntax needed, which it obviously doesn't find, and thus gives the error, of having your syntax or character~symbol out of place, aka ~ "error: missing token: blah"

-------

(AGAIN, only when you yourself are directly typing in the code, in code: aka when NOT using the GUI~Editor)

with the 'CDATA' tags:

<![CDATA[[

if (student.score > 90) { student.grade = "A" }

]]>

quest now correctly sees the '>' character~symbol in:

if (student.score > 90) { student.grade = "A" }

correctly, as a greater than symbol~character operator, as it is suppose to be seen as

-----------------

ERROR:

<function name="set_student_grade_function" parameters="student">
if (student.score > 90) {
student.grade = "A"
} else if (student.score > 80) {
student.grade = "B"
} else if (student.score > 70) {
student.grade = "C"
} else if (student.score > 60) {
student.grade = "D"
} else {
student.grade = "F"
}
</function>


vs

NO error:

<function name="set_student_grade_function" parameters="student"><![CDATA[
if (student.score > 90) {
student.grade = "A"
} else if (student.score > 80) {
student.grade = "B"
} else if (student.score > 70) {
student.grade = "C"
} else if (student.score > 60) {
student.grade = "D"
} else {
student.grade = "F"
}
]]></function>

or, if it's easier to see, a separated format for you (either format works, most people here, including me, use the format above):

[code]<function name="set_student_grade_function" parameters="student">
<![CDATA[
if (student.score > 90) {
student.grade = "A"
} else if (student.score > 80) {
student.grade = "B"
} else if (student.score > 70) {
student.grade = "C"
} else if (student.score > 60) {
student.grade = "D"
} else {
student.grade = "F"
}
]]>
</function>

The Pixie
Quest is coded in XML, which you can see if you do File - Code view. XML is all angle brackets, < and >, and if you have them in your content, the XML parser will get confused, so you put it in as CDATA, which tells the XML parser that all that section is content and not XML codes. As said, Quest will do this for you as long as you are not looking at the XML.

If File - Code view you might see this:
<script type="script"><![CDATA[
msg("First line.<br/>Second line.")
]]><script>

In the code view for the script itself, just this:
  msg("First line.<br/>Second line.")


And you need the CDATA for any <, whether in an HTML tag, a mathematical comparison or even in a string.

Quest output in HTML. In HTML all white space (spaces, returns and tabs) and considered to be a single space. If you want a return to get displayed you need to use <br/>. Because that is part of the string to be output, it has to go inside the double quotes.

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums