Recursive function not working?
max17
29 Nov 2015, 01:18Hi there everyone! I'm new around here and i just started using this software, and like the title said, I had some problems wit recursive functions. I tried making one recursive function and saving the return value for next use but it won't let me do it. I don't know what I'm doing wrong so i would be really grateful if someone could tell me what i'm doing wrong. Thanks for your help
This is an example code of how i'm trying to implement the function:
Even if i try to do this in the "start" does not work:
It says " Error running script: Error compiling expression 'tryme': Unknown object or variable 'tryme' "

This is an example code of how i'm trying to implement the function:
<function name="tryme" type="boolean">
msg ("Try me?")
get input {
if (result = "no") {
msg (" Okay im sad")
return (false)
}
else if (result = "yes") {
msg ("YaY")
return (true)
}
else {
msg ("What?")
return (tryme)
}
}
</function>
Even if i try to do this in the "start" does not work:
myvariable= tryme
It says " Error running script: Error compiling expression 'tryme': Unknown object or variable 'tryme' "

XanMag
29 Nov 2015, 01:33I'm not the best person (by far) to give advice on this topic, but are you just trying to create relevant feedback to some prompt the player has responded to? Or am I missing something?
If you are trying to illicit a response from player input, especially if prompted by the "game" to do so, I have always used the 'Get Input' option followed by a 'Switch' Script. Below is a snippet from a section of my current game.
The LCase is really important I realized as it forces whatever the player types into lower case. I found that out the hard way! lol
I hope this helps. If not, please clarify your question so a code dummy like me might be able to help!
If you are trying to illicit a response from player input, especially if prompted by the "game" to do so, I have always used the 'Get Input' option followed by a 'Switch' Script. Below is a snippet from a section of my current game.
switch (LCase(result)) {
case ("dingo") {
play sound ("alarm.mp3", false, false)
msg ("CLASSIFIED...<br/><br/>CLASSIFIED...<br/><br/>ACCESS DENIED.<br/><br/>CLASSIFIED...<br/><br/>CLASSIFIED...<br/><br/>ACCESS DENIED.")
wait {
computer loop
}
}
case ("magoo") {
msg ("<b><u>Name:</b></u> Xanadu Magoo<br/><br/><b><u>General Appearance:</b></u> Serious dork. Early thirties. 5'8\". 105 pounds. Light brown hair, hazel eyes, pasty white complexion.<br/><br/><b><u>Occupation:</b></u> Science Teacher at the Holy Moly Incapables<br/><b><u>Ethnicity:</b></u> Caucasian, American<br/><br/><b><u>Specialties:</b></u> Above average knowledge in science, especially physics and chemisty<br/><br/><b><u>Notes:</b></u> Escaped several assassination attempts in the desert. In doing so, killed two desert patrol guards, one via electrocution and one died of complication with an unknown biotic factor. Infiltrated our compound for unknown reasons. Was forced into unconsciousness by Dr. Dingo himself upon arrival. Assassination was to immediately follow, but Dr. Dingo would not allow it. Instead, Xanadu Magoo is currently being help captive in prison cell one by orders of Dr. Dingo.")
wait {
computer loop
}
The LCase is really important I realized as it forces whatever the player types into lower case. I found that out the hard way! lol
I hope this helps. If not, please clarify your question so a code dummy like me might be able to help!

max17
29 Nov 2015, 01:43Whoa that LCase surely is usefull! I would have need it in the future for sure! Many thanks just for that 
Anyway, about my "present needs", I can't "save" the return of my function. I made the function i wrote before and when i try to save the return it passes, the software gave me this error " Error running script: Error compiling expression 'tryme': Unknown object or variable 'tryme' " . So i was wondering if i wrote something wrong in my "tryme" function or if this :
EDIT: sorry for my poor english, it is not my first language

Anyway, about my "present needs", I can't "save" the return of my function. I made the function i wrote before and when i try to save the return it passes, the software gave me this error " Error running script: Error compiling expression 'tryme': Unknown object or variable 'tryme' " . So i was wondering if i wrote something wrong in my "tryme" function or if this :
myvariable= tryme
is not the right way to save the return value for a function.EDIT: sorry for my poor english, it is not my first language

HegemonKhan
29 Nov 2015, 02:36to call~run~activate a Function (or to do the Function again) in code, you just type in the Function's name (and arguments if you got Parameters, of course), so change your:
return (tryme)
to:
tryme ()
and it should work...
also, remember that you need a Variable for assigning the 'tryme' Function's return of 'true' or 'false', to it, or you need to use that returned Value in some way, otherwise, you'll get an error (or it just gets ignored or worse maybe is a memory leak).
----------
I don't think there will be any major problems with doing both a return and a 'msg' in quest (aside from your game design needs), but maybe there might be a problem, too lazy to test it myself.
return (tryme)
to:
tryme ()
and it should work...
also, remember that you need a Variable for assigning the 'tryme' Function's return of 'true' or 'false', to it, or you need to use that returned Value in some way, otherwise, you'll get an error (or it just gets ignored or worse maybe is a memory leak).
----------
I don't think there will be any major problems with doing both a return and a 'msg' in quest (aside from your game design needs), but maybe there might be a problem, too lazy to test it myself.
max17
29 Nov 2015, 02:40Thanks for your answer HegemonKhan, so what should i write if i want to use the "return value" of the tryme function?
Is this not correct?
Is this not correct?
newvariable= tryme()
if (newvariable = true){
//do this
}
else{
//do that
}
HegemonKhan
29 Nov 2015, 02:46yes, or alternatively, you could do an action with it, instead of first assigning it to a VARIABLE:
or
--------------
also, note about quest:
there's 3 types of VARIABLES:
VARIABLES:
-> (1) Variables: local VARIABLES
-> (2) Attributes: global VARIABLES (as long as the Object it is "attached to" exists, of course)
-> (3) Parameters: for Functions and Commands, hopefully you already understand about these, as they're a pain to try to explain, lol
if (tryme = true) {
// do this
} else {
// do that
}
or
<function name="critical_hit" type="int">
// blah scripting:
// if (...) { return 2 }
// else { return 1 }
</function>
some scripting elsewhere, such as a custom (self-made) 'fight' Verb on an 'orc' monster Object:
player.damage = player.weapon.damage * critical_hit ()
// it'll either be conceptually:
// player.damage = player.weapon.damage * 2
// or
// player.damage = player.weapon.damage * 1
--------------
also, note about quest:
there's 3 types of VARIABLES:
VARIABLES:
-> (1) Variables: local VARIABLES
-> (2) Attributes: global VARIABLES (as long as the Object it is "attached to" exists, of course)
-> (3) Parameters: for Functions and Commands, hopefully you already understand about these, as they're a pain to try to explain, lol
max17
29 Nov 2015, 02:48The problem is it does not work both way :S and it tells me the error i wrote in the first post :S
HegemonKhan
29 Nov 2015, 02:51you did change the 'return (tryme)' to 'tryme()', right?
it's in your 'else', see below:
<function name="tryme" type="boolean">
msg ("Try me?")
get input {
if (result = "no") {
msg (" Okay im sad")
return (false)
}
else if (result = "yes") {
msg ("YaY")
return (true)
}
else {
msg ("What?")
return (tryme)
}
}
</function>
it should look like this:
-------
and your:
myvariable = tryme
must be:
myvariable = tryme ()
as you need the parenthesis, to tell quest that the 'tryme' is a Function, and not an Object nor a VARIABLE.
it's in your 'else', see below:
<function name="tryme" type="boolean">
msg ("Try me?")
get input {
if (result = "no") {
msg (" Okay im sad")
return (false)
}
else if (result = "yes") {
msg ("YaY")
return (true)
}
else {
msg ("What?")
return (tryme)
}
}
</function>
it should look like this:
<function name="tryme" type="boolean">
msg ("Try me?")
get input {
if (result = "no") {
msg (" Okay im sad")
return (false)
}
else if (result = "yes") {
msg ("YaY")
return (true)
}
else {
msg ("What?")
tryme()
}
}
</function>
-------
and your:
myvariable = tryme
must be:
myvariable = tryme ()
as you need the parenthesis, to tell quest that the 'tryme' is a Function, and not an Object nor a VARIABLE.
max17
29 Nov 2015, 02:56Yes i changed it, but it continue giving me that error. have you tried it?
HegemonKhan
29 Nov 2015, 02:57here's an infinite loop via recursion, just to show an example of recursion in action, lol:
<function name="bad">
msg ("infinite loops are very bad! Don't use this Function!")
bad ()
</function>
HegemonKhan
29 Nov 2015, 02:59err, seems that you do NOT use the parenthesis, if you don't have any args~parameters, my bad.
max17
29 Nov 2015, 03:02Now i got it why it does not work... I don't know what's the name of the programming language that quest uses, anyway it acts weird or at least, not like any other language i studied. You(I and everyone) can't put a "get input" inside a function that returns a value, IF, that "get input" decides what that return value will be. The code just keeps running and does not stop to "get input" but instead will end the function and return a "no value" or the last return value he finds...
HegemonKhan
29 Nov 2015, 03:15ah, if you're using 'get input' or 'show menu', it's very very difficult to get a loop (such as recursion) to work, as the loop occurs while the first 'get input' or 'show menu' is waiting for input or selection by you, and thus another 'get input' and 'show menu' runs, which causes a crash, as you can't have two of them running at the same time.
---------
quest uses its own langauge: quest's code design structure is similar to Python, but its scripting code is similar to C++ or Java.
Also, for the 'creation' tag blocks, in code, that uses 'XML', XML is similar to a browser language like HTML
------
hmm... maybe if you wouldn't mind posting your entire game code, there may be other things causing problems too, from what you said.
----
we just need to troubleshoot what you got, to figure out what's causing the problem...
-----
here's a recursion in action:
---------
quest uses its own langauge: quest's code design structure is similar to Python, but its scripting code is similar to C++ or Java.
Also, for the 'creation' tag blocks, in code, that uses 'XML', XML is similar to a browser language like HTML
------
hmm... maybe if you wouldn't mind posting your entire game code, there may be other things causing problems too, from what you said.
----
we just need to troubleshoot what you got, to figure out what's causing the problem...
-----
here's a recursion in action:
<asl version="550">
<include ref="English.aslx" />
<include ref="Core.aslx" />
<game name="test">
<gameid>(put in your own generated number)</gameid>
<version>1.0</version>
<attr name="count" type="int">0</attr>
<start type="script">
recursion
</start>
<game>
<object name="room">
<object name="player">
</object>
</object>
<function name="recursion">
msg ("HK")
game.count = game.count + 1
if (game.count < 5) {
recursion
}
</function>
</asl>
max17
29 Nov 2015, 03:17So is there a way to make my function work like i want to? some "wait function" that makes the program stop and wait for the keyboard input?
Anyway thanks for your help and for your answers
Anyway thanks for your help and for your answers

HegemonKhan
29 Nov 2015, 03:24here's some really bad recursion usage, lol (this was before I knew what recursion was, and why it's so bad to use):
viewtopic.php?f=18&t=4988
---------
there's also recent thread~post somwhere, where Pixie delves into a solution dealing with 'get input or show menu' and recursion...
unfortunately, I don't think the 'wait' Function~Script works... or if it does, it requires the same advanced~difficult coding needing that Pixie delves into.
I'll see if I can find it...
viewtopic.php?f=18&t=4988
---------
there's also recent thread~post somwhere, where Pixie delves into a solution dealing with 'get input or show menu' and recursion...
unfortunately, I don't think the 'wait' Function~Script works... or if it does, it requires the same advanced~difficult coding needing that Pixie delves into.
I'll see if I can find it...
max17
29 Nov 2015, 03:28Actually there's nothing in my code, a part from what i wrote here. The problem is just as I and you said, with a "getinput", i can't use a variable assignation. If i write
there's no way he will store that false in that var
<start>
var = tryme()
</start>
<function name="tryme" type="boolean">
get input {
if (result = "no") {
return false
}
}
</function>
there's no way he will store that false in that var
max17
29 Nov 2015, 03:30I found it viewtopic.php?f=10&t=5570&p=38388&hilit=wait#p38388 but unfortunately that's not what i need, cause "pixie" is not using variables to store info from a getinput. Anyway, nice graph you made 
For what I wrote before, i think i need a global variable... but where do i put Global variables? hahahah and how do i delete them after i don't need them anymore?

For what I wrote before, i think i need a global variable... but where do i put Global variables? hahahah and how do i delete them after i don't need them anymore?
HegemonKhan
29 Nov 2015, 03:51ya, it doesn't seem to be possible, unless Pixie found a way, but I can't find his~her post on it, sorry.
HegemonKhan
29 Nov 2015, 03:53in quest, global VARIABLES are members~Attributes (of an Object Element), and I'm not sure if Attributes can be directly deleted in quest... you can delete or remove an Object, which will indirectly delete or remove the global VARIABLES (Attributes). Actually... maybe you can via simply setting the Attribute to equal 'null', for example: orc.strength = null
see this thread for how quest's scripting works:
viewtopic.php?f=18&t=5559
and here's quest's documentation:
http://docs.textadventures.co.uk/quest/
see this thread for how quest's scripting works:
viewtopic.php?f=18&t=5559
and here's quest's documentation:
http://docs.textadventures.co.uk/quest/
max17
29 Nov 2015, 04:00Thanks! I think i'll try to work something and/or will ask pixie for "help", if he has time for me xD Btw is there any kind of chat ? Somewhere to talk in real time? Like an IRC chat or something?
HegemonKhan
29 Nov 2015, 04:03not really... but there's always skype or whatever other means that're out there for "real time" chatting.
max17
29 Nov 2015, 04:06Right! I just don't want to be too much of a nuisance xD Now it's time for me to go to bed, maybe tomorrow someone will answer this post, or maybe not xD Thanks again for everything!
HegemonKhan
29 Nov 2015, 09:30found the thread with Pixie's post in it:
viewtopic.php?f=10&t=5625&hilit=recursion
(which has a link to the much more detailed post~thread~library by Pixie, see the link below)
viewtopic.php?f=18&t=5492
(wow was that easy, I simply searched Pixie's posts*, using the search word "recursion", and it was the only post, laughs)
*click on user's name to go to their profile screen, then click on the ~ 'search user's posts', and type in your search word~phrase, hehe
-------------------------------
so, in general, try to avoid anything but the most simpliest design~implementation with using 'get input' and~or 'show menu', laughs.
viewtopic.php?f=10&t=5625&hilit=recursion
(which has a link to the much more detailed post~thread~library by Pixie, see the link below)
viewtopic.php?f=18&t=5492
(wow was that easy, I simply searched Pixie's posts*, using the search word "recursion", and it was the only post, laughs)
*click on user's name to go to their profile screen, then click on the ~ 'search user's posts', and type in your search word~phrase, hehe
-------------------------------
so, in general, try to avoid anything but the most simpliest design~implementation with using 'get input' and~or 'show menu', laughs.
The Pixie
29 Nov 2015, 10:08max17 wrote:Actually there's nothing in my code, a part from what i wrote here. The problem is just as I and you said, with a "getinput", i can't use a variable assignation. If i write
<start>
var = tryme()
</start>
<function name="tryme" type="boolean">
get input {
if (result = "no") {
return false
}
}
</function>
there's no way he will store that false in that var
The problem is that the code continues to run, even while waiting for the player to type. If your function is this, you will see what I mean.
<function name="tryme" type="boolean">
get input {
if (result = "no") {
msg("Reply given")
return false
}
}
msg("At end of function")
</function>
The player will see something like this:
Are you happy?
At end of function
YES
Reply given
The bit inside the get input block waits, but not the rest. So the function ends, but no return value has been provided, and then when the player does type in a reply, you have a return for a function that has already terminated.
Take a look at the posts HK found in his last post, and see if all that makes sense.

jaynabonne
29 Nov 2015, 10:23As The Pixie indicated, generally, you can't implement blocking input in Quest. That is, you can't have a script that waits until input is received before proceeding. Quest follows more of the JavaScript model where scripts are invoked and need to return in a timely fashion in order for the system to continue functioning (if you ever had a case with an infinite loop, where the script never returns, you'd find even the Quest desktop environment frozen).
The basic design behind Quest is to have callback scripts that are invoked when things happen (e.g. timers fire, input is received, etc). It requires a mental shift, but once you get your head around it, it will make sense (even if it's a bit of a pain at times when you really want things to wait).
"get input" (more or less) puts the engine into a mode where the next line of input entered by the user will be passed to the script you hand it. "get input" doesn't so much *get* the input as "begin the process of getting input from the user". It sets the mode and then returns to you. Once the input has been entered, the script you pass it will be called. You are sort of setting up an event handler to capture the next line of input.
I hope that makes some sense.
The basic design behind Quest is to have callback scripts that are invoked when things happen (e.g. timers fire, input is received, etc). It requires a mental shift, but once you get your head around it, it will make sense (even if it's a bit of a pain at times when you really want things to wait).
"get input" (more or less) puts the engine into a mode where the next line of input entered by the user will be passed to the script you hand it. "get input" doesn't so much *get* the input as "begin the process of getting input from the user". It sets the mode and then returns to you. Once the input has been entered, the script you pass it will be called. You are sort of setting up an event handler to capture the next line of input.
I hope that makes some sense.

max17
29 Nov 2015, 15:34Thanks for all the answers. I'm used to program with C++ and Java, and even if I used some of the javascript language to make a little game, I did not get into this kind of problems hahaha. It makes "sense", even if I'd rather preferred to have a "get input" that would "stop" my whole code from running, but, oh well, i think i just need to think differently
In fact I did find what pixie said and wrote in that post very usefull, so i was able to find a workaround for my "function"
I just used a "room" as a way to "stop" my function xD For what i needed now, it works, anyway i'm not quite sure that i could use the same method the next time i'll have a problem like this (hope i won't have more, but that's unlikely). Maybe i'll gain more experience and i'll find a better work around. Anyway, thanks again everyone for the help !
PS i think i'll use a lot of your libraries, Pixie
and hegemonkhan's combat library too XD


PS i think i'll use a lot of your libraries, Pixie

HegemonKhan
29 Nov 2015, 18:39just note that, that combat code of mine is really old and very poorly done (it was when I was just learning to program for my very first time), so just take this into account as you look at it and~or use it (or pieces of it), and also, I used all of those annoying abrevs too (and have some mistakes with using the right abrevs too in my game code), I just am too lazy to fix it up at this point, as it's such old and poor code, but it can be of help to people for ideas, at least.
And it's not really my combat code, as I used Pertex' combat code design's structure, I crafted the design structure that I did, as that's what I could do~understand from Pertex' Combat code, as Pertex' combat code uses some fancy coding to do equipment and etc other aspects of combat, which I wasn't able to understand at that time, and may still not understand now, lol.
------------
P.S.
here's Pertex' Combat code if you want to look at the actual source that I used to craft my combat code from:
viewtopic.php?f=18&t=2660
And it's not really my combat code, as I used Pertex' combat code design's structure, I crafted the design structure that I did, as that's what I could do~understand from Pertex' Combat code, as Pertex' combat code uses some fancy coding to do equipment and etc other aspects of combat, which I wasn't able to understand at that time, and may still not understand now, lol.
------------
P.S.
here's Pertex' Combat code if you want to look at the actual source that I used to craft my combat code from:
viewtopic.php?f=18&t=2660
max17
29 Nov 2015, 21:09Hahahah sure, i know what you mean
I'll take a look at everything 

