I’m struggling a bit with storing pointers to method functions.
I have a bunch of different struct types that have a method called run(self: *@This(), game: *State)
. I’d like to store a list of such function to be called in a for-loop.
For example, here’s what I have now:
spawnBullets(game);
state.collisionResponse.run(game);
entityAging(game);
state.turretAI.run(game);
state.spaceshipAI.run(game);
state.bombshipAI.run(game);
I’d like to store each of those invocations into an array and then run all of them in a for-loop:
for (systems) |s| {
s.run(game);
}
My motivation is that I can give them names, measure each run
calls execution time, and so on.
But I’m struggling with casting method function pointers. This is what I have now:
pub const System = struct {
ptr: *anyopaque,
runFn: *const fn (ptr: *anyopaque, game: *State) void,
pub fn run(self: *@This(), game: *State) void {
self.runFn(self.ptr, game);
}
};
fn foo() {
var f = System{
.ptr = @ptrCast(*anyopaque, &state.movement),
.runFn = @ptrCast(*const fn (ptr: *anyopaque, game: *State) void, &systems.MovementSystem.run),
};
// pointer stored, now I can run it!
f.run(game);
}
It works but it’s a lot of casting. I wonder if there’s a cleaner way?
I also thought that I don’t need to use the &-operator to obtain a pointer to a function, but that was the only way I could get &systems.MovementSystem.run
to compile above.