What is Wrong with this Script?

I made this script with this purpose of adding a stick clone with a random value (for quantity of sticks) into a room when the player enters it, but when I enter a room with a stick clone in it, I get an error message.

Here is my script:

if (RandomChance(90)) {
  game.sticks_clone = CloneObject (Sticks)
  game.sticks_clone.parent = player.parent
  while (RandomChance(50)) {
    game.sticks_clone.quantity = game.sticks_clone.quantity + 1
  }
  msg ("There are " + game.sticks_clone.quantity + " sticks here")

Here is the error:

Error running script: Error compiling expression 'game.sticks_clone.quantity + 1': ArithmeticElement: Operation 'Add' is not defined for types 'Object' and 'Int32'

I'm not familiar with 'while'? It might work, but I tend to just use 'else if' instead. so, else if (RandomChance(50)).

You need to add in a } between these two lines, like so;

game.sticks_clone.parent = player.parent
}
while (RandomChance(50)) {

, as currently your first if statement isn't being closed before it tries to begin a second one.

Past that, is sticks_clone.quantity a numerical variable? Sometimes Quest can throw errors if you're trying to add numbers to, say, a word string. Go into the Sticks object, create an attribute called quantity, and set it to Integer. when you clone an object, you clone it's attributes, too.

Try all this and see if it works.


You need to add in a } between these two lines,

No he doesn't. The indentation shows that there is another } at the end; presumably cut off when copying and pasting to the forum.

Past that, is sticks_clone.quantity a numerical variable?

The error message says it is an object; probably null (undefined).

The line game.sticks_clone.quantity = game.sticks_clone.quantity + 1 adds 1 to a numeric attribute. But you haven't set the initial value, so it's attempting to add 1 to an unknown value.

You probably want:

if (RandomChance(90)) {
  game.sticks_clone = CloneObject (Sticks)
  game.sticks_clone.parent = player.parent
  game.sticks_clone.quantity = 1
  while (RandomChance(50)) {
    game.sticks_clone.quantity = game.sticks_clone.quantity + 1
  }
  msg ("There are " + game.sticks_clone.quantity + " sticks here")
}

(Though I am curious why you use an attribute game.sticks_clone rather than a local variable sticks_clone. Is there somewhere later on that you want to refer to the last sticks_clone created?)


-- Bah, just ran back upstairs after realizing I was completely wrong, my version would've made two separate checks while the OP wanted the 'while' to potentially activate if the first random chance was met. Disregard me, mrangel's got you.

I was curious about the use of game.(object) too, but I've never used it myself, so I didn't want to fiddle with it.

game.sticks_clone.quantity = 1 will, if no attribute of that name already exists, create it on the fly instead. Either creating the attribute in the Sticks object beforehand or doing it this way should sort out the error message, as now the system has a number to add to a number, instead of a number to add to an undefined void.


Mrangel actually yes, I need to refer to that specific stick clone so that it will be unhidden and given a random value again after x amount of turns so that it appears the sticks have eventually respawned there.


Alright, I fixed the error message, but now for some reason I get two seperate stick clones with two different values, as such:

There are 6 sticks here
There are 17 sticks here

Furthermore, the stone values won't show up at all, even though the stones function has the exact same script as the stick one. It will only display "stones" in the list of objects visible to the player, as such:

You can see: Sticks, Stones, a Boar, Trees and a Thorn Bush.

I notice that in your "You can see:" line, it doesn't start with "Sticks, Sticks, Stones" … is this the case even in rooms where it displays the sticks twice?

If so, my first guess would be that your stones function with the exact same script might look like:

if (RandomChance(90)) {
  game.stones_clone = CloneObject (Stones)
  game.stones_clone.parent = player.parent
  game.stones_clone.quantity = 1
  while (RandomChance(50)) {
    game.stones_clone.quantity = game.stones_clone.quantity + 1
  }
  msg ("There are " + game.stones_clone.quantity + " sticks here")
}

As this simple typo would cause both of the issues you describe.


Wow, you were right. That exact typo was there, and it fixed both issues.


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

Support

Forums