HELP: Unknown object or variable 'this'

Im having similar issue as previous where object is not getting recognized by 'this'.

Why for the life of me is this function makePurchase script working below but the tryStealing function with the exact same request not: AddToInventory (this) - they both belong to the food object.

This is the error im getting:

Error running script: Error compiling expression 'this': Unknown object or variable 'this'

Working Function:

Function Name: makePurchase
Return Type: None
Parameters: This
Script:
if (Got(this)) {
msg ("You have puchased it already.")
}
else if (game.pov.money >= this.price) {
DecreaseMoney (this.price)
msg ("You pay for " + GetDisplayName(this) + " and pick " + this.article + " up.")
AddToInventory (this)
this.purchased = true
}
else {
msg ("You don't have enough money.")
}

Not working function:
Function Name: tryStealing
Return Type: None
Parameters: This
Script:

Ask ("Are you sure you want to steal this item? You have a 50/50 chance of getting caught. If you are caught you will be thrown out and barred from the shop.") {
if (result = true) {
if (RandomChance(10)) {
SetObjectFlagOn (this, "barred")
MoveObject (this.player, main street)
msg ("")
msg ("You have been caught stealing and are barred from entering the store.")
}
else {
msg ("")
msg ("You have got away with stealing.")
AddToInventory (this)
}
}
}
Please note that all the other "This" works just the AddToInventory (this) in the tryStealing does not.

Thanks in advance.


As written, neither of those functions will work.

Both have a parameter This, and then attempt to use the variable this in the function body.
This and this are different variables.

I think that the parameter is actually this but you capitalised it when posting here?


Your actual problem is that the Ask function does two things: It sends a question to the screen, and makes a note of a script to run when the player has chosen.

The script inside the Ask block is run next turn, after the function has finished. This means that the function's parameters and variables no longer exist.

You probably want something like:

game.trying_to_steal = this
Ask ("Are you sure you want to steal this item? You have a 50/50 chance of getting caught. If you are caught you will be thrown out and barred from the shop.") {
  this = game.trying_to_steal
  game.trying_to_steal = null
  if (result) {
    if (RandomChance(10)) {
      SetObjectFlagOn (this, "barred")
      MoveObject (this.player, main street)
      msg ("")
      msg ("You have been caught stealing and are barred from entering the store.")
    }
    else {
      msg ("")
      msg ("You have got away with stealing.")
      AddToInventory (this)
    }
  }
}

Putting the variable into an attribute (or the game itself, or of the player) allows it to persist from one turn to the next.

Although I am a little confused by the function. AddToInventory (this) implies that this means the item you're trying to steal, which makes sense.

But then you have SetObjectFlagOn (this, "barred") - the text says the player will be barred from the shop if they're caught, but you appear to be barring them from that item. I would expect this.parent or this.shop in that line.

And then you have MoveObject (this.player, main street) - I assume you're ejecting the player from the shop; but in that case, why does the player belong to the object they tried to steal? I think this.player would normally be player or game.pov here, unless you're doing something very strange.


If you're posting script of the forums, you should put a line of backticks before and after it, like this:

```
code goes here
```

Then it's easier for other people to read, because it doesn't strip spaces from the beginning of the line.


Also, naming a parameter this is considered bad practice.


Ok thank you for all that, its a lot to take it but here goes.

"I think that the parameter is actually this but you capitalised it when posting here?" - Yes, that was an error. This function was copied from somewhere else and taking out the "this" parameter made it fail so i just left it there.

"If you're posting script of the forums, you should put a line of backticks before and after it, like this:" - ok thanks for that.

As for the coding, yes im still learning but thanks i will definitely go through it carefully and try get it to work now.

Thanks so much for your full and speedy response. I did not expect all that.


Yes, it works. You are a genius. Thanks again mrangel.


Log in to post a reply.

Support

Forums