How can I make an NPC randomly say things indefinitely?

Siren Amber
I have an NPC standing in a room and I would like for her to randomly say things indefinitely (or more accurately, for it to "print a message" indefinitely).

I'd like for her to say a few different things in this fashion but I'm really new at using this program so I don't yet get how to use lists. I have, however, managed to make it where when I enter a room, the NPC will say something after a random amount of time using the "GetRandomInt(x,y)" syntax. The problem is, once she has said the message, it does not repeat unless I reenter the room. Is there any way to make it so she will repeat it at random times indefinitely? I'd like it based on time over turns as I want her to seem completely seperate from the player's actions. Could anyone point me in the right direction?

Thanks for reading! :)

The Pixie
Select Timers on the left hand, then click add to create a new timer, give it a name, tick yes to start at the beginning, and give a suitable interval, say 5 seconds (sounds like you have already done this bit). Then try this

if (npc.parent = game.pov.parent) {
switch (GetRandomInt(1, 10)) {
case (1) {
msg ("'So what you going to do next?' says the NPC.")
}
case (2) {
msg ("'Well do something then,' says the NPC impatiently.")
}
}
}


First line checks the NPC is in the same room as the player. Next line is the random bit. Note that on values 3 to 10 nothing happens, which will make interval of random length. You can add more case/msg sections as you like, and adjust GetRandomInt accordingly.

If you have more than one NPC, use the same timer, but just repeat the code, changing "npc" to the object name, and the texts.

Siren Amber
Thank you Pixie! That works wonderfully!

HegemonKhan
wow, that's really ingenius Pixie, hehe. Never knew that the 'GetRandomInt' + 'switch' could be used in this way! Thank you! :D

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

as for repeating ('looping'), you can do it via creating~Adding a Function (add your scripts to~within this Function), and then for the Function's last script line, use (add) the 'call a function' script ( and for it, then simply type in the name of your Function in the box, don't worry about the Parameters, until you understand how they're used, so you can leave the Parameters box blank: aka don't add any parameters in).

here's an example, in code:

<object name="npc">
<inherit name="editor_object" />
<attr name="dialogue" type="script">
hello_function
</attr>
<attr name="displayverbs" type="listextend">dialogue</attr>
</object>

<verb>
<property>dialogue</property>
<pattern>dialogue</pattern>
<defaultexpression>blah blah blah</defaultexpression>
</verb>

<function name="hello_function">
msg ("Hello")
hello_function
</function>

onimike
The Pixie wrote:Select Timers on the left hand, then click add to create a new timer, give it a name, tick yes to start at the beginning, and give a suitable interval, say 5 seconds (sounds like you have already done this bit). Then try this

if (npc.parent = game.pov.parent) {
switch (GetRandomInt(1, 10)) {
case (1) {
msg ("'So what you going to do next?' says the NPC.")
}
case (2) {
msg ("'Well do something then,' says the NPC impatiently.")
}
}
}


First line checks the NPC is in the same room as the player. Next line is the random bit. Note that on values 3 to 10 nothing happens, which will make interval of random length. You can add more case/msg sections as you like, and adjust GetRandomInt accordingly.

If you have more than one NPC, use the same timer, but just repeat the code, changing "npc" to the object name, and the texts.


Wow here I been doing this the hard way and its this easy man I really need to learn more on all the game features lol thank you for posting this response :)

jaynabonne
To respond to an earlier part of this:

function name="hello_function">
msg ("Hello")
hello_function
</function>

If you do this, you will lock up Quest. A thread of execution must exit at some point. This is an infinite loop. You can get away with invoking the same function in a recursive way if the function "bottoms out" at some point and no longer recurses. You can also use the technique in Quest where you re-invoke the same method from a script that is called asynchronously (e.g. via something like "get input" or "show menu"). It's ok in that case because there is no infinite loop.

I deliberately tried an infinite loop in Quest once, and it not only locked up the game, it also locked up the desktop Quest IDE!

HegemonKhan
my bad, in my haste just to give an example, I forgot to put in the checks (if X then keep doing loop, but if Y don't do the loop), so as Jay points out, it's an infinite loop without my missing 'check' (if) scripts.

it should conceptually and code-wise look something like this, an example (using two methods due to quick and poor coding of mine to make it not an infinite loop, lol):

<function name="hello_function">
if (game.flag_1 = true) {
msg ("Hello")
ask ("Do you want to repeat this?") {
if (result = true) {
hello_function
} else if (result = false) {
// see the below comments about the 'else if'
}
}
} else if (game.flag_1 = false) {
// this 'else if' or just 'else' is optional, you don't need it.
// optional: some script: msg ("The looping stops")
}
</function>

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

Support

Forums