Amerio.Stephane Posted June 6, 2025 Posted June 6, 2025 Hello, It looks like Console::run() has some delayed effect. We have a state machine, that, on each state 'enter', will read a file with console commands and will execute them.; these commands are used to easily configure each state. But the result of calling these commands through Console::run() is not immediate and appear to be effective only in the next frame. Is it the intended purpose? Is there anyway to have them called immediately? (or to call the Callback attached to the command manually?) EDIT: I'm starting to think this issue is caused because we call Console::run() from another console command, itself called with Console::run(). Could that be the cause?
bmyagkov Posted June 9, 2025 Posted June 9, 2025 Hello! On 6/6/2025 at 7:08 PM, Amerio.Stephane said: Hello, It looks like Console::run() has some delayed effect. We have a state machine, that, on each state 'enter', will read a file with console commands and will execute them.; these commands are used to easily configure each state. But the result of calling these commands through Console::run() is not immediate and appear to be effective only in the next frame. Is it the intended purpose? Is there anyway to have them called immediately? (or to call the Callback attached to the command manually?) EDIT: I'm starting to think this issue is caused because we call Console::run() from another console command, itself called with Console::run(). Could that be the cause? The behavior you’re seeing is quite intended. When you call Console::run() the command isn’t executed immediately — it’s added to an internal queue. This queue is flushed (executed) at the beginning of the next frame via Console::flush(). This mechanism is designed to ensure safety and consistency across the engine’s execution phases. If you need to force immediate execution of console commands (for instance in testing or special use cases) you can explicitly call Console::flush() right after Console::run() as demonstrated in this minimal example: static int frame_index; int AppWorldLogic::init() { Console::addCommand("enter", "", MakeCallback([](int argc, char** argv){ int new_frame_index = Engine::get()->getFrame(); if (frame_index == new_frame_index) Log::warning("Callback executed at the same frame\n"); else Log::warning("Callback executed at different frame\n"); })); return 1; } int AppWorldLogic::update() { if (Input::isKeyDown(Input::KEY_R)) { frame_index = Engine::get()->getFrame(); Console::run("enter"); Console::flush(); } // Just Execute and flush in the next frame if (Input::isKeyDown(Input::KEY_T)) { frame_index = Engine::get()->getFrame(); Console::run("enter"); } return 1; } Pressing R: The command is flushed immediately, so the callback runs in the same frame — the console will log "Callback executed at the SAME frame". Pressing T: The command is not flushed manually, so it's deferred to the next frame — logging "Callback executed at a DIFFERENT frame". Would that be helpful for your setup? We are happy to explore other options if needed. Thanks!
Amerio.Stephane Posted June 9, 2025 Author Posted June 9, 2025 (edited) That's perfect for my use case! In our setup Console::run() is called in the enter() step of each of our state machine's state, which is itself called in the update() of the WorldLogic. In this specific case, I think it's safe to execute run() at the point of call. But I can see how it would be unsafe to execute it immediately from another point of execution, like in asyncUpdate() and why you made this deferred execution choice. Thanks again! Edited June 9, 2025 by Amerio.Stephane 1
bmyagkov Posted June 9, 2025 Posted June 9, 2025 16 minutes ago, Amerio.Stephane said: That's perfect for my use case! In our setup Console::run() is called in the enter() step of each of our state machine's state, which is itself called in the update() of the WorldLogic. In this specific case, I think it's safe to execute run() at the point of call. But I can see how it would be unsafe to execute it immediately from another point of execution, like in asyncUpdate() and why you made this deferred execution choice. Thanks again! You're welcome!
Recommended Posts