Add get_game_tick_counter() (#852)

Coop now maintains a counter that increments at the start of each game tick,
And another counter that increments at the start of each frame render.

This is to be able to identify specific frames regardless of mod load, hook,
and execution order.

---------

Co-authored-by: MysterD <myster@d>
This commit is contained in:
djoslin0 2025-06-15 00:26:53 -07:00 committed by GitHub
parent 24b92ecc2a
commit f42e4ad55b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 55 additions and 0 deletions

View file

@ -11104,6 +11104,12 @@ function texture_to_lua_table(tex)
-- ... -- ...
end end
--- @return integer
--- Gets the total number of game ticks since the game was launched. Wraps around at U64_MAX.
function get_game_tick_counter()
-- ...
end
--- @param name string --- @param name string
--- @return ModelExtendedId --- @return ModelExtendedId
--- Gets the extended model ID for the `name` of a `GeoLayout` --- Gets the extended model ID for the `name` of a `GeoLayout`

View file

@ -5128,6 +5128,27 @@ Converts a texture's pixels to a Lua table. Returns nil if failed. Otherwise, re
<br /> <br />
## [get_game_tick_counter](#get_game_tick_counter)
### Description
Gets the total number of game ticks since the game was launched. Wraps around at U64_MAX.
### Lua Example
`local integerValue = get_game_tick_counter()`
### Parameters
- None
### Returns
- `integer`
### C Prototype
`u64 get_game_tick_counter(void);`
[:arrow_up_small:](#)
<br />
--- ---
# functions from smlua_model_utils.h # functions from smlua_model_utils.h

View file

@ -1991,6 +1991,7 @@
- [geo_get_current_camera](functions-6.md#geo_get_current_camera) - [geo_get_current_camera](functions-6.md#geo_get_current_camera)
- [geo_get_current_held_object](functions-6.md#geo_get_current_held_object) - [geo_get_current_held_object](functions-6.md#geo_get_current_held_object)
- [texture_to_lua_table](functions-6.md#texture_to_lua_table) - [texture_to_lua_table](functions-6.md#texture_to_lua_table)
- [get_game_tick_counter](functions-6.md#get_game_tick_counter)
<br /> <br />

View file

@ -63,6 +63,7 @@ struct Controller *gPlayer3Controller = &gControllers[2];
struct DemoInput *gCurrDemoInput = NULL; // demo input sequence struct DemoInput *gCurrDemoInput = NULL; // demo input sequence
u16 gDemoInputListID = 0; u16 gDemoInputListID = 0;
struct DemoInput gRecordedDemoInput = { 0 }; // possibly removed in EU. TODO: Check struct DemoInput gRecordedDemoInput = { 0 }; // possibly removed in EU. TODO: Check
u64 gGameTickCounter = 0;
/** /**
* Initializes the Reality Display Processor (RDP). * Initializes the Reality Display Processor (RDP).
@ -602,6 +603,8 @@ void thread5_game_loop(UNUSED void *arg) {
void game_loop_one_iteration(void) { void game_loop_one_iteration(void) {
profiler_log_thread5_time(THREAD5_START); profiler_log_thread5_time(THREAD5_START);
gGameTickCounter++;
// if any controllers are plugged in, start read the data for when // if any controllers are plugged in, start read the data for when
// read_controller_inputs is called later. // read_controller_inputs is called later.
if (gControllerBits) { if (gControllerBits) {

View file

@ -58,6 +58,7 @@ extern u8 gDemoInputs[];
extern u16 frameBufferIndex; extern u16 frameBufferIndex;
extern u32 gGlobalTimer; extern u32 gGlobalTimer;
extern u64 gGameTickCounter;
void setup_game_memory(void); void setup_game_memory(void);
void thread5_game_loop(UNUSED void *arg); void thread5_game_loop(UNUSED void *arg);

View file

@ -33296,6 +33296,21 @@ int smlua_func_texture_to_lua_table(lua_State* L) {
return 1; return 1;
} }
int smlua_func_get_game_tick_counter(UNUSED lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 0) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "get_game_tick_counter", 0, top);
return 0;
}
lua_pushinteger(L, get_game_tick_counter());
return 1;
}
///////////////////////// /////////////////////////
// smlua_model_utils.h // // smlua_model_utils.h //
///////////////////////// /////////////////////////
@ -37179,6 +37194,7 @@ void smlua_bind_functions_autogen(void) {
smlua_bind_function(L, "geo_get_current_camera", smlua_func_geo_get_current_camera); smlua_bind_function(L, "geo_get_current_camera", smlua_func_geo_get_current_camera);
smlua_bind_function(L, "geo_get_current_held_object", smlua_func_geo_get_current_held_object); smlua_bind_function(L, "geo_get_current_held_object", smlua_func_geo_get_current_held_object);
smlua_bind_function(L, "texture_to_lua_table", smlua_func_texture_to_lua_table); smlua_bind_function(L, "texture_to_lua_table", smlua_func_texture_to_lua_table);
smlua_bind_function(L, "get_game_tick_counter", smlua_func_get_game_tick_counter);
// smlua_model_utils.h // smlua_model_utils.h
smlua_bind_function(L, "smlua_model_util_get_id", smlua_func_smlua_model_util_get_id); smlua_bind_function(L, "smlua_model_util_get_id", smlua_func_smlua_model_util_get_id);

View file

@ -625,3 +625,7 @@ void texture_to_lua_table(const u8 *tex) {
lua_rawseti(L, -2, i / bytesPerPixel + 1); lua_rawseti(L, -2, i / bytesPerPixel + 1);
} }
} }
u64 get_game_tick_counter(void) {
return gGameTickCounter;
}

View file

@ -234,4 +234,7 @@ struct GraphNodeHeldObject* geo_get_current_held_object(void);
/* |description|Converts a texture's pixels to a Lua table. Returns nil if failed. Otherwise, returns a table as a pure memory buffer. Supports rgba16 and rgba32 textures.|descriptionEnd|*/ /* |description|Converts a texture's pixels to a Lua table. Returns nil if failed. Otherwise, returns a table as a pure memory buffer. Supports rgba16 and rgba32 textures.|descriptionEnd|*/
void texture_to_lua_table(const u8 *tex); void texture_to_lua_table(const u8 *tex);
/* |description|Gets the total number of game ticks since the game was launched. Wraps around at U64_MAX.|descriptionEnd|*/
u64 get_game_tick_counter(void);
#endif #endif