For Quest: Can A Template Return A Function?

0 votes
90 views
asked Apr 22 in Authoring by nimrod (1 point)
edited Apr 23 by Alex

I want to change the default responses of the engine, for example the "NotCarrying"-dynamic template. What do I have to enter in "Text:" so that it accesses the function "es"?

2 Answers

0 votes
answered Apr 22 by The Pixie (121 points)

DynamicTemplates are kind of like functions that take a single parameter, object, and return a string. The returned string depends on what you put in the "Text" field.

This is the default for NotCarrying:

"You are not carrying " + object.article + "."

Quest will evaluate that and return it. Specifically it will join together the first string, the value of the article attribute of object (which will be "it", "them", etc.) and the last string (so the player might see "You are not carrying it."). You can call other functions, for example:

"You are not carrying " + GetDisplayName(object) + "."

The GetDisplayName function will get the name or alias of the object as well as its prefix, so now the player might see "You are not carrying a ball."

You could do both. You can access other attributes on other objects too:

player.name + " is not carrying " + object.article + ". And by " + object.article + " I mean " + GetDisplayName(object) + "."

The player might see "Mary is not carrying them. And by them I mean some rocks." You can use functions and attributes you have added yourself too. It is very flexible.

commented Apr 22 by nimrod (1 point)
But that doesn't seem to work with a function I created.
the function is called: es
it is a switch script that randomly selects texts
does that change anything? what exactly would I have to type in?
0 votes
answered Apr 22 by jaynabonne (141 points)

This works for me:

  <function name="es" type="string">
    return("This is the result of es")
  </function>

with dynamic template having text:

es()

Be sure your function is of type "string" and actually returns a string.

commented Apr 22 by nimrod (1 point)
So, I set the function to type "string" and I entered "es()" in text, and it does display random text now, but it also says "Function does not return value".
I never used a return-script before, how does it exactly work?
commented Apr 22 by jaynabonne (141 points)
If your function has a return type, then you must return some sort of value. Since you say it's printing the random text, I'm guessing you have used "msg"s (or print messages) in your random function. You don't want to do that. You want to assign the random string to a variable and then return the value (or directly return the value) so the dynamic template can print it *for* you.

Something like:

<function name="es" type="string">
    switch (GetRandomInt(1, 3)) {
        case (1) {
            return("This is random string #1")
        }
        case (2) {
            return("This is random string #2")
        }
        case (3) {
            return("This is random string #3")
        }
    }
</function>

(Note: the above code is untested, so it might have a typo I don't see. But it has the general idea anyway.)
commented Apr 23 by nimrod (1 point)
Alright, it works with normal text, but I guess there is a problem with the random text that I need.
All the text is written in this way: ["Error: blah blah blah."]
and I guess one of the symbols messes something up. Can I use the brackets and the quotes or do I have to get rid of those?
commented Apr 23 by jaynabonne (141 points)
The brackets signify a template replacement (where you then look up the template named within the brackets). I may be wrong, but I don't think you do template replacement within a dynamic template return string. Also, with templates, you usually put the name of the template in the square brackets, not a string. I suspect you just need to get rid of the brackets and return strings directly. (It might be helpful, if this continues, to actually post the code you have so far. I'm making some guesses here about what you have actually done.)
commented Apr 23 by nimrod (1 point)
<function name="es1" type="string">
    switch (GetRandomInt(1,13)) {
      case (1) {
        return ("["Error: The action, you just wanted to perform, is not within stipulated parameters. Your mother has been notified."]")
      }
      case (3) {
        return ("["Error: The action, you just wanted to perform, is not within stipulated parameters. Your mother has been notified. She sounded disappointed."]")
      }
      case (4) {
        return ("["Error: The action, you just wanted to perform, is not within stipulated parameters. Your mother has been notified. In case of your mother being dead, remind yourself that you'd might have a mother if you wouldn't violate stipulations."]")
      }
      case (5) {
        return ("["Error: The action, you just wanted to perform, is not within stipulated parameters. If you cannot respect stipulations, why don't you go *in order to keep our family-friendly image, but still utilize this line as efficiently as possible, we removed this word* yourself and come back and try when you got your head out of your *in order to keep our family-friendly image, but still utilize this line as efficiently as possible, we removed this word*?"]")
      }
      case (6) {
        return ("["Error: The action, you just wanted to perform, is not within stipulated parameters. Your mother has been notified. In case of your mother being dead, your father has been notified. In case of your father being dead as well, well, go nuts, we won't blame you."]")
      }
      case (7) {
        return ("["Error: The action, you just wanted to perform, is not within stipulated parameters. Your mother has been notified. In case of your mother being dead, your father has been notified."]")
      }
      case (8) {
        return ("["Error: The action, you just wanted to perform, is not within stipulated parameters. If you cannot respect stipulations, why don't you go *in order to keep our family-friendly image, but, ah, what the fuck, how the fuck are we family-friendly?!* yourself and come back and try when you got your head out of your *FUCKING ASS FUCKING ASS FUCKING ASS*?"]")
      }
      case (9) {
        return ("["Error: The action, you just wanted to perform, is not within stipulated parameters. If you cannot respect stipulations, why don't you go *fuck* yourself and come back and try when you got your head out of your *ass*?"]")
      }
      case (10) {
        return ("["Error: The action, you just wanted to perform, is not within stipulated parameters. Please refrain from violating stipulations and we'll refrain from pointing out your several flaws."]")
      }
      case (11) {
        return ("["Error: The action, you just wanted to perform, is not within stipulated parameters. Please refrain from violating stipulations and we'll refrain from pointing out how valuable testing merchandise gets caught in your gravitational field and orbits around you."]")
      }
      case (12) {
        return ("["Error: The action, you just wanted to perform, is not within stipulated parameters. Please refrain from violating stipulations and we'll refrain from pointing out your ugly face and how people use it to misrepresent values. For example *This is what communism does to your face.* and etc."]")
      }
      case (13) {
        return ("["Error: The action, you just wanted to perform, is not within stipulated parameters. Please refrain from violating stipulations and we'll refrain from pointing out how you are alone in this world and how no warmth and connection can change the fact that you're a single entity, separated from all the other single entities, and how in the end, you'll simply die in the way you were born and in the way you lived. Alone."]")
      }
      case (2) {
        return ("["Error: The action, you just wanted to perform, is not within stipulated parameters. Please refrain from violating stipulations and we'll refrain from pointing out that/... Please refrain from violating stipulations."]")
      }
    }
  </function>
commented Apr 23 by jaynabonne (141 points)
Ah yes, that helps. So change something like this:

return ("["Error: The action, you just wanted to perform, is not within stipulated parameters. Your mother has been notified."]")

to this:

return ("Error: The action, you just wanted to perform, is not within stipulated parameters. Your mother has been notified.")

The way you have it at the moment, you have a starting string "[" and ending string "]" with text in between outside the quotes, which is going to really confuse things.
commented Apr 23 by nimrod (1 point)
Ah, yes, thank you, it works. But why doesn't it work for regular templates as well?
commented Apr 23 by jaynabonne (141 points)
To be honest, I'm not sure what you're question is. You can't have functions with regular templates. And what you were doing with the brackets wasn't a regular template anyway, as a template is something named (e.g. "MyTemplate) with a replacement string, and you would then use "[MyTemplate]" to replace it with the chosen string. You had the brackets but with the string inside, which isn't how templates work. And even if you had the syntax right, I don't know if dynamic templates have been coded to handle the case where a dynamic template returns templates itself inside the string, which must then be parsed again. It's not too difficult to do, but it might not have been considered necessary when dynamic templates were being coded. Or maybe it works. I don't know. I never tried it. :)
commented Apr 23 by nimrod (1 point)
Oh, I meant, if I enter "es()" in a template like "UnrecognisedCommand" it just prints "es()". Why won't it use the function like a dynamic template does?
commented Apr 23 by jaynabonne (141 points)
That is the difference between a normal (textual) template and a dynamic (function-based) one. :) The former is just text.
commented Apr 23 by nimrod (1 point)
Oh, so there is no way to have randomized text with a normal template?
commented Apr 23 by jaynabonne (141 points)
Nope. A normal template is just fixed text. That's why dynamic templates were created. Unfortunately, you can only implement a dynamic template where they're actually used by the core code, so you can't replace everything as you might wish.
commented Apr 23 by nimrod (1 point)
Ah, I see, okay, well, thanks for the help. Really good to know, I clearly had no idea of anything, thanks.
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.
...