Question on While functionality.
Foxxpaw
07 Jul 2016, 04:32Okay so, a few questions on using "while..."
1: is it possible for the script to check multiple while statements? Like, if I said:
set attribute1 = 0
While (attribute1 = 0) {
do function1()
attribute1 = attribute1 + 1
}
While (attribute1 = 1) {
do function2()
attribute1 = attribute1 + 1
}
theoretically, it should set attribute1 to 0, then run function1, increase the attribute1 value to 1, then run function2 and increase attribute1 to 2, but this doesn't seem to be working.
2: If the while... statement doesn't work, is there a way to constantly check an "if..." statement instead?
3: And I'm sure there's a much better way to do this, ANY suggestion is very very welcome here XD I'm not sure what I'm doing wrong right now ^^;
hegemonkhan
07 Jul 2016, 04:54I'm still a code noob, but I think you're going to likely hit a lot of (logic) issues ('while' in quest seems to have lots of issues on its own), especially with trying to chain multiple 'whiles'... Jay, Pixie, and etc others, can probably get into (due to understanding coding much better than I) the logic of chaining 'whiles' better and can give a more detailed and accurate assessment of it.
also, due note that with coding, you NEVER can have an "infinite loop"... looping must terminate! (your computer's resources, memory/space, will run out, as they're finite, whereas the looping is infinite - and thus uses memory/space - thus uses infinite memory/space, and thus quest and/or the computer will crash)
with quest, using recursive (looping) Functions works well, as long as you keep them simple and/or do tail-recursion, whereas again, 'while' has more issues.
so, try to use Functions and the 'if/switch' Scripts/Functions, for what you want, and have the Function call itself (at the end of its scripting: "tail recursion")
here's an example of many looping funtions:
http://textadventures.co.uk/forum/samples/topic/4988/character-creation-crude-code-and-sample-game
(my poorly coded character creation earlier snapshot of my still conintuing progress on it, lol)
HK Edit:
oops, I totally forgot about using 'for' and 'foreach', they're useful for/with incrementing/decrementing (addition~muliplication/subtraction~division) an Attribute, too.
bergedorfcacher
07 Jul 2016, 05:45When I saw your code I thought it should work fine. So I made a quick test:
x = 0
while (x=0) {
msg ("Message 1")
x = x+1
}
while (x=1) {
msg ("Message 2")
x = x+1
}
Output:
Message 1
Message 2
If you have problems, maybe your functions change atrribute1?
BTW: I assume this was just test code to find out how while works. Because if your functions do not change the attribute your code shold be equivalent to:
set attribute1 = 2
do function1()
do function2()
Jay Nabonne
07 Jul 2016, 06:55If it helps, to invoke a function just use
function1()
or even just
function1
if it has no arguments. There is no "do function1()"
The Pixie
07 Jul 2016, 07:09As far as I can see, you would achieve the same by doing this:
attribute1 = 0
function1()
attribute1 = 1
function2()
attribute1 = 2
Or even just this, if attribute1 is not used elsewhere
function1()
function2()
Is there something I am missing here?
Foxxpaw
07 Jul 2016, 10:08Hokay, so to clarify (Maybe I should've mentioned this earlier, didn't think to do so) I'm trying to separate the game into sections, something akin to chapters. The setup I'd like to run on it's own, followed by the prologue, then chapter 1, followed by chapter 2, etc... Technically, I could just chain the whole thing together, but I'm trying to avoid doing that so it's easier for me to skip to the later sections and begin testing from that point on :P
@hegemonkhan
Right. I accidentally nested something in itself and the game was not a happy camper at all. lol. I do have a few functions that loop based on player responses for character creation (Basically, confirm the input was correct for their name, if not, get input again and confirm, etc...) but I'm not sure how I can write up a function that will call each chapter of the game. Right now I have two functions I'm trying to work with, gameSetup and gamePrologue. If there's some way I can write up a function that'll constantly check for the completion of gameSetup before it calls gamePrologue, that would be awesome but I can't think of it for some reason :(
@bergedorfcacher
Right, basically that's what I'm going for but I'm not sure what's keeping it from working. As far as I understand it, those should work, but it just freezes up. This is what I've got in the code:
set (game, "x", 1)
while (game.x = 1) {
msg (game.x)
game.x = game.x + 1
}
while (game.x = 2) {
msg (game.x)
}
Maybe I did something wrong in there, but I'm not seeing anything that would crash it...
@Jay
Right, I did get that part figured out a little while ago running some tests of my own but it still doesn't seem happy with it :(
@The Pixie
Ideally, that would work just fine... I'm not sure why, but quest just keeps going while waiting for input, so instead of completing the character setup (which is nested in function1) it'll ask for the players name and then give them a room description. I have the player starting out in an empty room just to avoid that because even when running just function1, it was printing out the description.
Basically, my understanding of this is that if I said:
function1
function2
then quest would try to run all of them at the same time...not good, since the player would be asked for a name, but even before answering it'll send them to a separate room while it waits to get input.
bergedorfcacher
07 Jul 2016, 10:21while (game.x = 2) {
msg (game.x)
}
You are not incrementing game.x here, so this will run forever.
Jay Nabonne
07 Jul 2016, 11:07So it's not the while problem. The issue is that getting input is non-blocking. It kicks off the input process, but it's processed asynchronously. Your inner script will then be called back once the input is received, but any script after the get input (not in the response block) will be executed as Quest returns out of its call from your script. There is no way to block in Quest. (And if you try - say, by having a while loop that sits forever - then you'll not only hang the game but you'll also hang the Quest editor UI!)
msg("before get input")
get input {
msg("This is called when the input is received")
}
msg("This will execute right away, since the get input call does not block waiting for input.")
Foxxpaw
07 Jul 2016, 11:09Oh wow, hahaha XD How'd I miss that?
So; that works... swapped the msg for functions and that works too. Now I've got to figure out what's going on with the actual game functions :( I'm so confused v.v...haha
Alright, I did some extra testing as well and used this...
set (game, "x", -1)
while (game.x = -1) {
function1
game.x = game.x + 1
}
while (game.x = 0 and game.function1 = "Complete") {
function2
game.x = game.x + 1
}
while (game.x = 1 and game.function1 = "Complete") {
function3
game.x = game.x + 1
}
but I don't understand why that works and the actual code doesn't... It runs the gameSetup but stops on gamePrologue. Just to note, set (game, setup, "complete") is set in the setupConfirmation which is called by the gameSetup function as the last thing that runs.
msg (""Greetings! Welcome to the city of Silverwood. You must be new here. What's your name?"")
while (game.section = -1) {
gameSetup
game.section = game.section + 1
}
while (game.section = 0 and game.setup = "complete") {
gamePrologue
game.section = game.section + 1
}
while (game.section = 1 and game.prologue = "complete") {
gameChapter1
game.section = game.section + 1
}
while (game.section = 2 and game.chapter1 = "complete") {
gameChapter2
game.section = game.section + 1
}
Jay Nabonne
07 Jul 2016, 11:10(Not sure if you saw my response above, as we overlapped. Just in case...)
Foxxpaw
07 Jul 2016, 11:29Right, I got that part figured out, but the first thing that is supposed to happen after it reaches the gamePrologue is print a message. It doesn't print anything, though. Nor does it move the player, or set the game.time attribute. It's like it just stops after running the setup. The gamePrologue is:
msg ("giant wall of text")
set (game, "time", "Sunrise")
MoveObject (player, Player Apartment Bedroom)
bergedorfcacher
07 Jul 2016, 11:55No offense meant, but your code looks very strange. You surely want to play a chapter only once. So having it in a while loop doesn't look right. You probably think you need that to get the input part working. It might help us in helping you if you could point out where in your functions you call get input and where (if anywhere) you change the attributes you use in the while conditions.
Foxxpaw
07 Jul 2016, 12:18@Bergedorfcacher
Yeah, the code is not something I'm happy with but I don't know how else to keep Quest from running like...everything at once, or trying to. Each chapter will probably end up being pretty long so when you look at it in the code there's so many things that get nested it's sort of painful to view, which is one reason I'm trying to split it. The second is for testing purposes. I don't want to play through the prologue just to check a few things in chapter 1 and I most certainly don't want to have to search for the start and end of a chapter with how much will end up going into them.
As for the get Input... That is called once, in the gameSetup under another function: nameSetup which asks for the player name.
nameSetup
get input {
set (player, "alias", result)
Ask (""Okay, your name is " + player.alias + "?"") {
if (result = True) {
set (game, "nameSetup", "True")
speciesSetup
}
else {
msg (""Oh, sorry. What is your name again?"")
nameSetup
}
}
}
bergedorfcacher
07 Jul 2016, 12:46OK, I think you should just try not to put your entire game into one single script. Instead display a message at the end of each part and offer something clickable ('continue with ...') for continuing. That could for example be a command link.
So at the end of nameSetup, display the link for the prologue.
On the command script for the prologue call gamePrologue and after that display a message with a link for chapter 1. And so on.
Or are you calling get input or ask again in the chapters? If yes, the entire approach probably won't work.