[SOLVED!] IF statements in PRINT

Hello all! I'm new to Quest, but not to light coding, so I'm a little slowed down by all the dropdowns. I did find the "edit in code" button, happily, but from the way it looks I'm wondering how to implement something.

What I want to do is hide a message in a secret code and make the player look around for each letter substitution. For now, they're all just substituted with asterisks. Say that the player finds the code for the letter "E". I set a flag to the player object that they've learned that letter. I would then like the secret code to reflect these changes.

How I'd normally do this would be to run a print command of some sort, and then manually encode each letter/asterisk combo with an if statement. EG pre-front it with msg: (" and then per letter

if (player.learned-E==True):
     ("e")
else:
     "*"

And sure, that would be a lot of work, but in my mind a good lot easier than selecting an if, then a print, then an else from drop-downs. In fact, I'm not even sure they would print all those messages in a single line, but on separate lines?

Does anyone have a suggestion on how to do this? :> In baby language, please, as I am in fact a Quest baby.


Io

The way you do it (In code-view) is like this:

msg("{if player.learned-E=True:E}{if player.learned-E<>True:*}")

From my experience, if-statements actually IN a print statement can only be very simplistic, like above.


In that case, you want to use {either, which is a text-processor directive like {if but it allows an else clause.

I usually recommend using {either anyway, because it doesn't have all the restrictions that {if does.

So you could do:

msg ("{either player.learned-E:e:*}")

But if you've got a lot of text like this, it would probably be faster to create your own text processor directive.

To create new text processor directives, you need to add an element to a script dictionary. Rather than messing about with core attributes, it's probably easier to put the following in your start script (or anywhere before the first time it is used):

secretcode_directive => {
  game.textprocessorcommandresult = ""
  for (i, LengthOf("secretcode")+2, LengthOf(section)) {
    char = Mid (section, i, 1)
    if (IsRegexMatch("\\w", char) and not GetBoolean (game.pov, "learned-"+UCase(char))) {
      char = "*"
    }
    game.textprocessorcommandresult = game.textprocessorcommandresult + char
  }
}
game.textprocessorcommands = game.textprocessorcommands
dictionary add (game.textprocessorcommands, "secretcode", secretcode_directive)

Then you can do something like:

msg ("The coded message says: {secretcode:I bet you can't read this!}")

That will initially generate the output:

The coded message says: * *** *** ***'* **** ****!

but once you set player.learned-I and player.learned-T to true, it will display

The coded message says: I **t *** ***'t **** t*i*!

Hey! Thank you both for your quick replies: all the solutions worked.
Because I do intend to drop quite a few of these missives, and it would be nice if they could be longer, I went ahead and used mrangel's tremendous init script. (With one modification; had to change regular hyphen to an underscore, or the variable names wouldn't work, but that's my mistake: I didn't know hyphens were a no-go.) It also made me look up where the start script is, so bonus learning.

Thank you so much!


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

Support

Forums