Refactor API callbacks and improve example scripts

Moved register_on_update_callback in api.py for better code organization. Fixed initialization of units_to_delete and corrected simulate_fire_fight usage in example_disembarked_infantry.py. In example_voice_control.py, added cleanup of generated audio files and fixed callback parameter naming for clarity.
This commit is contained in:
Pax1601
2025-08-08 13:14:59 +02:00
parent 5fa1a26843
commit 4e6701ff01
3 changed files with 21 additions and 17 deletions

View File

@@ -157,17 +157,7 @@ class API:
except Exception as e:
# Log the error but don't crash the update process
self.logger.error(f"Error in callback: {e}")
def register_on_update_callback(self, callback):
"""
Register a callback function to be called on each update.
Args:
callback (function): The function to call on update. Can be sync or async.
The function should accept a single argument, which is the API instance.
"""
self.on_update_callback = callback
async def _run_async(self):
"""
Async implementation of the API service loop.
@@ -203,6 +193,16 @@ class API:
finally:
self.logger.info("API stopped")
self.running = False
def register_on_update_callback(self, callback):
"""
Register a callback function to be called on each update.
Args:
callback (function): The function to call on update. Can be sync or async.
The function should accept a single argument, which is the API instance.
"""
self.on_update_callback = callback
def unregister_on_update_callback(self):
"""