Help Wanted Please - Its all about 'Flags'

In most programs you can create a flag by giving it a name and give it a default state of false
eg HasWater = True
Than you can use the flag in your program by checking it's state, setting it to True or Setting it False.

I can't see anywhere in Quest where I can create a flag and set it's default value to false.
Somewhere I read that you can attach a flag to an object in Quest. Also maybe an object becomes a flag in Quest.

Can someonw please help me with this.


A flag is just a name for a boolean attribute.
In Quest, all attributes belong to an object. This could be some object relevant to what the flag is for, the game, or the player.

So, for example, if you have a flag that's set when the player gets drunk, it would make sense to have it as an attribute of the player. You could set it using one of the following lines (all of which do the same thing):

  • player.drunk = true
  • set (player, "drunk", true)
  • SetObjectFlagOn (player, "drunk")

If you're setting a flag to indicate that a flashlight is turned on, you would probably do flashlight.switchedon = true (and this is exactly what the default "turn on" and "turn off" commands do if you enable them for an object).

If you're setting a flag to indicate that the world has been flooded, you'd probably want to make it an attribute of the special object game.

(In Gamebook mode, the built-in function SetFlagOn ("some name") sets an attribute of game, because gamebook mode doesn't have flags as such)

There are two main ways to check the status of a flag:

  • if (player.drunk = true) { - this will run a command if the flag is true, and not if it's false. I see this a lot in code posted on the forum, and it kind of bugs me because the if statement already compares its contents to true. This will cause an error if the flag is neither true or false (for example if it hasn't been set at all)
  • if (player.drunk) { - easier to write. I think this will also give an error if the attribute hasn't been set yet.
  • if (GetBoolean (player, "drunk")) { - this will run code if player.drunk is true; but not if it's false or unset. GetBoolean treats an unset attribute (or a string, or a number) as being false, so you don't need to worry about setting it to false initially.
  • if (not GetBoolean (player, "drunk")) { - runs code if the attribute is false, unset, or some non-boolean value
  • if (HasBoolean (player, "drunk")) { - Used to test if the attribute has been set yet, regardless of whether it's true or false.

I would recommend using objectname.attributename = true to set a flag (because it's quicker), and GetBoolean (objectname, "flagname") to test them, because it lets you assume that all flags are false initially.


And a side note: I used player.drunk in the examples above. That's generally a bad habit, because it will stop working if you rename the player object or allow the player to choose which character to control. You never know who's going to look at your code and try to reuse it, so it's a good habit to use game.pov instead of player, especially when sharing code on the forums. game.pov is a built in attribute which refers to the current player object regardless of what its name is.


Thanks mrangel for your help with flags.

so a command such as if (car.FlatTyre = true)
then the game needs to know that car exists but is FlatTyre is created when the above command is used?

In Gamebook mode would I use SetFlagOn ("EngineOn") or would I use SetFlagOn("red button") ?

In your text you said
if (player.drunk) { - easier to write. I think this will also give an error if the attribute hasn't been set yet.
Do you mean I need to add 'drunk' to the player 'attribute'?


so a command such as if (car.FlatTyre = true)
then the game needs to know that car exists but is FlatTyre is created when the above command is used?

No. The car needs to exist, and FlatTyre must be set to true or false. If it hasn't been set yet, that line will generate an error.

That's why I suggest using if (GetBoolean (car, "FlatTyre")) { - which treats "not set" the same as false.

In Gamebook mode would I use SetFlagOn ("EngineOn") or would I use SetFlagOn("red button") ?

In gamebook mode, SetFlagOn ("EngineOn") is a shortcut for game.EngineOn = true. The flag is related to the game, not to any specific object, so you can name it whatever helps you to remember what it means.

Do you mean I need to add 'drunk' to the player 'attribute'?

If you're using if (player.drunk) { you will need to add a drunk attribute to the player, and set it to false initially.
It's easier just to use if (GetBoolean (player, "drunk")) { in most cases.


Thanks mrangel.
I now have a better understanding on flag.

The next thing I want to study is timers. It should be possible to display a message followed by a 2 second delay then display another message.


Data/VARIABLE/Attribute/Variable/Value Types:


Amount (able to do arithmetic upon) Data Types:

  1. Integers (ints): ..., -999, -1, 0, 1, 999, ....

attribute example: player.strength = 100

  1. Doubles: ..., -999.789173682734, -1.123, 0.0, 1.43834923464, 999.123, ....

attribute example: player.damage = 54.9


Non-Amount (NOT able to arithmetic upon) Data Types:

  1. Booleans ("flags"): true/false

attribute examples:

orc.dead = false
// or
orc.dead = true

player.poisoned = false
// or
player.poisoned = true

tv.switchedon = false
// or
tv.switchedon = true

room.visited = false
// or
room.visited = true

  1. Strings ("text"): "a", "abc", "1", "123", "abc_123", "hi, my name is HK, what is your name?", etc etc etc

attribute example: player.alias = "HK"

  1. Object References/Pointers: an Object's name is a reference/pointer to its memory address (location)

attribute example:

// create ("katana") // creating a 'katana' Object
player.weapon = katana

  1. Scripts: does an action (or multiple actions)

attribute example:

game.start => {
  msg ("welcome to my game, I hope you enjoy it")
}
  1. Lists/Arrays

  2. Dictionaries


Hi hegemonkhan.

I'm not certain what your text refers to as it does not refer to flags.


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

Support

Forums