As fun as it can be to include challenges in your hold, it's easy to get it wrong. I've done it myself. So I thought it would be good to put down some sample code for people to look at in order to avoid common problems.
These are the main common pitfalls in challenge scripting, put in a spoiler so that you don't have to scroll past them when checking for the sample script:
Click here to view the secret text
×
1. Failing the challenge and leaving the room prevents the challenge from ever being awarded.
This is a problem that can arise when using the End command. What usually happens is that the challenge has a fail condition, (such as "don't step on this pressure plate") and the script checks if that condition is met, then uses an End command if it is.
However, the End command permanently removes the character it's on. So if you do this, if the player meets the fail condition and then leaves the room, the challenge will never be awarded unless the player restores to an older save. This is very inconvenient!
2. It's possible to get the challenge without being able to leave the room.
This can happen if you simply use a "Wait for clean room" command before awarding the challenge, without checking anything else. If the room involves a trap of some sort that prevents the player from leaving, the challenge will be awarded even if the player is trapped. This can sometimes result in trivial solutions.
3. The challenge awards if you re-enter the room after clearing it, regardless of whether you met the conditions or not.
This is another potential consequence of using "Wait for clean room". If you don't set the checks properly, the challenge may be awarded immediately on setting foot into the room if it's already clean.
4. There is a seeding beacon in the room but the challenge isn't reset properly if the beacon is hit.
This is less common, but it's still something to be aware of. Aside from the above issues, this could be caused by failing to reset a variable used for the challenge. Or, if the script checks the status of a beacon and it's possible for that to be changed later in a turn than characters move, (such as by a bomb or firetrap going off) then the script won't activate if the player does that and leaves the room on the very next turn.
So how can you avoid those issues? Well, here's some sample code. (I based this example on the scripting for Xindaris' entry The Persistence of Briar in the Arky's Revenge contest.)
For a challenge that involves "
don't do something"
:
Click here to view the secret text
×Use two characters.
Character 1:
Set var ChallengeFailure = 0
whatever code checks if the player is doing the thing they shouldn't
Set var ChallengeFailure = 1
Character 2: (moves after Character 1)
If
Wait for clean room
Else
Wait for clean room
if necessary, check if the player can exit the room here
If
Wait until var ChallengeFailure = 0
Challenge completed "Sample challenge"
If End
If End
It's not hard to vary that for a challenge that involves doing something and then clearing the room. For convenience I've copied the code and made the appropriate tweaks.
Click here to view the secret text
×Use two characters.
Character 1:
Set var ChallengeSuccess = 0
whatever code checks if the player is doing the thing they should
Set var ChallengeSuccess = 1
Character 2: (moves after Character 1)
If
Wait for clean room
Else
Wait for clean room
if necessary, check if the player can exit the room here
If
Wait until var ChallengeSuccess = 1
Challenge completed "Sample challenge"
If End
If End
If you're checking for multiple things (for instance, a challenge that involves not turning your sword and also stepping on a pressure plate), then have a separate version of Character 1 for each thing you're checking, obviously using different variables for each. That will be easier than trying to make one character do a bunch of different checks. You can then just check all the conditions at once when the room is clear.
If a challenge isn't directly checking if the player does or doesn't do something, it might be checking whether or not something is true (say, a door is closed) at the end of the room. In that case, you would use the Character 2 code and do a check for whatever else you're looking at before awarding the challenge.
If you're doing some weird other check, well, I don't know what that might be, but you can probably adapt the structure of the example regardless.
How the code works:
Click here to view the secret text
×Whenever you re-enter the room, the characters will start from the beginning of their scripts. So, ChallengeFailure or ChallengeSuccess will go to zero.
Character 2's code may be a bit confusing if you're not very familiar with scripting. When an If command is used, a "Wait for" command immediately after it becomes "if this is true, do X, otherwise do what's in the Else section if there is one". Without an If command, a "Wait for" command instead means "don't do anything until this is true, then do X".
So this code means that if you enter the room when it's clear, the script will do what's after "Wait for clean room" that isn't in Else. In this case, that's nothing, which means the challenge won't be awarded.
If the room isn't cleared on entry, the script goes to the Else branch, which means it doesn't do anything until the room is cleared. Then it checks the variable to see if the challenge should be awarded.
[Last edited by Dragon Fogel at 10-30-2019 07:44 PM]