(11-02-2011, 12:54 PM)Apjjm Wrote: You would simply use a variable to track the "expected" button press. Below is some sample code i have thrown together - it is untested and will need configuring to your exact problem but should show the approach i would take:
//Array of each of our button elements [NB: This button calls stuff buttons - this applies to levers etc too!]
string[] buttons = {"Button_1","Button_2","Button_3"}; //Feel free to add more!!!
void OnStart()
{
//Add a callback for each of our buttons
for(int i = 0; i<buttons.length(); i++)="" setentityconnectionstatechangecallback(buttons[i],"cbbuttonpressed");
}
//This is called when the player depresses a button
void cbButtonPressed(string &in asEntity, int alState)
{
//We are only interested in the events that occur when the button is pressed (Not released)
if(alState != 1) return;
//Determine which of the buttons has triggered this callback. -1 If none of the buttons matched
int buttonIndex = -1;
for(int i =0; i<buttons.length(); i++)
{
//For each button, If this button is the button that was pressed, store the index of this button in the buttons array.
if(buttons[i] = asEntity) buttonIndex = i;
}
//We should only continue the puzzle script if the button was actually found in the buttons array!
if(buttonIndex < 0) return;
//Determine if this button is the button that was supposed to be pressed
if(buttonIndex == GetLocalVarInt("buttonState"))
{
//It was the button that was supposed to be pressed. Move to the next "state" / target button
AddLocalVarInt("buttonState",1);
//Is this the final state? If so, do some action
if(GetLocalVarInt("buttonState") == buttons.length()) onButtonPuzzleSolve()
}
}
//We call this when we determine the button puzzle has been solved
void onButtonPuzzleSolve()
{
AddDebugMessage("You solved the puzzle!",false);
}
I Should add that the code should extend to an arbitrary combination & number of buttons and levers. You just need to place them in the array, and configure the onButtonPuzzleSolve event. If you want more than one puzzle in the map you will obviously have to tweak variable names etc to prevent collisions.
Thanks for the script, but I just did it with local variables and 5 levers. Thanks for that, it could actually come in handy