TheGreatCthulhu
Member
Posts: 213
Threads: 10
Joined: Oct 2010
Reputation:
32
|
RE: Need script help asap! extremely difficult script!
OK then, here's a script for that - all you need to do is to replace the names of the script areas to match the names in your map, as well as the names of the sound files.
const int INVALID_VALUE = -1; // represents some invalid value, just for readability... const int PASS_LENGTH = 4;
int secretPassCode = 4739; int numPresses = 0; // the number of button presses (so far) int userEntry = 0;
void OnEnter() { SetEntityPlayerInteractCallback("ScriptArea_1", "OnButtonPress", false); SetEntityPlayerInteractCallback("ScriptArea_2", "OnButtonPress", false); SetEntityPlayerInteractCallback("ScriptArea_3", "OnButtonPress", false); SetEntityPlayerInteractCallback("ScriptArea_4", "OnButtonPress", false); SetEntityPlayerInteractCallback("ScriptArea_5", "OnButtonPress", false); SetEntityPlayerInteractCallback("ScriptArea_6", "OnButtonPress", false); SetEntityPlayerInteractCallback("ScriptArea_7", "OnButtonPress", false); SetEntityPlayerInteractCallback("ScriptArea_8", "OnButtonPress", false); SetEntityPlayerInteractCallback("ScriptArea_9", "OnButtonPress", false); SetEntityPlayerInteractCallback("ScriptArea_0", "OnButtonPress", false); SetEntityPlayerInteractCallback("ScriptArea_Accept", "OnAccept", false); SetEntityPlayerInteractCallback("ScriptArea_Cancel", "OnCancel", false); AddDebugMessage("pass: " + secretPassCode, false); }
// This function handles numbered buttons void OnButtonPress(string &in sender) { if (numPresses >= PASS_LENGTH) // all digits were entered { // THIS SOUND PLAYS if the player tries to enter more than 4 digits PlaySoundAtEntity("", "no_more_allowed_sound.snt", sender, 0.0f, false); // Don't allow any more digits to be entered return; }
int digit = GetDigitFromButton(sender); if (digit == INVALID_VALUE) return; PlaySoundAtEntity("", "button_press_sound.snt", sender, 0.0f, false); userEntry += digit * Pow(10, PASS_LENGTH - 1 - numPresses); AddDebugMessage(sender + "; SUM: " + userEntry, false); ++numPresses; // increments numPresses by 1 }
void OnAccept(string &in sender) { if (userEntry == secretPassCode) { SetSwingDoorLocked("door_1", false, false); // Unlocks the door PlaySoundAtEntity("", "unlock_door.snt", "door_1", 0.0f, false); } else { PlaySoundAtEntity("", "passcode_error_sound.snt", sender, 0.0f, false); } ResetUserCodeVars(); }
void OnCancel(string &in sender) { PlaySoundAtEntity("", "cancel_sound.snt", sender, 0.0f, false); ResetUserCodeVars(); }
// This function resets the input code, allowing the player to retry void ResetUserCodeVars() { userEntry = 0; numPresses = 0; }
int GetDigitFromButton(string &in buttonID) { int result = INVALID_VALUE; if (buttonID == "ScriptArea_1") result = 1; else if (buttonID == "ScriptArea_2") result = 2; else if (buttonID == "ScriptArea_3") result = 3; else if (buttonID == "ScriptArea_4") result = 4; else if (buttonID == "ScriptArea_5") result = 5; else if (buttonID == "ScriptArea_6") result = 6; else if (buttonID == "ScriptArea_7") result = 7; else if (buttonID == "ScriptArea_8") result = 8; else if (buttonID == "ScriptArea_9") result = 9; else if (buttonID == "ScriptArea_0") result = 0; return result; }
// Raises an integer to some power int Pow(int number, int power) // doesn't support negative powers! { if (power == 0) return 1; int result = number; for (int i = 1; i < power; ++i) result *= number; return result; }
BTW, if your digit-related script areas are named in a similar way, you can replace all of these
SetEntityPlayerInteractCallback("ScriptArea_1", "OnButtonPress", false); SetEntityPlayerInteractCallback("ScriptArea_2", "OnButtonPress", false); SetEntityPlayerInteractCallback("ScriptArea_3", "OnButtonPress", false); SetEntityPlayerInteractCallback("ScriptArea_4", "OnButtonPress", false); SetEntityPlayerInteractCallback("ScriptArea_5", "OnButtonPress", false); SetEntityPlayerInteractCallback("ScriptArea_6", "OnButtonPress", false); SetEntityPlayerInteractCallback("ScriptArea_7", "OnButtonPress", false); SetEntityPlayerInteractCallback("ScriptArea_8", "OnButtonPress", false); SetEntityPlayerInteractCallback("ScriptArea_9", "OnButtonPress", false); SetEntityPlayerInteractCallback("ScriptArea_0", "OnButtonPress", false);
with just this
for (int i = 0; i < 10; ++i) SetEntityPlayerInteractCallback("ScriptArea_" + i, "OnButtonPress", false);
Also, I just noticed that my Pow() function contained a bug in the version a few posts back, so I fixed it here.
EDIT: Also updated the original post.
This is what the script does: the player is allowed to enter up to 4 numbers from a 10-button keypad (0-9). If the player tries to enter 5 or more numbers, a sound is played to indicate that this is not allowed. The player can press the cancel button at any time, and start over (cancel sound plays). When done, the player must press the accept button. If the code is correct, the door is unlocked, and the appropriate sound follows, if the code is wrong, the error sound is heard, and then the player is free to try again.
Copy the script (from the first code box) to your hps file, and replace these names to match those in your map:
- "ScriptArea_0" through "ScriptArea_9" - these are the areas for numerical buttons, in OnEnter() and GetDigitFromButton() functions;
- "ScriptArea_Cancel" - for the Cancel button, in OnEnter();
- "ScriptArea_Accept" - for the Accept button, in OnEnter();
- "door_1" - replace with the name of your door, in OnAccept();
- "no_more_allowed_sound.snt" - the sound that is played if the player tries to enter more than 4 digits, in OnButtonPress();
- "button_press_sound.snt" - heard on (allowed) button presses, in OnButtonPress();
- "unlock_door.snt" - for the Accept button, when the pass is correct and the door unlocks, in OnAccept();
- "passcode_error_sound.snt" - when the pass is wrong, in OnAccept();
- "cancel_sound.snt" - when the Cancel button is pressed, in OnCancel().
|
|