Where am I going wrong with my simple code?
Brian5757
05 Jan 2020, 06:08Can you please help me to get started.
I'm trying to created a simple game to gain some experience with programming with Quest.
I'm in an Engine room with a red button. When I press the red button then it turns on the engine. There is a check when I press the button using an EngineOn flag, if set then the engine has already been turned on and I get a message telling me this.
When I type "Press button" I get the following error:
Error running script: Error compiling expression 'object.article <> button': Unknown object or variable 'button'
This is my code:
mrangel
05 Jan 2020, 09:41Is button
the name of an object?
Remember that in code you have to use the name of an object, not its alias. And it's case-sensitive, so button
and Button
are different objects.
Also, I notice that you're checking if object.article
is a reference to button
. This is likely to cause problems, as article
is supposed to be a string containing "him", "her", "it", "you", or "me", and is used by lots of the built-in messages. Setting article
to an object would break quite badly.
If this is a commmand checking if the object it's been used on is the button, you would want (object <> button)
.
If you're setting an object's article to the string "button" and then need to check it for some reason, your expression would be (object.article <> "button")
. This wouldn't cause errors, but may cause Quest's grammar to be a bit odd.
This is my code:
I don't see the code, so I guessed what it might be.
Brian5757
05 Jan 2020, 10:11Hi mrangel.
Thankld for your reply. I hope you can see my code this time.
This is my code:
Brian5757
05 Jan 2020, 10:19Hi mrangel.
I had to leave the < sign at the start of my code so that my code would be accepted by this Forum.
This is my code:
asl version="580">
include ref="English.aslx" />
include ref="Core.aslx" />
game name="Test 3-1-20">
gameid>49bfc0b4-5f46-4eb6-b609-9c25d1972390
version>1.0
firstpublished>2020
/game>
object name="Engine room">
inherit name="editor_room" />
isroom />
object name="player">
inherit name="editor_object" />
inherit name="editor_player" />
/object>
object name="red button">
inherit name="editor_object" />
look>Below the button is written "Engine ON"
alt type="stringlist">
value>Red
/alt>
displayverbs type="stringlist">
value>Look at
value>Take
value>Press
/displayverbs>
/object>
/object>
verb>
pattern>press #object#
/verb>
command name="PressButton">
pattern>press #object#
script> button) {
msg ("That's not a button")
}
else if (GetBoolean(true, "EngineOn")) {
msg ("The Engine is already on")
}
else {
msg ("You switch the engine on")
}
]]>
/command>
/asl>'''
mrangel
05 Jan 2020, 12:26The forum has still mangled your code a little, but I think I can see what you're trying to do.
You should be able to look at a single script in code view; so in this case you only needed to include the script for the PressButton command.
I think what you want is something like this:
if (object <> red button) {
msg ("That's not a button")
}
else if (GetBoolean (object, "EngineOn")) {
msg ("The Engine is already on")
}
else {
msg ("You switch the engine on")
object.EngineOn = true
}
This might also be a good place to use a verb instead of a regular command.
Verbs are a special type of commands which do different things depending on what object they are used on. So if you created a press verb instead of a press command, the button would have a script which runs when it is pressed, and pressing any other object would automatically generate a message like "You can't press that".
The advantage of verbs is that if you add another button later, you can give it its own press verb without needing to modify the command.
About posting scripts:
For mentioning a single line of code or an error message, you can surround it with backticks ```Like this```
.
For pasting multiple lines of code, put a line of backticks above it and another below it.
``` like this code goes here ```
It's common to use one backtick for the inline method and three for a block of code, but that isn't really necessary. You can use any number as long as it's the same number at the beginning and end. If your code actually contains backticks (which some error messages do), the forum will get confused if the number of backticks before/after the code is less than the number in the code. So if you want to paste some code that has a row of three backticks in it somewhere, you'd want to put four above and below it.
If you can't find the backtick key on your keyboard (it's below 'Esc' in most countries) you can copy and paste three of them from the instructions to the right of the reply box.
I know it says you can indent code with 4 spaces, but that doesn't work unless it's in a quote
To quote the message your're replying to, put a >
at the start of the line you're quoting.
If you're quoting someone else's code, you can put five spaces after the >
on each line.
This is quoted text with a
>
at the beginning
This is quoted text with a>
and a space at the beginning
This is quoted text with a>
and 2 spaces at the beginning
This is quoted text with a>
and 3 spaces at the beginning
This is quoted text with a>
and 4 spaces at the beginning
This is quoted text with a `>` and 5 spaces at the beginning This is quoted text with a `>` and 6 spaces at the beginning This is quoted text with a `>` and 7 spaces at the beginning
Brian5757
06 Jan 2020, 01:23Thanks mrangel.
By removing .article and putting red infront of the word button solved one of the probles.
It now has a problem with this code
else if (GetBoolean(true, "EngineOn")) {
msg ("The Engine is already on")
}
I now get the error:
Error running script: Error evaluating expression 'GetBoolean(true, "EngineOn")': GetBoolean function expected object parameter but was passed 'True'
Maybe I need to create a flag in the game called EngineOn and set it to false.
I'm not certain how you would do this as I don't see anywhere in the game for doing this.
mrangel
06 Jan 2020, 10:24Did you look at the example code in my previous reply?
A flag needs to be set for a particular object. In this case, the button seems a good choice.
So you'd have:
GetBoolean (object, "EngineOn")
You can't set a flag on true
because true
isn't an object.
You don't need to set it to false, because GetBoolean
treats unset values the same as false
.
Once the engine is turned on, you would set the flag using:
object.EngineOn = true
Brian5757
06 Jan 2020, 12:32Thanks for your help mrangel.
It worked this time with the code changes.