Best practices for events running at the same time or triggering eachother (Mouse and UI)

0 favourites
  • 2 posts
From the Asset Store
Best car suspension with spring effect and very cool terrain generation.
  • Hi, I dont have a specific problem. Instead, I was noticing that I had to use a bunch of workarounds recently dealing with the mouse object and Im suspecting that Im following some bad programming practices. Hopefully someone can help me improve.

    Im working on a tower defence game, specifically on the UI for building and upgrading towers. My problem is that Im running a lot into situations where the order of events matters or events are running at the same time, sabotaging each other. For example:

    Event 1: Triggers on mouse click, opens a menu and should only trigger when the game does not display a menu (e.g. some variable "menu"=false)

    Event 2: Also triggers on mouse click, does something in the menu and should only trigger when the menu is open

    So, in this case, if the menu is closed and the player clicks: Event 1 opens the menu and sets the Menu variable to true, but then the conditions for Event 2 are immediately fulfilled and Event 2 is triggered at the same time / by the same click.

    I know a bunch of ways to avoid this problem like "wait for previous actions to complete", having a timer, a wait, using a intermediate state for the menu variable and probably some more. But what is the general best practice for this? My solutions always seem kind of clunky to me.

    Thank you very much in advance!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • When using a variable like "menuIsOpen", or checking if the menu layer is visible, the important thing is to change this variable (or layer visibility) after a small delay. For example:

    Mouse On MenuCloseButton clicked 
    Layer "menu" is visible
    ... Wait 0
    ... Set layer "menu" invisible

    There is a new cool feature in Construct - interactive layers. When layer is not interactive, objects on it will not receive click events. But you still need to be careful not to enable layer interactivity setting in the same tick with the mouse click, if you don't want the same click to go through to that layer.

    Mouse On MenuCloseButton clicked 
    ... Wait 0
    ... Set layer "game" interactive True
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)