Moving an object to a new location based on attribute

Hi there.

I have an object called 'plastic barrels' which I want to move to different locations as they are pushed. They start at location 1, and end up in location 4.

First thing is, the code doesn't work. I get an error when checking and changing the attribute 'location', which I've specifically set on that object. I tried originally using a string for the attribute which matched the location names, but that didn't work, so I changed it to a simple integer going from 1-4, and that also doesn't work. What obvious thing am I missing?

Second thing is, is there an easier way of doing this? The reason I did it like the below is because I need the action of pushing and the associated message to change at each of the four locations.

Thanks for your help guys as ever!

SO looking forward to a full GUI for Quest 6, btw! <3

Here's the script as it appears (although I use the GUI editor most of the time):

if (plastic barrels.location  = 1) {
  msg ("They topple into the water, proving themselves to be very buoyant. The barrels begin to float further down stream!")
  set (plastic barrels, location, 2)
  MoveObject (plastic barrels, River Edge)
}
else if (plastic barrels.location = 2) {
  msg ("You give the barrels a second push, and once more they begin to bob gently along, heading further down the river.")
  set (plastic barrels, location, 3)
  MoveObject (plastic barrels, Down Stream)
}
else if (plastic barrels.location = 3) {
  msg ("A third time you push the barrels along. This time, the current takes them! The barrels bounce over the rocks in the river, careening between the banks on either side, rapidly disappearing from view. Where they'll end up is anyone's guess.")
  set (plastic barrels, location, 4)
  MoveObject (plastic barrels, Lake Edge)
}
else if (plastic barrels.location = 4) {
  msg ("You've had enough of pushing them around.")
}
else {
  msg ("You've had enough of pushing them around.")
}```

Still struggling with this. Don't know what I'm doing wrong 😑

The error message I get is Error running script: Error compiling expression 'location': Unknown object or variable 'location'


OK, the line set (plastic barrels, location, 4) takes 3 parameters

  1. The object you want to act on, plastic barrels. This is an expression that returns an object. So it can either be a function that returns an object, an attribute of type "object", a variable containing an object, or a literal object name (like plastic barrels)

  2. The name of the attribute to set. This needs to be a string. So it can either be an expression that returns a string, a variable or attribute whose type is string, or a literal string (like "location")

  3. The value to set it to. This can be a variable, an expression, or a literal value of any type. In this case it's a literal integer, 4.

The problem here is the second parameter.

  set (plastic barrels, location, 4)

Quest reads this as "Find the object or variable named 'plastic barrels'. Find the variable named 'location', and use the string in that variable as the name of an attribute that we want to set. And set that attribute to 4."

You don't have a variable named location. You want it to be a string literal. Quest uses quote marks to tell if something is a variable or a string literal.
So what you want may be:

  set (plastic barrels, "location", 4)

However, if you're always setting the same attribute, there is no reason to use set. Its only purpose is if you don't know when you write the code what the name of the attribute will be (for example, if a character has 6 attributes representing statistics, and the player picks one to increase)

If you know the name of the attribute, it would be quicker to write:

  plastic barrels.location = 4

Amazingly in depth response! Thank you so much. What you said makes perfect sense, and I feel like an idiot 😄

I'm still trying to get my head around some things.


Just to help anyone else who's more familiar with the GUI than editing code directly, this would be done by pressing +, then choose set variable. plastic barrels.location goes in the first box, choose expression, then type 4 in the last box.


Just to help anyone else who's more familiar with the GUI than editing code directly, this would be done by pressing +, then choose set variable. plastic barrels.location goes in the first box, choose expression, then type 4 in the last box.

For the GUI users, there are a lot of places where you have to choose between "Expression" or "String" at the type of some value.

It probably helps to understand the answer if you understand what the difference between those two. When you choose 'String', if you look in code view you will see that it's put quotes around what you put in the box.

So if you choose type "expression" and enter 4, it will be the number 4. If you choose string, it will be "4" in code view, meaning the character 4.

When Quest looks at an expression, it goes through quite a lot of steps to determine what to do with the sequence of characters in the code.

  • If it consists of digits, treat it as an integer (number)
  • If it has digits and a decimal point, it's a float (fraction)
  • If it has quotation marks around it, it's a string
  • if it has operators like +, -, or, = and similar in, then look at the bits on either side first, and then use the operator to combine them
  • If it is true, false, or null, those are special values
  • If it has a dot in, look at the thing to the left first, and see if that's an object. The part to the right is assumed to be the name of that object's attribute.
  • If it's got brackets, it must be a function. Evaluate the bits in brackets first, and then call the function to get a value for the whole expression
  • If it's the name of a variable within this script, use the contents of that variable
  • If it's the name of an object (including objectlike elements such as turnscripts, commands, exits, and game) use that object
  • If it isn't any of the above, give an error message

So, for example, the expression "My sword!" is the same as the string My sword!. But as you discovered, the expression location will be an error unless there is a variable or object named location.


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

Support

Forums