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
Button sequence?
flamez3 Offline
Posting Freak

Posts: 1,281
Threads: 48
Joined: Apr 2011
Reputation: 57
#1
Button sequence?

I've looked at the campaigns puzzle sequence (with the levers and roman numbers). Tried it my way, didn't work. I was wondering if there was a way for a 6 button sequence? Like If they press it in the right order, then something happens?

11-02-2011, 07:08 AM
Find
Apjjm Offline
Is easy to say

Posts: 496
Threads: 18
Joined: Apr 2011
Reputation: 52
#2
RE: Button sequence?

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.
(This post was last modified: 11-02-2011, 12:56 PM by Apjjm.)
11-02-2011, 12:54 PM
Find
flamez3 Offline
Posting Freak

Posts: 1,281
Threads: 48
Joined: Apr 2011
Reputation: 57
#3
RE: Button sequence?

(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 Smile

11-02-2011, 02:03 PM
Find




Users browsing this thread: 1 Guest(s)