Help With Else and If, and a Locked Box
missrithenay
23 Apr 2014, 21:16Hi everyone,
I am still trying to program a game for my classroom in social studies... it's been uphill because I've NO experience in programming, but I'm enjoying the process. Until now... I am trying to create a puzzle where the player enters a room with four pillars. They need to place four blocks on the correct pillars to receive a key which unlocks a box. Two problems...
1. At various times, placing the block in the correct place elicits no response and does not remove the object from inventory, although it does seem to set the object flag as I want it to, suggesting I messed something up in the "else" programming. I'm trying to make it so this only works if all four pillars are set correctly.
2. The key, once received, unlocks the box, but will not open it. I noticed that under container I can't check the box "can be opened" (perhaps because it's set to lockable item). I *do* have the "open automatically if player has key" box checked, but that doesn't seem to be happening.
I'm attaching two versions of the game, because I tried it two different ways -- one using "else" one using "else if" (not clear on the difference between the two commands). Both have a walkthrough that takes you to the room with the problem. If anyone has time/inclination to help, I would be VERY grateful
I am still trying to program a game for my classroom in social studies... it's been uphill because I've NO experience in programming, but I'm enjoying the process. Until now... I am trying to create a puzzle where the player enters a room with four pillars. They need to place four blocks on the correct pillars to receive a key which unlocks a box. Two problems...
1. At various times, placing the block in the correct place elicits no response and does not remove the object from inventory, although it does seem to set the object flag as I want it to, suggesting I messed something up in the "else" programming. I'm trying to make it so this only works if all four pillars are set correctly.
2. The key, once received, unlocks the box, but will not open it. I noticed that under container I can't check the box "can be opened" (perhaps because it's set to lockable item). I *do* have the "open automatically if player has key" box checked, but that doesn't seem to be happening.
I'm attaching two versions of the game, because I tried it two different ways -- one using "else" one using "else if" (not clear on the difference between the two commands). Both have a walkthrough that takes you to the room with the problem. If anyone has time/inclination to help, I would be VERY grateful


jaynabonne
23 Apr 2014, 22:08For 1)...
I think your orig version looks better. The only problem I could see is that the "justice" pillar's script has commands missing parameters. (I had just the luck to do justice last, so I saw the error.)
You can simplify it, though. What you want to check for is that all four blocks are on their pillars. That means when you add one (e.g. D), you want to know that the other three are on (A and B and C all set). By using that idea (and factoring out duplicate code), you can replace this:
with this:
Note that you always set the object flag on, and you always move the object, so you can move those outside the "if". Also, notice that I preserved your typo, but you can fix that yourself.
(It says "freedom" instead of "equity".)
I think your orig version looks better. The only problem I could see is that the "justice" pillar's script has commands missing parameters. (I had just the luck to do justice last, so I saw the error.)
You can simplify it, though. What you want to check for is that all four blocks are on their pillars. That means when you add one (e.g. D), you want to know that the other three are on (A and B and C all set). By using that idea (and factoring out duplicate code), you can replace this:
<item key="equity">
if (GetBoolean(freedom, "set")) {
if (GetBoolean(representation, "set")) {
if (GetBoolean(justice, "set")) {
msg ("You set the freedom block in its place. The pillars shudder and suddenly glow. A key appears in your hand.")
AddToInventory (pillar key)
SetObjectFlagOn (equity, "set")
MoveObject (equity block, equity)
}
else {
msg ("You set the equity block in its place. You hear a distinctive \"click.\"")
SetObjectFlagOn (equity, "set")
MoveObject (equity block, equity)
}
}
else {
msg ("You set the equity block in its place. You hear a distinctive \"click.\"")
SetObjectFlagOn (equity, "set")
MoveObject (equity block, equity)
}
}
else {
msg ("You set the equity block in its place. You hear a distinctive \"click.\"")
SetObjectFlagOn (equity, "set")
MoveObject (equity block, equity)
}
</item>
with this:
<item key="equity">
SetObjectFlagOn (equity, "set")
MoveObject (equity block, equity)
if (GetBoolean(freedom, "set") and GetBoolean(representation, "set") and GetBoolean(justice, "set")) {
msg ("You set the freedom block in its place. The pillars shudder and suddenly glow. A key appears in your hand.")
AddToInventory (pillar key)
}
else {
msg ("You set the equity block in its place. You hear a distinctive \"click.\"")
}
</item>
Note that you always set the object flag on, and you always move the object, so you can move those outside the "if". Also, notice that I preserved your typo, but you can fix that yourself.


jaynabonne
23 Apr 2014, 22:33For 2), there seems to be some sort of bug that I don't understand. I'll take a look later, but something definitely feels wrong.
When you try to open the box, you get the message, "No verb response defined", which is generated by the default verb handling code. But I don't think open normally goes through that code, which is why it's failing - the "open" attribute is a Boolean, which the verb code doesn't know how to deal with. When I overrode that and made it a string, it printed the string but bypassed all the normal open functionality. For some reason, it's not going down the right path. There might be something odd in your setup or it might be an outright bug. I (or someone else) will look and see.
When you try to open the box, you get the message, "No verb response defined", which is generated by the default verb handling code. But I don't think open normally goes through that code, which is why it's failing - the "open" attribute is a Boolean, which the verb code doesn't know how to deal with. When I overrode that and made it a string, it printed the string but bypassed all the normal open functionality. For some reason, it's not going down the right path. There might be something odd in your setup or it might be an outright bug. I (or someone else) will look and see.
missrithenay
24 Apr 2014, 01:20Sorry, one more question. I tried copying and pasting the code directly as you wrote it, and this is the response I get when I try to play:
Failed to load game.
The following errors occurred:
Error: Error adding script dictionary 'selfuseon' to element 'freedom block': Invalid variable name ''
Thank you
The typo is what happens when you change things fifteen times...
Not the most fun game, but not the most interesting material. Still better than a text book!
Thanks again.
Failed to load game.
The following errors occurred:
Error: Error adding script dictionary 'selfuseon' to element 'freedom block': Invalid variable name ''
Thank you

Not the most fun game, but not the most interesting material. Still better than a text book!
Thanks again.
HegemonKhan
24 Apr 2014, 05:00I'll try to explain 'if else' and 'else', as Jay's already working with you on your code:
do you like the color: red (if), blue (else if), yellow (else if), orange (else if), purple (else if), green (else if) ??? Otherwise, you get pizza (else).
do you like the color: red (if), blue (else if), yellow (else if), orange (else if), purple (else if), green (else if) ??? Otherwise, you get pizza (else).
if (you.like = "red") {
msg ("You get a strawberry")
] else if (you.like = "blue") {
msg ("You get a blueberry")
} else if (you.like ="yellow") {
msg ("You get a banana")
} else if (you.like = "orange") {
msg ("You get a pumpkin.")
} else if (you.like = "purple") {
msg ("You get a grape")
} else if (you.like = "green") {
msg ("You get spinach")
} else {
msg ("You get pizza")
}

jaynabonne
24 Apr 2014, 06:40missrithenay (miss?): could you please post your game file again with the change? I just munged your code around t make what I handed back to you. There is undoubtedly an error somewhere, but I can't tell from here. 
(HK: by looking at the code, you can tell there's no confusion about what "if" and "else" mean as concepts! It's the application in this tricky case that's the issue.)

(HK: by looking at the code, you can tell there's no confusion about what "if" and "else" mean as concepts! It's the application in this tricky case that's the issue.)
missrithenay
24 Apr 2014, 16:28Thank you again a thousand times. File attached.

Pertex
24 Apr 2014, 16:59Ui, your selfuseon scripts seem to be a bit broken.
And did you rename the object "freedom block" to "freedom_block" ? Then you have to update all calls of this object in the code. Here is the corrected file
<item key="representation"><![CDATA[
<item___SPACE___key = "freedom"> <------what is this??? I removed it
SetObjectFlagOn (freedom, "set")
MoveObject (freedom block, freedom)
if (GetBoolean(equity, "set") and GetBoolean(representation, "set") and GetBoolean(justice, "set")) {
msg ("You set the freedom block in its place. The pillars shudder and suddenly glow. A key appears in your hand.")
AddToInventory (pillar key)
}
else {
msg ("You set the freedom block in its place. You hear a distinctive \"click.\"")
}
</item> <-------- double end tag of <item>. Did you copy this code?
]]></item> <--------
And did you rename the object "freedom block" to "freedom_block" ? Then you have to update all calls of this object in the code. Here is the corrected file
missrithenay
24 Apr 2014, 17:53OK. Thank you 
