Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need script help asap! extremely difficult script!
TheGreatCthulhu Offline
Member

Posts: 213
Threads: 10
Joined: Oct 2010
Reputation: 32
#8
RE: Need script help asap! extremely difficult script!

The "a b c things" are not script, it's just me trying to explain to you what to do on a conceptual level.

Here, I made you an example: there's a single map and a relatively short hps script - just unpack to the custom_stories folder. It demonstrates how to do a pass code. Since you said you're using script areas, I made a similar setup. For simplicity, the pass code has only 3 digits. There's a door, and a... eh... console of sorts xD, which enables you to enter the password. The password is set in the example.hps script file, and it's currently 231 - but you can change that.

Here's the most important part of the script:
PHP Code: (Select All)
const int INVALID_VALUE = -1;   // represents some invalid value, just for readability...

int secretPassCode 231;
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);
}

void OnButtonPress(string &in sender)
{
   
    
int digit GetDigitFromButton(sender);
    if (
digit == INVALID_VALUE)
        return;
        
    
PlaySoundAtEntity("""static_rock_start.snt"sender0.0ffalse);
        
    
userEntry += digit Pow(10numPresses);
    
    
AddDebugMessage(sender "; SUM: " userEntryfalse);
    
    ++
numPresses;   // increments numPresses by 1

    
if (numPresses >= 3)    // all digits were entered
    
{
        if (
userEntry == secretPassCode)
        {
            
SetSwingDoorLocked("door_1"falsefalse);   // Unlocks the door
            
PlaySoundAtEntity("""unlock_door.snt""door_1"0.0ffalse);
        }

        
// Otherwise, reset all the variables, so that the user can try again
        
userEntry 0;        
        
numPresses 0;            
    }


As you can see, it's not very long. A constant and some variables are declared at the top. Then, in OnEnter(), the same interaction callback function is hooked up with each script area. It's the OnButtonPress() function, and it get's called whenever a "button" (script area) is interacted with.

The line
int digit = GetDigitFromButton(sender);
calls another function I made, which simply takes the script area name as an argument, and returns a corresponding digit.
Here's how:
PHP Code: (Select All)
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;
    
    return 
result;


Now, in the OnButtonPress() function, this is the important line - it's the line that actually implements the "a b c thing" I was talking about:
userEntry += digit * Pow(10, 2 - numPresses);

I used the += shorthand, but it can be also written like this:
userEntry = userEntry + (digit * Pow(10, 2 - numPresses));

Here, the digit obtained earlier is first multiplied by a power of 10, depending where in the sequence it should be (that's what the Pow() function does). Then it's added to the overall sum that is going to become the user entry when the final button is pressed.

After each button press, I increment the numPresses counter. The line
++numPresses;

is equivalent to
numPresses = numPresses + 1;

Then I simply check if all 3 buttons where pressed, and if so, I compare the user code to the secret code. If they are equal, I unlock the door, if not, I reset everything, so that the user can try again.

BTW, the Pow() function is another function I wrote for this script:
PHP Code: (Select All)
// Raises an integer to some power
int Pow(int numberint power)  // doesn't support negative powers!
{
    if (
power == 0)
        return 
1;
   
    for (
int i 1power; ++i)
        
number *= number;
        
    return 
number;


EDIT: There's a bug in the Pow() function - it gives incorrect numbers for the powers greater than 2 (which is important if there's more than 3 digits). Here's the correct version - just replace it with this in the HPL file if downloading:
PHP Code: (Select All)
// Raises an integer to some power
int Pow(int numberint power)  // doesn't support negative powers!
{
    if (
power == 0)
        return 
1;
        
    
int result number;
   
    for (
int i 1power; ++i)
        
result *= number;
        
    return 
result;


That's the entire script - it's not that complicated.

P.S. If there happen to be any problems with the map, delete the map_cache file (in the "maps" dir), just in case.


Attached Files
.rar   tgc_passCodeExample.rar (Size: 94.64 KB / Downloads: 158)
(This post was last modified: 12-20-2012, 11:47 AM by TheGreatCthulhu.)
12-19-2012, 07:00 PM
Find


Messages In This Thread
RE: Need script help asap! extremely difficult script! - by TheGreatCthulhu - 12-19-2012, 07:00 PM



Users browsing this thread: 3 Guest(s)