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!
DnALANGE Offline
Banned

Posts: 1,549
Threads: 73
Joined: Jan 2012
#1
Need script help asap! extremely difficult script!

I have this script here///
The code = 070 atm...
Can;'t figure out anymore how to get 3 different codes like 123-098-678 or somthign like that
SCRIPT:

////////////BUTTONS/////////////

void num0(string &in entity) ///////////////BUTTON 0//////////////////
{
SetMessage("Messages", "num0", 2);
PlayGuiSound("elevator_button.ogg", 100);
AddLocalVarInt("varcheck", 1);
AddLocalVarInt("vartotal", 99);
if(GetLocalVarInt("vartotal") == 4)
{
varcheck();
}
}
void num1(string &in entity) ///////////////BUTTON 1//////////////////
{
SetMessage("Messages", "num1", 2);
PlayGuiSound("elevator_button.ogg", 100);
AddLocalVarInt("varcheck", 1);
AddLocalVarInt("vartotal", 99);
if(GetLocalVarInt("vartotal") == 4)
{
varcheck();
}
}
void num2(string &in entity) ////////////////BUTTON 2/////////////////
{
SetMessage("Messages", "num2", 2);
if(GetLocalVarInt("var2") == 0)
{
if(GetLocalVarInt("varcheck") > 0)
{
PlayGuiSound("elevator_button.ogg", 100);
AddLocalVarInt("varcheck", 1);
AddLocalVarInt("vartotal", 99);
SetLocalVarInt("var2", 1);
if(GetLocalVarInt("vartotal") == 4)
{
varcheck();
}
}
else
{
PlayGuiSound("elevator_button.ogg", 100);
AddLocalVarInt("varcheck", 1);
AddLocalVarInt("vartotal", 1);
SetLocalVarInt("var2", 1);
if(GetLocalVarInt("vartotal") == 4)
{
varcheck();
}
}
}
else if(GetLocalVarInt("var2") == 1)
{
if(GetLocalVarInt("var4") == 1)
{
AddLocalVarInt("vartotal", 1);
AddLocalVarInt("varcheck", 1);
if(GetLocalVarInt("vartotal") == 4)
{
varcheck();
}
}
else
{
AddLocalVarInt("vartotal", 99);
AddLocalVarInt("varcheck", 1);
if(GetLocalVarInt("vartotal") == 4)
{
varcheck();
}
}
}
}
void num3(string &in entity) ///////////////BUTTON 3//////////////////
{
SetMessage("Messages", "num3", 2);
PlayGuiSound("elevator_button.ogg", 100);
AddLocalVarInt("varcheck", 1);
AddLocalVarInt("vartotal", 99);
if(GetLocalVarInt("vartotal") == 4)
{
varcheck();
}
}
void num4(string &in entity) ////////////////BUTTON 4/////////////////
{
SetMessage("Messages", "num4", 2);
if(GetLocalVarInt("var4") == 0)
{
if(GetLocalVarInt("vartotal") == 4)
{
PlayGuiSound("elevator_button.ogg", 100);
AddLocalVarInt("varcheck", 1);
AddLocalVarInt("vartotal", 1);
SetLocalVarInt("var4", 1);
if(GetLocalVarInt("vartotal") == 4)
{
varcheck();
}
}
else
{
PlayGuiSound("elevator_button.ogg", 100);
AddLocalVarInt("varcheck", 1);
AddLocalVarInt("vartotal", 99);
SetLocalVarInt("var4", 1);
if(GetLocalVarInt("vartotal") == 4)
{
varcheck();
}
}
}
else if(GetLocalVarInt("var4") == 1)
{
if(GetLocalVarInt("var4") == 1)
{
AddLocalVarInt("vartotal", 99);
AddLocalVarInt("varcheck", 1);
if(GetLocalVarInt("vartotal") == 4)
{
varcheck();
}
}
}
}
void num5(string &in entity) ///////////////BUTTON 5//////////////////
{
SetMessage("Messages", "num5", 2);
PlayGuiSound("elevator_button.ogg", 100);
AddLocalVarInt("varcheck", 1);
AddLocalVarInt("vartotal", 99);
if(GetLocalVarInt("vartotal") == 4)
{
varcheck();
}
}
void num6(string &in entity) ///////////////BUTTON 6//////////////////
{
SetMessage("Messages", "num6", 2);
PlayGuiSound("elevator_button.ogg", 100);
AddLocalVarInt("varcheck", 1);
AddLocalVarInt("vartotal", 99);
if(GetLocalVarInt("vartotal") == 4)
{
varcheck();
}
}
void num7(string &in entity) ///////////////BUTTON 7//////////////////
{
SetMessage("Messages", "num7", 2);
PlayGuiSound("elevator_button.ogg", 100);
AddLocalVarInt("varcheck", 1);
AddLocalVarInt("vartotal", 99);
if(GetLocalVarInt("vartotal") == 4)
{
varcheck();
}
}
void num8(string &in entity) ///////////////BUTTON 8//////////////////
{
SetMessage("Messages", "num8", 2);
PlayGuiSound("elevator_button.ogg", 100);
AddLocalVarInt("varcheck", 1);
AddLocalVarInt("vartotal", 99);
if(GetLocalVarInt("vartotal") == 4)
{
varcheck();
}
}
void num9(string &in entity) ///////////////BUTTON 9//////////////////
{
SetMessage("Messages", "num9", 2);
PlayGuiSound("elevator_button.ogg", 100);
AddLocalVarInt("varcheck", 1);
AddLocalVarInt("vartotal", 99);
if(GetLocalVarInt("vartotal") == 4)
{
varcheck();
}
}
void varcheck()
{
if(GetLocalVarInt("vartotal") == 4)
{
SetSwingDoorLocked("door_storagedoor_1", false, true);
PlayGuiSound("unlock_door.snt", 100);
SetEntityActive("0", false);
SetEntityActive("1", false);
SetEntityActive("2", false);
SetEntityActive("3", false);
SetEntityActive("4", false);
SetEntityActive("5", false);
SetEntityActive("6", false);
SetEntityActive("7", false);
SetEntityActive("8", false);
SetEntityActive("9", false);
CompleteQuest("hospitalquest02", "hospitalquest2");
StartEffectFlash(1.0f, 0.005, 1.0f);
PlayGuiSound("questcompleted01.ogg", 100);
}
else
{
SetLocalVarInt("var0", 0);
SetLocalVarInt("var1", 0);
SetLocalVarInt("var2", 0);
SetLocalVarInt("var3", 0);
SetLocalVarInt("var4", 0);
SetLocalVarInt("var5", 0);
SetLocalVarInt("var6", 0);
SetLocalVarInt("var7", 0);
SetLocalVarInt("var8", 0);
SetLocalVarInt("var9", 0);
SetLocalVarInt("varcheck", 0);
SetLocalVarInt("vartotal", 0);
PlayGuiSound("lock_door.snt", 100);
}
}
void ScriptArea_27(string &in asParent, string &in asChild, int alState)
{
AddQuest("hospitalquest02", "hospitalquest2");
}
void YourHP(string &in asTimer)
{
if(GetPlayerHealth() < 25)
{
SetMessage("Messages", "LowHP", 4);
AddTimer("Tyourhphigher", 1.0f, "YourHPHigher");
}
else if(GetPlayerHealth() > 25)
{
AddTimer("TyourhpAgain", 1.0f, "YourHP");
}
}
void YourHPHigher(string &in asTimer)
{
if(GetPlayerHealth() > 25)
{
AddTimer("TyourhphigherWAIT", 1.0f, "YourHP");
}
else if(GetPlayerHealth() < 25)
{
AddTimer("TyourhpAgain2", 1.0f, "YourHPHigher");
}
}

ohh and at the end ....
When pusehd 3 number THEN needed to press acceptbutton
So 3 numbers then accept to unlock a lock... but for now actuive a entity for easy use now!
THANXXX
12-18-2012, 10:08 PM
Find
xxxxxxxxxxxxxxxx Away
Posting Freak

Posts: 935
Threads: 0
Joined: Jun 2012
Reputation: 54
#2
RE: Need script help asap! extremely difficult script!

Quote: The code = 070 atm...

Can;'t figure out anymore how to get 3 different codes like 123-098-678 or somthign like that
Sorry, I'm a bit slow today...so before I try my luck digging through that epic script - what exactly are you trying to do?
Script a code lock that changes between three codes at random? Or one where you have to type in three different codes one after another? Or one that reacts differently depending on which one of the three codes you enter?
12-19-2012, 03:08 AM
Find
TheGreatCthulhu Offline
Member

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

Please use the code-tags when posting code (especially when posting looong code), and make sure the formating stays preserved - it helps us make sense of everything, and help you in return.

Instead of Set/Add-LocalVarInt() functions, you can simply use an integer variable (or more than one), as it is a bit easier to deal with.
You can declare them near the top of your script, outside of any function. Something like this:
int code;
int userEntry;


Then you can use the code & userEntry variables throughout your script file.
Note that the codes are just numbers - so once you have converted a sequence of the button presses to corresponding numbers, you can simply compare for equality.

You can even internally consider the whole 123-098-678 sequence as a single number, namely 123098678.

The trick is how to get from a sequence of button presses to the actual multi-digit number. Let's assume for the moment that the code has only 3 digits. If the user presses the button a, then b then c, you should be able to get the number abc. Well, take a look at this:

abc = (a * 100) + (b * 10) + (c * 1)

So, all you need to do is count how many buttons have been pressed since the start of the sequence, and multiply the digit connected to the current button by a corresponding power of 10, and then add the result to the overall sum. So, for the first button press, you do nothing, for the second, you multiply by 10, for the third, you multiply by 10*10, etc... (it helps if you start counting from 0, instead from 1 - then you can make the indices and the exponents match).

The steps are:
  1. Check what button is it in a row.
  2. Multiply it's digit by the corresponding power of 10 (say: int temp = 6 * 100;)
  3. Add the result to the overall sum: userEntry = userEntry + temp;
  4. If this was the last number, check if it is equal to the code.
  5. Respond accordingly.
12-19-2012, 10:32 AM
Find
DnALANGE Offline
Banned

Posts: 1,549
Threads: 73
Joined: Jan 2012
#4
RE: Need script help asap! extremely difficult script!

(12-19-2012, 10:32 AM)TheGreatCthulhu Wrote: Please use the code-tags when posting code (especially when posting looong code), and make sure the formating stays preserved - it helps us make sense of everything, and help you in return.

Instead of Set/Add-LocalVarInt() functions, you can simply use an integer variable (or more than one), as it is a bit easier to deal with.
You can declare them near the top of your script, outside of any function. Something like this:
int code;
int userEntry;


Then you can use the code & userEntry variables throughout your script file.
Note that the codes are just numbers - so once you have converted a sequence of the button presses to corresponding numbers, you can simply compare for equality.

You can even internally consider the whole 123-098-678 sequence as a single number, namely 123098678.

The trick is how to get from a sequence of button presses to the actual multi-digit number. Let's assume for the moment that the code has only 3 digits. If the user presses the button a, then b then c, you should be able to get the number abc. Well, take a look at this:

abc = (a * 100) + (b * 10) + (c * 1)

So, all you need to do is count how many buttons have been pressed since the start of the sequence, and multiply the digit connected to the current button by a corresponding power of 10, and then add the result to the overall sum. So, for the first button press, you do nothing, for the second, you multiply by 10, for the third, you multiply by 10*10, etc... (it helps if you start counting from 0, instead from 1 - then you can make the indices and the exponents match).

The steps are:
  1. Check what button is it in a row.
  2. Multiply it's digit by the corresponding power of 10 (say: int temp = 6 * 100;)
  3. Add the result to the overall sum: userEntry = userEntry + temp;
  4. If this was the last number, check if it is equal to the code.
  5. Respond accordingly.

WOW... never used those a b c things...
in can script a lot but never used those kind of thngs!!!

I know for some reason it can be very short..
let me explane what i have done for better instructions.
-----
I have a controlpanel with 1-0 and *#
ALL of them are NOT toutchable -> there are scriptarea's in front of them so in fact you toucht those!

I want a 4 digit code for example 1 3 5 7 THEN press ACCEPT ={ # in the editor }

IF Done right ..... like a door opens..
IF wrong ...... it will reset and you can try again over and over....
THIS button * i already fixed , it respwns you to the other plr-start.

Could someone make a script for this?

I don't really think so, might be to diffficulty... Sad

If it is NOT possible let me know wwhat i can do then.

THIS part is really importent to my FC.

THANKS for everyone trying to help me!
(This post was last modified: 12-19-2012, 03:08 PM by DnALANGE.)
12-19-2012, 03:07 PM
Find
The chaser Offline
Posting Freak

Posts: 2,486
Threads: 76
Joined: Jun 2012
Reputation: 113
#5
RE: Need script help asap! extremely difficult script!

It isn't actually a so hard script...

Do the following thing:

At the beggining:

-If you touch the wrong button, something (what you want) happens, but if you touch the correct one, you allow a script area (the next button) to add a LocalVar, if you tick the wrong button, ouch! :3

So:

////Long list of things

//When first good button is pressed
AddLocalVarInt("Buttons", 1);
//The thing so when you press the next correct button it adds a LocalVarInt.

It's quite simple for me :)

THE OTHERWORLD (WIP)
[Image: k6vbdhu]

Aculy iz dolan.
12-19-2012, 03:46 PM
Find
DnALANGE Offline
Banned

Posts: 1,549
Threads: 73
Joined: Jan 2012
#6
RE: Need script help asap! extremely difficult script!

(12-19-2012, 03:46 PM)The chaser Wrote: It isn't actually a so hard script...

Do the following thing:

At the beggining:

-If you touch the wrong button, something (what you want) happens, but if you touch the correct one, you allow a script area (the next button) to add a LocalVar, if you tick the wrong button, ouch! :3

So:

////Long list of things

//When first good button is pressed
AddLocalVarInt("Buttons", 1);
//The thing so when you press the next correct button it adds a LocalVarInt.

It's quite simple for me :)



Hmmm...
trying some stuff...
probly wrong.. never use those things what you do..
can you explane a bit more?

//Long list of things?????

Or is it more simple to build?

Would be awesome if you made a script for me... at least a beginning wat to do.

Those VAR works fine but it's just this...
when i also try to change the numbers like 707 whats the code now...
wont work...
so 4 digits would be awesome!
12-19-2012, 05:22 PM
Find
The chaser Offline
Posting Freak

Posts: 2,486
Threads: 76
Joined: Jun 2012
Reputation: 113
#7
RE: Need script help asap! extremely difficult script!

Well, it would be this:

///Let's get serious! There will be three buttons

void OnStart() //This can depend, if player can only be one time in the map and it cannot exit without doing the puzzle this is the function. For others, Enter.
{
///Interactions with areas
///I will do the things for the right buttons here
SetPlayerInteractCallback("Area_3", "Call", false);
SetPlayerInteractCallback("Area_2", "Call", false);
SetPlayerInteractCallback("Area", "Call", false);This will make the area always interactable
}

void Call (string &in asEntity)
{
AddLocalVarInt("Buttonsyay", 1); //Assuming this button is the correct one
Checkdabuttons();
}

void Checkdabuttons()
{
if (GetLocalVarInt("Buttonsyay", 3);
{
//Do the stuff you want done when good buttons are pressed
}
}

Of course, this is just the good combination. The bad combination would be a lot of "if"'s and "else"'s.

THE OTHERWORLD (WIP)
[Image: k6vbdhu]

Aculy iz dolan.
(This post was last modified: 12-19-2012, 06:04 PM by The chaser.)
12-19-2012, 06:03 PM
Find
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
DnALANGE Offline
Banned

Posts: 1,549
Threads: 73
Joined: Jan 2012
#9
RE: Need script help asap! extremely difficult script!

(12-19-2012, 07:00 PM)TheGreatCthulhu Wrote: 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;


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.

I can see the number here is 231..
now IF it is possible i would like to have an ENTER\accept to the finishing toutch...

Now there are 3 numbers 231
I aint SOOO good as you, i appoligize for that!

I am an OK scripter but thisn is i bit too much for me atm...

Is it possible if you add me on skype?
Or offcourse you mauy explane here as well.

My name is redmaster12345

I am veyr thankfull already but its a bit too much :O
(This post was last modified: 12-19-2012, 07:35 PM by DnALANGE.)
12-19-2012, 07:34 PM
Find
TheGreatCthulhu Offline
Member

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

(12-19-2012, 07:34 PM)dnalange Wrote: I am an OK scripter but thisn is i bit too much for me atm...
I know it kinda looks scary but, trust me, it's not that hard. Just try to understand it one step at the time. Hopefully, you'll then be able to transfer the code to the script for your level.

I'll try to help here as much as I can. But, can you write in more detail next time which bits you don't understand, so that people here can focus on that.

OK, here goes.

Fist, are you unfamiliar with these?
PHP Code: (Select All)
int secretPassCode 231;
int numPresses 0;     // the number of button presses (so far)
int userEntry 0

Those are just local variables. For example, writing
int secretPassCode = 231;

is really similar to writing
SetLocalVarInt("secretPassCode", 231);

except that it's easier to work with, and potentially somewhat faster. Also, Amnesia's functions allow you to set only 3 types of data (SetLocalVarInt(), SetLocalVarFloat(), SetLocalVarString()), but the script engine itself supports variables, and it supports more types than just those 3.

The format is:
type variable_name = initial_value;

For example:
int numPresses = 0;
int userEntry = 0;
string myName = "TheGreatCthulhu";
bool isAmnesiaAwesome = true;
float pi = 3.14f;


So, using variables now I can write things like:
if (userEntry == secretPassCode)

instead of:
if (GetLocalVarInt("userEntry") == GetLocalVarInt("secretPassCode"))

It's easier to read and understand.

You should be familiar with the OnEnter() part - it simply sets what function is to be called when the player clicks on each script area. I've set them all to the same function, the OnButtonPress() function.

Now, I know that on the wiki it says that the callback syntax for SetEntityPlayerInteractCallback() is:
void MyFunc(string &in asEntity)

but note that the name of the single parameter, asEntity, is actually irrelevant - the types have to match (it has to be a string), and I can name it whatever I like. So I named it 'sender' - to denote that it represents the id of the script area which "sent" the button press.

Next, inside the function, on the line
int digit = GetDigitFromButton(sender);

I declare a local int variable called digit, that will hold the current digit the user entered, and assign it the return value of the GetDigitFromButton() function. I hope you understand what's going on there. Forget for the moment how GetDigitFromButton() does what it does - you just need to know that it takes the name of the script area as input, and returns the corresponding digit as output. The number it returns then get's stored inside the digit variable. OK?

GetDigitFromButton() accomplishes this simply through use of if statements - I've seen you've used them before, so I suppose you understand what's going on inside the function.


I'm guessing that you're having trouble to understand this line, though:
userEntry += digit * Pow(10, 2 - numPresses);

For that, we have to go back to my "a b c thing", as you called it. What I was trying to explain is this: suppose the player presses the buttons "2", then "3", then "1", to enter the code 231.
How do you get from separate digits ("2", "3", and "1") to the code 231?

Well, take a look at this:
231 = 200 + 30 + 1

This can be written as:
231 = (2 * 100) + (3 * 10) + (1 * 1)

Now I don't know how good you're at math, but numbers 100, 10, and 1 (and such) are known as powers of 10. That simply means this:

1000 = 10^3 = 10 * 10 *10
100 = 10^2 = 10 * 10
10 = 10^1 = 10
1 = 10^0 ------> (that's what mathematicians decided)

You read, for example, 10^2, as "10 to the power of 2" - this simply means 10 * 10, ot 100 (note that the number of zeros is 2).

OK. So I made a function which can raise integer numbers to some power.
So, what we wrote here as 10^2, we can now write in script as
Pow(10, 2)

The function will return 100 in that case.
So, what then, Pow(10, 2 - numPresses) does?

Well, since it's a 3 digit code, for the 1st button press, you want it to return 100, for the 2nd press you want it to return 10, and for the third press you want it to return 1.
(Remember how we got to 231 from "2", "3" and "1"?)

Which means that you need this:
Pow(10, 2) // returns 100
Pow(10, 1) // returns 10
Pow(10, 0) // returns 1


This is why we count how many button presses there were since the start of the sequence. What are the corresponding values of numPresses variable? These:
0
1
2


For this reason, I can use this variable to get the powers of 10 I need; the values of
Pow(10, 2 - numPresses) will be:

Pow(10, 2 - 0) // 10^2
Pow(10, 2 - 1) // 10^1
Pow(10, 2 - 2) // 10^0


Then I simply multiply this value with the current digit:
digit * Pow(10, 2 - numPresses);

and add the result to the overall sum:
userEntry = userEntry + digit * Pow(10, 2 - numPresses);

Because, as you remember:
231 = 200 + 30 + 1


If you're confused by the AddDebugMessage() function calls - these lines are not important; you can safely delete them, and the script will still work. These just display some messages in the lover left corner of the screen, so that you can follow what's going on while you're ingame (if you've enabled debug messages).


(12-19-2012, 07:34 PM)dnalange Wrote: now IF it is possible i would like to have an ENTER\accept to the finishing toutch...

It is possible, but some tweaks need to be made. Basically if the pass code is always the same length (same number of digits), you can simply add another interact callback function for the enter "button", and move all of the pass code checking code from OnButtonPress() to that other function.

That would be all the code inside the if (numPresses >= 3) part (just copy everything between { and }), except that now there's really no need to check if all of the buttons were pressed, since pressing "enter" is basically the Player saying "I'm done".

In the end, if you still have some trouble understanding how this all works - that's ok; you can copy and make use of the helper functions Pow() and GetDigitFromButton() to your code (also make sure to copy the INVALID_VALUE const at the top), and they should work - but, maybe you'd have to make some modifications to match the names and number of your script areas.

Any other parts you don't understand or need help with?
(This post was last modified: 12-19-2012, 09:14 PM by TheGreatCthulhu.)
12-19-2012, 09:09 PM
Find




Users browsing this thread: 5 Guest(s)