Frictional Games Forum (read-only)

Full Version: Help with script for "3 levers to unlock a door"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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();
}
}
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
}
}
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:
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.
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 .