Need Help With Potions

jmnevil54
15 Aug 2019, 21:32

(filler text)
I'm working on my pokemom game. I can't get the potions or the money/pokedollars script to work.

This is my code.

player.current_room = player.parent
player.old_room = player.current_room
MoveObject (player, Battle Room)
if (HasInt(game, "crittercount")) {
  game.crittercount = game.crittercount + 1
}
else {
  game.crittercount = 1
}
create ("crittercount" + game.crittercount)
obj = GetObject("crittercount" + game.crittercount)
obj.parent = room
obj.old_parent = room
obj.dead = false
obj.parent = player.parent
obj.displayverbs = Split("Look at;Attack;Direct Hit;Stat;Status", ";")
obj.changedhitpoints => {
  if (this.hitpoints < 1) {
    this.dead = true
    msg ("It whited out!")
    player.exp = player.exp + 20
    player.pokedollar = player.pokedollar + this.old_parent.pokedollar
    RemoveObject (this)
    player.parent = player.old_room
    AddToInventory (Zephyr Badge)
    player.badge = player.badge + 1
  }
}
obj.hero = false
obj.alias = "Pidgeotto"
obj.listalias = CapFirst(obj.alias)
obj.look = ProcessText("A " + obj.alias + ". A flying type. A common sight in forests and woods. It's very docile. It flaps its wings at ground level to kick up blinding sand. If threatened, it rapidly flaps its wings in the grass, stirring up a dust cloud that drives insect prey out into the open.")
obj.hitpoints = 40
obj.max = 40
obj.damage = 9
obj.maxdamage = 9
obj.attack = 6
obj.maxattack = 6
obj.defence = 6
obj.maxdefence = 6
obj.armour = 10
obj.maxarmour = 10
obj.selectweapon => {
  this.weapon = GetObject(PickOneString("Tackle;Gust"))
}
obj.firearmdamage = 12
obj.firearmattack = 6
obj.ammo = 20
obj.ammomax = 20
obj.choosepotion => {
  if (this.hitpoints < this.max / 4) {
    if (RandomChance(25)) {
      if (this.old_parent.potion = 1) {
        this.old_parent.potion = obj.old_parent.potion - 1
        this.hitpoints = this.max
        msg (this.old_parent.potion + " used an Oran Berry on Hoothoot!<br/>Health was restored!")
      }
    }
  }
}

Thid code is supposed to make Falkner, the gym leader, the obj.old_parent.

obj.parent = room
obj.old_parent = room

Which is then supposed to be used in this code.

obj.choosepotion => {
  if (this.hitpoints < this.max / 4) {
    if (RandomChance(25)) {
      if (this.old_parent.potion = 1) {
        this.old_parent.potion = obj.old_parent.potion - 1
        this.hitpoints = this.max
        msg (this.old_parent.potion + " used an Oran Berry on Hoothoot!<br/>Health was restored!")
      }
    }
  }
}

But none of it is working! Please help!

Coincidentally, this code SHOULD work. I haven't had the luck for it to use a potion, bit it got the money right. The parent is the Trainer.

player.current_room = player.parent
player.old_room = player.current_room
MoveObject (player, Battle Room)
if (HasInt(game, "crittercount")) {
  game.crittercount = game.crittercount + 1
}
else {
  game.crittercount = 1
}
create ("crittercount" + game.crittercount)
obj = GetObject("crittercount" + game.crittercount)
obj.dead = false
obj.parent = room
obj.parent = obj.old_parent
obj.parent = player.parent
obj.changedhitpoints => {
  if (this.hitpoints < 1) {
    this.dead = true
    msg ("It whited out!")
    player.exp = player.exp + 20
    player.pokedollar = player.pokedollar + Trainer.pokedollar
    RemoveObject (this)
    player.parent = player.old_room
  }
}
obj.displayverbs = Split("Look at;Attack;Direct Hit;Stat;Status", ";")
obj.hero = false
obj.alias = "Hoothoot"
obj.listalias = CapFirst(obj.alias)
obj.look = ProcessText("A " + obj.alias + ". A flying type. It has a perfect sense of time. Whatever happens, it keeps rhythm by precisely tilting its head in time. It begins to hoot at the same time every day. Some trainers use them in place of clocks.")
obj.hitpoints = 25
obj.max = 25
obj.damage = 2
obj.maxdamage = 2
obj.attack = 2
obj.maxattack = 2
obj.defence = 3
obj.maxdefence = 3
obj.armour = 0
obj.maxarmour = 0
obj.selectweapon => {
  this.weapon = GetObject(PickOneString("Struggle;Tackle;Peck"))
}
obj.firearmdamage = 5
obj.firearmattack = 3
obj.ammo = 20
obj.ammomax = 20
obj.choosepotion => {
  if (this.hitpoints < this.max / 8) {
    if (RandomChance(25)) {
      if (Trainer.potion = 1) {
        Trainer.potion = Trainer.potion - 1
        this.hitpoints = this.max
        msg ("Harrison used an Oran Berry on Hoothoot!<br/>Health was restored!")
      }
    }
  }
}

jmnevil54
16 Aug 2019, 12:47

Anything?


mrangel
16 Aug 2019, 16:48

So the variable room is the trainer? Why is it called "room"?

It would be a lot easier to debug your code if you named variables for what they actually are. In this code, you're using the variable "room", but we can't see what that is set to.


jmnevil54
16 Aug 2019, 16:57

So Falkner (the trainer) is what I want to be the room. That way, Falkner is the obj.old_parent.

Room = room

I already tried object. I'm just testing everything I can think of right now.

I am simply trying to find a way to store the trainer's name in an attribute. You know how Pokemon are stpred and carried around in pokeballs? The trainer carries the pokeballs. The trainer also carries items, like potions.


jmnevil54
16 Aug 2019, 17:29

I revised the code a bit, but not much. It still isn't working. I keep getting the "cannot use + add arithmetic with object" error. It thinks obj.old_parent.pokedollar is an object.

player.current_room = player.parent
player.old_room = player.current_room
MoveObject (player, Battle Room)
if (HasInt(game, "crittercount")) {
  game.crittercount = game.crittercount + 1
}
else {
  game.crittercount = 1
}
create ("crittercount" + game.crittercount)
obj = GetObject("crittercount" + game.crittercount)
obj.parent = room
obj.old_parent = obj.parent
obj.dead = false
obj.parent = player.parent
obj.displayverbs = Split("Look at;Attack;Direct Hit;Stat;Status", ";")
obj.changedhitpoints => {
  if (this.hitpoints < 1) {
    this.dead = true
    msg ("It whited out!")
    player.exp = player.exp + 20
    player.pokedollar = player.pokedollar + this.old_parent.pokedollar
    RemoveObject (this)
    player.parent = player.old_room
    AddToInventory (Zephyr Badge)
    player.badge = player.badge + 1
  }
}
obj.hero = false
obj.alias = "Pidgeotto"
obj.listalias = CapFirst(obj.alias)
obj.look = ProcessText("A " + obj.alias + ". A flying type. A common sight in forests and woods. It's very docile. It flaps its wings at ground level to kick up blinding sand. If threatened, it rapidly flaps its wings in the grass, stirring up a dust cloud that drives insect prey out into the open.")
obj.hitpoints = 40
obj.max = 40
obj.damage = 9
obj.maxdamage = 9
obj.attack = 6
obj.maxattack = 6
obj.defence = 6
obj.maxdefence = 6
obj.armour = 10
obj.maxarmour = 10
obj.selectweapon => {
  this.weapon = GetObject(PickOneString("Tackle;Gust"))
}
obj.firearmdamage = 12
obj.firearmattack = 6
obj.ammo = 20
obj.ammomax = 20
obj.choosepotion => {
  if (this.hitpoints < this.max / 4) {
    if (RandomChance(25)) {
      if (this.old_parent.potion = 1) {
        this.old_parent.potion = obj.old_parent.potion - 1
        this.hitpoints = this.max
        msg (this.old_parent.potion + " used an Oran Berry on " + this.name + "<br/>Health was restored!")
      }
    }
  }
}

Richard Headkid
16 Aug 2019, 22:34

Hello.

I'll give this a shot.

Breaking the beginning of the code down . . .

The first bit:

player.current_room = player.parent
player.old_room = player.current_room
MoveObject (player, Battle Room)
if (HasInt(game, "crittercount")) {
  game.crittercount = game.crittercount + 1
}
else {
  game.crittercount = 1
}
create ("crittercount" + game.crittercount)
obj = GetObject("crittercount" + game.crittercount)

At this point, the player is in Battle Room.

Also, the obj variable is pointing to a newly created object which has no parent attribute set yet.

Most importantly, though, a room variable has not been created. So, the next line of code will throw an error:

obj.parent = room

It appears that you are writing this code specifically for Pidgeotto. So, as long as Falkner is definitely the desired target of obj.parent in this code, you might as well just change the above line of code to the following:

obj.parent = Falkner

Alternatively, you could just do this to set a trainer attribute which will point to Falkner:

obj.trainer = Falkner

jmnevil54
16 Aug 2019, 22:44

No KV, obj.parent = room is the same code the Pixie uses for his zombie apocalypse game, and it's correct.

object.parent = Falkner

Yeah, but what am I gonna do for Stacie, Stephanie, Tyler, Jake, Patrick, Paul, Hillary, Zack, Gary....

Point is, I need something I can copy-paste.


mrangel
16 Aug 2019, 23:42

Most importantly, though, a room variable has not been created.

I think it's a parameter to the function. In which case, check what is being passed to it.

Calling the variable room when it isn't a room is still really dumb, though. Giving variables/attributes names that don't indicate their purpose just makes it harder for anyone who wants to help.


Dcoder
17 Aug 2019, 01:13

And just to complicate things...

player.current_room = player.parent
player.old_room = player.current_room

This code doesn't seem to make sense, as both player.current_room and player.old_room will then be identical. Maybe the order of these lines should be reversed?

player.old_room = player.current_room
player.current_room = player.parent

This way, player.current_room will be the current room, and player.old_room will be the last different room (if this code is run as a change script) or last turn's room (if this code is run as a turn script)?


Richard Headkid
17 Aug 2019, 02:53

Yeah, but what am I gonna do for Stacie, Stephanie, Tyler, Jake, Patrick, Paul, Hillary, Zack, Gary....

I assumed this line of code (from your script) rendered that question moot:

obj.alias = "Pidgeotto"

I think it's a parameter to the function.

Oh.


I keep getting the "cannot use + add arithmetic with object" error. It thinks obj.old_parent.pokedollar is an object.

Hrmm . . .

Is obj.old_parent actually defined? I think Quest treats undefined attributes as if they were empty objects.

Anyway, if you were to copy and paste the entire error message for us, that might shed light on something we're not seeing.


And just to complicate things...

That bit of her code runs before moving the player. So, in this code, it probably works correctly.


jmnevil54
17 Aug 2019, 03:55

@Dcoder KV is right, now anyways. Also, the code came from hegemonkhan, so it isn't the shortest code.


jmnevil54
17 Aug 2019, 04:22

Alright. I printed an expression for obj.old_parent, obj.old_parent is Falkners Gym 6.

I made new code.

player.current_room = player.parent
player.old_room = player.current_room
MoveObject (player, Battle Room)
if (HasInt(game, "crittercount")) {
  game.crittercount = game.crittercount + 1
}
else {
  game.crittercount = 1
}
create ("crittercount" + game.crittercount)
obj = GetObject("crittercount" + game.crittercount)
obj.parent = room
obj.old_parent = obj.parent
GetDirectChildren (obj.parent)
if (HasAttribute(result, "lose")) {
  this.name = obj.old_parent
}
obj.dead = false
obj.parent = player.parent
obj.displayverbs = Split("Look at;Attack;Direct Hit;Stat;Status", ";")
obj.changedhitpoints => {
  if (this.hitpoints < 1) {
    this.dead = true
    msg ("It whited out!")
    player.exp = player.exp + 20
    player.pokedollar = player.pokedollar + this.old_parent.pokedollar
    RemoveObject (this)
    player.parent = player.old_room
    AddToInventory (Zephyr Badge)
    player.badge = player.badge + 1
  }
}
obj.hero = false
obj.alias = "Pidgeotto"
obj.listalias = CapFirst(obj.alias)
obj.look = ProcessText("A " + obj.alias + ". A flying type. A common sight in forests and woods. It's very docile. It flaps its wings at ground level to kick up blinding sand. If threatened, it rapidly flaps its wings in the grass, stirring up a dust cloud that drives insect prey out into the open.")
obj.hitpoints = 40
obj.max = 40
obj.damage = 9
obj.maxdamage = 9
obj.attack = 6
obj.maxattack = 6
obj.defence = 6
obj.maxdefence = 6
obj.armour = 10
obj.maxarmour = 10
obj.selectweapon => {
  this.weapon = GetObject(PickOneString("Tackle;Gust"))
}
obj.firearmdamage = 12
obj.firearmattack = 6
obj.ammo = 20
obj.ammomax = 20
obj.choosepotion => {
  if (this.hitpoints < this.max / 4) {
    if (RandomChance(25)) {
      if (this.old_parent.potion = 1) {
        this.old_parent.potion = obj.old_parent.potion - 1
        this.hitpoints = this.max
        msg (this.old_parent.potion + " used an Oran Berry on " + this.name + "<br/>Health was restored!")
      }
    }
  }
}
msg (obj.old_parent)

This code is supposed to fix the issue.

GetDirectChildren (obj.parent)
if (HasAttribute(result, "lose")) {
  this.name = obj.old_parent
}

This code prints the expression.

msg (obj.old_parent)

Richard Headkid
17 Aug 2019, 05:15

If you're still getting the same error, try:

msg("DEBUGGING: Next line will show the value of: obj.old_parent.pokedollar:")
msg (obj.old_parent.pokedollar)

My theory is Falkners Gym 6 does not have a defined pokedollar attribute. (It's more like a wild guess than a theory.)


Richard Headkid
17 Aug 2019, 05:24

PS

When posting a big block of code like that, I make it a "spoiler" thingy to make it easier for everyone to read the other important bits in my post.

Like this:


Here is the code:

<details>

```
CODE GOES HERE
```

</details>


That looks like this in the post:

Here is the code:

CODE GOES HERE

NOTE:

Sometimes I have to add extra line breaks in between the <details> line and the first line with ```


mrangel
17 Aug 2019, 09:52
GetDirectChildren (obj.parent)
if (HasAttribute(result, "lose")) {
  this.name = obj.old_parent
}

What is that supposed to do?

Seriously, it looks like you're deliberately adding complexity just to mess with people.

Going through your code line-by-line:

  • GetDirectChildren (obj.parent) - Gets a list of objects in the same room as obj, then doesn't do anything with it
  • if (HasAttribute(result, "lose")) { - generates an error because there isn't a variable named result
    • this.name = obj.old_parent - two errors. You can't ever set this.name because name cannot be changed during a game. And in this case, there's no this.
  • } - this line doesn't have any errors.

I think what you meant is:

foreach (result, GetDirectChildren (room)) {
  if (HasAttribute (result, "lose")) {
    room = result
  }
}

That will change the room variable from the room, to the trainer.

However you never use the room variable for anything else, so it would make a lot more sense to name it something else.

Better solution:

Remove the room parameter, and have a trainer parameter instead

Then you can change SpawnFalknerPidgeotto (this.parent) to SpawnFalknerPidgeotto (Falkner) or SpawnFalknerPidgeotto (this)

Then you can just do: obj.trainer = trainer and you've got the attribute you want.

(Note that if the parameter to the function is the trainer, I recommend calling it trainer rather than room, so that anyoone else looking at your code doesn't get confused. And the attribute should be called obj.trainer, not obj.old_parent, because it isn't the critter's parent. You keep on doing this. You have attributes and variables whose name isn't what they actually contain. Then every time you post a problem you've got a dozen people making suggestions based on incorrect information, and you try to include all their suggestions. By this point, half your game is junk code that attempts to fix what someone thought a problem might have been; code that does nothing but introduce more bugs. Using meaningful names for your variables and attributes would be the first step towards actually fixing the bugs)


jmnevil54
17 Aug 2019, 12:45

@KV Thank you! Cool.
@mrangel Thank you! Had I known it was that easy to make a parameter, I would have done it already.


jmnevil54
18 Aug 2019, 22:37

The code works for the money, so it should work for the potions! Thank you!