"On key pressed" issues

This forum is currently in read-only mode.
From the Asset Store
Random Maze Generator with Door & Key System - tutorial capx
  • Hey guys - been working hard on a couple of games over the past few months, but I still have yet to find a solution to this issue I've been having.

    It regards the keyboard and mouse object. In this particular game, I'm making a basic menu system where picking certain options leads to different menus and such. What I WANT to do is make it so when a certain global variable "Menu" is set to 0, it's on one menu, and if it's set to 1 or something, then it's set to the next menu.

    Pressing "Z" is what sets "Menu" to a different value. However, it seems like Construct registers a single Z press as a Z press at each different value of the variable.

    For instance, let's say if "Menu" is 0 pressing Z sets it to 1, and if "Menu" is 1 pressing Z sets it to 2. The problem: Starting at 0, pressing Z once will set this to 2.

    This seems like a pretty glaring problem, so I'm sure I must just be doing something wrong. Either way it'd be a huge help if somebody knows a way around this.

  • This is simply a problem with your logic. Pressing a key will trigger all keypress events during that frame. It's necessary to have some understanding of the underlying engine if you want to truly understand what you're doing. For example, the event sheet runs top to bottom, executing code. If you place the event where it gets sets from 1 to 2 before the event where it sets from 0 to 1, then it won't get set to 1, check if it's 1, then set it to 2. It will check if it's 1 (it's not), skip that event, then set it from 0 to 1, and the frame will end. Then, next time you press it ( on a different frame), it will set it from 1 to 2. However, if you want it to loop back to 0 (like, 0,1,2,0), then the problem still exists, and can't be fixed by code order. The true solution to this problem is to create a new variable (call it "lock"), and set it to one every time you change your counter. Then, only allow the counter to change if the lock hasn't been set by another event in the same frame. (on keypress AND if lock equals 0). Then at the end of the code, set lock back to zero to prepare it for the next frame. With this solution, only one event can ever fire in a frame, and the variables wont cascade from a single press. But here's an even better way, don't use two events at all! Simply set the value to: (value+1)%(maxvalue+1) , where max value is the highest number you want the counter to reach. This makes it so every press increments the value, and then it returns to 0. (0,1,2,3,4,...,maxvalue,0,1,2,3 etc)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • That was very informative, thanks man!

    I actually solved it a different way, though your way would probably be better if I used Z for more things. Instead of having separate Z press events, I put every event that needs one as a sub-event of "On Z pressed". That way Construct only picks one at a time.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)