Is Something Wrong with My Code?

LadySnowflake
17 Aug 2016, 09:07

While I was working on the simple game from the tutorial, XanMag had the idea to use a newspaper to squash the annoying bee in the kitchen. I decided to try this tonight, to see if I could do it without help. I went through the process of setting up scripts that would allow me to use either the newspaper from the lounge or a roll of paper towels in the kitchen to swat the bee. It works, but after I use either object on the bee, I still get the message "The bee buzzes past you. Pesky bee," which is the message that's supposed to print every twenty seconds when the timer is activated. It only prints the message once after the bee is swatted, but I wasn't expecting it to print it again at all.

Below is my code from the two objects I used on the bee, as well as my code from the bee. Is there something wrong somewhere that is making this happen?

if (GetBoolean(bee, "Dead")) {
DisableTimer (Bee Timer)
RemoveObject (bee)
msg ("To your relief, the annoying bee is now no more.")
}
else {
msg ("A tiny yellow and black bumblebee.")
}

msg ("You swat the bee with the newspaper.")
SetObjectFlagOn (bee, "Dead")

msg ("You swat the bee with the roll of paper towels.")
SetObjectFlagOn (bee, "Dead")


The Pixie
17 Aug 2016, 10:16

I am not sure what you have done with the first script; where is it? It wants to be on the swat command. So for the newspaper, it might be this:

msg ("You swat the bee with the newspaper.")
DisableTimer (Bee Timer)
RemoveObject (bee)
msg ("To your relief, the annoying bee is now no more.")

Although it is the bee that everything happens to, the script needs to be with the trigger. In this case swatting with the newspaper is the trigger, so everything needs to go there, so it all happens at once.

Also, not a lot of point flagging the bee as dead as you are removing it altogether (or do not remove it, and have a dead bee lying around).


LadySnowflake
17 Aug 2016, 10:54

The first script is on the bee's "use/give" tab. I didn't add any additional verbs or commands. This is what I did:

  1. Changed the bee's "look at" description to run script, and added an "if" command.

  2. Chose the expression "object has flag," and entered the flag name "dead."

  3. Printed messages for both "then" and "else."

  4. Went to the bee's "use/give" tab and selected "use other object on this." Added both a roll of paper towels and a newspaper that could be used on the bee. Ran script and printed a message for each object.

  5. Added a "set object flag" command called "dead" on the bee.

After I did all that, I disabled the timer and printed the message. I don't know why I used an object flag and removed the object at the same time. All I can say is, it's almost four in the morning here, and I tend to not get around to editing this stuff until I'm half awake, so I blame it on that, lol. Thanks for pointing out the faulty logic there.

I don't know if that clarified anything, but there it is. Maybe I just need to come back to it later when I'm not tired.


hegemonkhan
17 Aug 2016, 17:03

Pixie already pointed the faulty logic out, but to explain it, here's my post (hopefully you can understand/follow it along):

you almost got it right (if I understand your previous/2nd-last post), but with a small logic mistake, let's walk through the (faulty) logic:

1. open window, (optional: having a 'bee' Object and moving it into the 'kitchen' room - this is not required if you just want/need the illusion of a bee being in the kitchen through the bee timer's bee msgs - though if you want to be able to do interactions with the bee, such as a 'look' description and using your 'bee.dead' Boolean Attribute, then having it be an actual Object and moving it into the kitchen, is required) flies into the room (enable bee timer)
2. the (now enabled) bee timer fires off (displays) its bee msgs every whatever X interval
3. the bee is alive (bee.dead = false)
4. use newspaper on (alive) bee
A. if bee is dead, disable bee timer
B. if bee is alive, make bee be dead (bee.dead = true)
4. since the bee is alive, we do B, which means that the bee timer has NOT been disabled, the bee has just been switched to now being dead (bee.dead = true), but this is after we've already went past the 'use newspapers' checking condition of 'if (bee.dead = true)'
5. and thus the bee timer is still enabled, and thus you're still seeing the bee msgs

6. (if you were to again - for a second time) use the newspaper on (now dead) bee
A. if bee is dead, disable bee timer
B. if bee is alive, make bee be dead (bee.dead = true)
6. since the bee is dead, we do A, which disables the bee timer, and thus no more bee msgs being displayed evey X interval.
7. the bee timer (is now) disabled, and thus NO more bee msgs.

to fix it... you can have/move the 'disable bee timer' Script into B's scripting location (it doesn't matter if it's before or after the 'set a variable or attribute: bee.dead=true' Script)

you also may want to put this into A's location (which is now empty): msg ("The bee is already dead, silly. There's no reason to use newspaper to swat a dead bee!")