digitalmars.D.learn - What are these functions called and how to implement they?
I saw these functions in some projects. For example: in Dagon (https://gecko0307.github.io/dagon/) there are functions like onKeyDown. This function doesn't need to call - it checks pressed keys every time. Or Update in Unity (game engine). It doesn't need to call, but it executes itself every frame. Okay, maybe it sounds confusing. I just want to understand how to implement functions that wait for events and execute code inside the function. Something like this: void onKeyPressed(int key) { //the code to be executed if a key has been pressed }
Jan 28 2021
On Thursday, 28 January 2021 at 17:10:57 UTC, dog2002 wrote:I saw these functions in some projects. For example: in Dagon (https://gecko0307.github.io/dagon/) there are functions like onKeyDown. This function doesn't need to call - it checks pressed keys every time. Or Update in Unity (game engine). It doesn't need to call, but it executes itself every frame. Okay, maybe it sounds confusing. I just want to understand how to implement functions that wait for events and execute code inside the function. Something like this: void onKeyPressed(int key) { //the code to be executed if a key has been pressed }Not 100% sure what you mean but I guess you ask how to implement a handler? If an event occurs, a routine decides to call your onKeyPressed function and pass the keyCode which was pressed. The routine must be registered on a event source that will emit the events. The routine is basically just a callback and that is indeed called every frame again if an event occurs.
Jan 28 2021
On Thursday, 28 January 2021 at 18:27:09 UTC, frame wrote:Not 100% sure what you mean but I guess you ask how to implement a handler? If an event occurs, a routine decides to call your onKeyPressed function and pass the keyCode which was pressed. The routine must be registered on a event source that will emit the events. The routine is basically just a callback and that is indeed called every frame again if an event occurs.A very simple example: bool myEventA = true; bool myEventB = false; // event source that generates the event (must be called to run) void source() { observe(myEventA); } // routine that decides what handler to call void observe(bool event) { switch (event) { case true: onMyEventA(event); break; case false: onMyEventB(event); break; default: assert(0); } } // handler void onMyEventA(bool event) { // do something } void onMyEventB(bool event) { // do something }
Jan 28 2021
On Thursday, 28 January 2021 at 18:56:15 UTC, frame wrote:On Thursday, 28 January 2021 at 18:27:09 UTC, frame wrote:Thank you, I'll try it[...]A very simple example: bool myEventA = true; bool myEventB = false; // event source that generates the event (must be called to run) void source() { observe(myEventA); } // routine that decides what handler to call void observe(bool event) { switch (event) { case true: onMyEventA(event); break; case false: onMyEventB(event); break; default: assert(0); } } // handler void onMyEventA(bool event) { // do something } void onMyEventB(bool event) { // do something }
Jan 28 2021