Is it okay to call functions when event occurs, or after that?

So I was not sure how to structure this title, feel free to edit it.

My question is:
When I have a Game Loop, I have 3 main blocks. Handle Events, Update game state, and draw.
Let’s say for example I check for keyboard/mouse events, and I want to move my character when buttons are pressed.
Whenever KEYDOWN event occurs, should I immediately call Player.move?
Or should I toggle move_left/move_right bools, and then call Player.move when I’m in the Update game state block.

I was wondering this becouse, for example a function performs a long algorithm and I call it when an event occur’s, calling the function will take some time, and meanwhile I might press more button’s/events. I think I will lose some of those events becouse in that time the algirthm function was runing.

Or, I can toggle a bool when the event occurs, and after checking for ALL events, I perform functions based on the bools.

PS: Feel free to edit this post in a more constructive question.

Well, simply put:

  • if you go with the “toggle booleans” way you risk “losing” user inputs. The user will sometimes click 3 times in order for his click to be “acknowledged” by such an input handling mechanism. For something like a fighting games which supports combos and such the effect would be very pronounced.

  • if you do something where the input event handler alters your game state directly (ignoring the game loop) is bad for several reasons: your code will no longer be nicely separated; even though technically there’s no user input event that is skipped it mays STILL look like that to the human player as some input state changes can happen so fast the graphics rendering doesn’t take them into account (imagine a red-blue, fast-blinking light, which changes color when the player pushes a button)

  • using an input queue is probablly the best way to go. Something like:

    function processInput() {
       while(!inputEventsQueue.isEmpty()) {
            //you may decide to cap the maximum number of events delt with in one step
            InputEvent e = inputEventsQueue.pop();
            for(Entity e:gameEntities) {
                e.processInput(e)
            }
       }
    }
    

    //game loop
    while(…) {
    processInput();
    updateState();
    updateGraphics();
    }

You must not skip valid user inputs. If your game function is supposed to respond to a single input, then you have no choice but responding to that input immediately. You can queue user inputs and play them in sequence as soon as you can. This would work when you are playing a single player game. Another option is to lock the input device (keyboard, etc.) until the function is processed after indicating that the system is processing the previous input. In all cases, try to optimize the long running function. Chances are with today’s computers, you can do that with some work.

It depends on the type of game you are making.

For an FPS you want to respond to what the user is doing right this second. In such a case yes I would process all events and determine what the user is doing right now. Then do that in the update function.

For an RTS the meaning of current input depends on previous input. Skipping to only track the last action would royally screw stuff up. i.e. Select unit 1, attack unit 3, select unit 2, attack unit 4, drop the select unit 2 because it wasn’t the last action on the event queue, now unit 1 is attacking unit 4, unit 2 isn’t doing anything, unit 3 is still alive and not engaged.

For your game what would dropping input mean? It could mean your game stays responsive even if the frame rate stutters. It could mean you screw up input state because one event depends on an earlier event in the stream.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật