Using a key to open a door

I was wondering if anyone had any experience with coding a situation where the user finds a key and then has access to open a door? I assumed they would “activate” the key and it would disappear (so it seems like they are taking it). But how would I code a door to open (on activate) only after I have retrieved the key?

Hi Mark,

You would need to store the fact that you’ve found the key as a variable, e.g. “foundKey”, which you could initially set to zero, and change to 1 once the key is found.

When you reach the door, check the value of “foundKey” variable, and if it’s 1 then open the door, else display error.

Cheers!
Geoff @ TechLeap

Geoff,

Oh my goodness, that makes perfect sense! We were just discussing this in our coding section. Initially, we were using a dictionary as an option, but that would be silly. Changing the variable’s value would make more sense. I’ll keep you posted on what we were able to do. I like seeing the students problem solve this without me giving away the exact code.

Best wishes,
Mark

Yep, you could talk about using a variable to not just store value, but state.

Glad to help!

Geoff @ TechLeap

Similarly, you can have a variable KeyCount, and have several keys that need to be collected. We do versions of the Sunken Ship scene where the player has to collect Chests or Skulls or Parrots etc. OnActivate you increase a counter variable, such as ChestsCollected, SkullsCollected, Then we use a character to be the ‘keeper of the count’. OnActivate of the character, if the ChestsCollected is the correct total he might say a phrase that includes the variable count, such as
“You have collected 4 Chests!. Good Job!. Now on to the next chapter” and then we add code to go to the next scene.