How do I disable an in-game button when it's pressed?

Just started using Construct 2? Post your questions here

Post » Sat Apr 04, 2015 7:55 pm

I've put a big red button on my local multiplayer game. If one of the two players steps on it it starts the lava which begins to fill up the stage. If the player steps on the button again, the lava stops flowing.

To achieve this I created a global variable called "StartLava" and an event which says:
Player1 or Player2 - On collision with Button = if StartLava is set to 0 (off), set StartLava to 1 (on)
If Else, set StartLava to 0


The problem is that the button is big enough to allow two players on it, and I don't want the lava to start or stop if a player steps on a button while there's already someone else on the button.

I've tried adding events like setting the StartLava value to itself if a player collides on the button while the other is already on it, but they don't work, because the event described above always applies first, activating or deactivating the lava BEFORE checking if the button is already "occupied" or not.


Thanks in advance, and sorry if this is a silly question.
B
8
S
1
Posts: 20
Reputation: 490

Post » Sat Apr 04, 2015 8:29 pm

I think you need one more layer of logic. Let's replicate the mechanics of a button. Think of your button as having two states: 'down' and 'up'. When a player is overlapping your button, you want the button to be 'down'. When a player is NOT overlapping your button, you want the button to be 'up'. Let's put these mechanics in event form: add a Boolean instance variable to your button called "isDown". When Player1 or Player2 is overlapping Button, set isDown to True. If neither is overlapping, set isDown to False.

Now we have a button that is either in a down state or up state (behind the scenes), depending on whether a player is standing on it. You want to link up your StartLava events to the state of your button. Basically, when the button is pushed (i.e isDown first becomes True), we want to toggle StartLava (set StartLava to 1-StartLava). To do this, we can use the "Trigger Once" condition. Our logic will look something like this: Button.isDown is True and Trigger Once, set StartLava to 1-StartLava.

Now, every time isDown changes to True from False, StartLava will toggle. If a player steps on the button (changing isDown to True), and another player steps on it later (keeping isDown as True), because there was no change in button state, StartLava will remain the same, as you would like it to.

EDIT: Or a simpler but conceptually similar approach would be: When either Player 1 or Player 2 is overlapping the button, in a Trigger Once SUBEVENT, toggle your StartLava variable.

This should work similarly.
Image
B
10
S
3
G
2
Posts: 196
Reputation: 2,053

Post » Sun Apr 05, 2015 9:34 am

Dalal wrote:I think you need one more layer of logic. Let's replicate the mechanics of a button. Think of your button as having two states: 'down' and 'up'. When a player is overlapping your button, you want the button to be 'down'. When a player is NOT overlapping your button, you want the button to be 'up'. Let's put these mechanics in event form: add a Boolean instance variable to your button called "isDown". When Player1 or Player2 is overlapping Button, set isDown to True. If neither is overlapping, set isDown to False.

Now we have a button that is either in a down state or up state (behind the scenes), depending on whether a player is standing on it. You want to link up your StartLava events to the state of your button. Basically, when the button is pushed (i.e isDown first becomes True), we want to toggle StartLava (set StartLava to 1-StartLava). To do this, we can use the "Trigger Once" condition. Our logic will look something like this: Button.isDown is True and Trigger Once, set StartLava to 1-StartLava.

Now, every time isDown changes to True from False, StartLava will toggle. If a player steps on the button (changing isDown to True), and another player steps on it later (keeping isDown as True), because there was no change in button state, StartLava will remain the same, as you would like it to.

EDIT: Or a simpler but conceptually similar approach would be: When either Player 1 or Player 2 is overlapping the button, in a Trigger Once SUBEVENT, toggle your StartLava variable.

This should work similarly.


Wow man you're amazing, not only this worked perfectly, but it's also far easier than all the solutions I tried before!

Like you suggested, I set a boolean to the button that checks if it's currently pressed or not. This solved the two players problem, since if player2 steps on the button while player1 is already on it, the button remains "IsDown=True" regardless.
After this, I've added an event which says:
Button IsDown + Trigger Once= Set StartLava to 1-Startlava

And bam, it now works perfectly.

Thank you a lot!

P.S. The "Set StartLava to 1-Startlava" you told me to use is amazingly smart and far better than the 2 events I used before ("if Startlava=0 > set StartLava to 1" + "if Else > Set StartLava to 0"). I still have to get in the correct mindset to use this program...
B
8
S
1
Posts: 20
Reputation: 490

Post » Sun Apr 05, 2015 9:57 pm

Glad to know I was able to help! And yeah, new tips/tricks are always fun to learn. Programming on the whole is a vast frontier, with new things to discover around every corner. Good luck on your game!
Image
B
10
S
3
G
2
Posts: 196
Reputation: 2,053


Return to Beginner's Questions

Who is online

Users browsing this forum: Lordshiva1948 and 1 guest