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
Help with script for "3 levers to unlock a door"
Mackiiboy Offline
Member

Posts: 101
Threads: 7
Joined: Jan 2012
Reputation: 11
#1
Help with script for "3 levers to unlock a door"

I have a problem with my script. I'm trying to use a script there 3 levers are needed to be pulled down to unlock a door. When I'm using one of the levers, the door unlocks before I've pulled down the two other levers.
I have no idea what's wrong with my script. If you can find what's wrong, please make a comment [Image: shy.gif].



void OnStart()
{
//-- Leversfunction
SetLocalVarInt("Var_sum1", 0);
SetEntityConnectionStateChangeCallback("lever1", "func_lever1");
SetEntityConnectionStateChangeCallback("lever2", "func_lever2");
SetEntityConnectionStateChangeCallback("lever3", "func_lever3");
}



// -- Levers - open door

void func_lever1(string &in asEntity, int alState)
{
if (alState == 1)
{
AddLocalVarInt("Var_sum1", 1);
Func_open_gate();
}
}

void func_lever2(string &in asEntity, int alState)
{
if (alState == 1)
{
AddLocalVarInt("Var_sum1", 1);
Func_open_gate();
}
}

void func_lever3(string &in asEntity, int alState)
{
if (alState == 1)
{
AddLocalVarInt("Var_sum1", 1);
Func_open_gate();
}
}

void Func_open_gate()
{
if(GetLocalVarInt("Var_sum1") == 3)
{
SetSwingDoorLocked("prison_cellardoor", false, true);
PlaySoundAtEntity("", "unlock_door", "prison_cellardoor", 0, false);
GiveSanityBoostSmall();
}
}
(This post was last modified: 01-30-2012, 08:06 AM by Mackiiboy.)
01-29-2012, 03:28 PM
Website Find
FlaW Offline
Junior Member

Posts: 48
Threads: 9
Joined: Nov 2011
Reputation: 0
#2
RE: Help with script for "3 levers to unlock a door"

Try this:
{
SetLocalVarInt("Var1", 0);


SetEntityConnectionStateChangeCallback("lever1", "func_lever_1");
SetEntityConnectionStateChangeCallback("lever2", "func_lever_2");
SetEntityConnectionStateChangeCallback("lever3", "func_lever_3");
}


void func_lever_1(string &in asEntity, int alState)
{
if (alState == 1)
{
AddLocalVarInt("Var1", 1);
func01();
SetLeverStuckState("lever1", 1, true);
}
}

void func_lever_2(string &in asEntity, int alState)
{
if (alState == 1)
{
AddLocalVarInt("Var1", 1);
func01();
SetLeverStuckState("lever2", 1, true);
}
}

void func_lever_3(string &in asEntity, int alState)
{
if (alState == 1)
{
AddLocalVarInt("Var1", 1);
func01();
SetLeverStuckState("lever3", 1, true);
}

void func01()
{
if(GetLocalVarInt("Var1") == 3)
{
your functions
}
}
(This post was last modified: 01-29-2012, 03:38 PM by FlaW.)
01-29-2012, 03:37 PM
Find
palistov Offline
Posting Freak

Posts: 1,208
Threads: 67
Joined: Mar 2011
Reputation: 57
#3
RE: Help with script for "3 levers to unlock a door"

Hey hey, looks like a script guru in the making. You've got the right idea here, but you'll want to compress this script. Try something like this

PHP Code: (Select All)
void OnStart()
{
    
//here we'll add the lever callbacks
    
for(int i=1;i<=3;i++) SetEntityConnectionStateChange("lever"+i"LeverPull");
}

void LeverPull(string &in leverNameint leverState)
{
    
// if lever is at correct state, set it stuck and add 1 to lever var
    
if(leverState == 1
    {
        
AddLocalVarInt("leverStates"1);
        
SetLeverStuckState(leverName1true);
    }
    
// solve the puzzle if all states are 1
    
if(GetLocalVarInt("leverStates") == 3func01();
}

void func01()
{
    
//puzzle solved! hoorah!
    
SetSwingDoorLocked("prison_cellardoor"falsetrue);
    
PlaySoundAtEntity("""unlock_door""prison_cellardoor"0false);
    
GiveSanityBoostSmall();


This is a cleaner way to do the code. Instead of having a separate function for each lever pulled, I combined them into one. There's no real need to track exactly which lever was pulled, so the script becomes even shorter, since the what we're focusing on is the lever changing states, not on which lever changes states.

Things I changed:
Compressed the call stack which adds the connection state callbacks.
Compressed the lever pull functions into a single function. Also changed the name to "LeverPull".
The local variable name.

Hope it works for ya. I had to put down my apple to write this....now I'm gonna pick it up and devour the rest of it.

(This post was last modified: 01-29-2012, 05:21 PM by palistov.)
01-29-2012, 05:18 PM
Find
Mackiiboy Offline
Member

Posts: 101
Threads: 7
Joined: Jan 2012
Reputation: 11
#4
RE: Help with script for "3 levers to unlock a door"

Thanks alot for the help "FlaW" and "palistov"! Now I do understand what I've made wrong. And special thanks for the tip how to compress the codes and make it more clean Shy .
01-29-2012, 11:40 PM
Website Find




Users browsing this thread: 1 Guest(s)