I thought I was getting it, too. So much for that.
OK, I have a Sprite structure, its init takes a SpritePt, which represents a fixed f32 or a Tween, that’s all good and dandy.
I have a struct representing the ‘Title screen’, it has init(), step() and draw() for Raylib to make it move. My issue is const regarding the Sprite, defined as a file container i.e. not a ‘struct’ but then imported as and where need with @import.
Now, the ‘level’ CONTAINS the Sprite, and a few more fields:
game_state: *g.Gamestate,
font: r.Font,
mp3: r.Music,
sprites: Spritesheet,
star_map: Starmap,
sprite1: Sprite,
The init() creates the sprite, given the base Sprite sheet, which just wraps the Raylib texture for now, nothing complex for now.
So, I have a struct that has a Sprite in it, that I created in the init():
pub fn init(game_state: *g.Gamestate, font: r.Font, mp3: r.Music, star_map: Starmap, sprite_sheet: Spritesheet) !Level_00_Title {
const sp1 = try Sprite.init(
Spritesheet.Type.redShip1,
150.0,
250.0,
sprite_sheet,
);
return Level_00_Title{
.game_state = game_state,
.font = font,
.mp3 = mp3,
.sprites = sprite_sheet,
.star_map = star_map,
.sprite1 = sp1,
};
}
And when I come to step update the sprite I get this error:
src/level_00_title.zig:79:17: error: expected type '*sprite', found '*const sprite'
self.sprite1.step(dT);
Where the hell did *const
come from, that is not how I declared it?!
79: self.sprite1.step(dT);
The step method has *Sprite to get the free level of dereference too:
pub fn step(self: *Sprite, dT: f32) void {
}
For reference the level step() is:
pub fn step(self: *Level_00_Title, dT: f32) void {
r.ClearBackground(r.BLACK);
r.UpdateMusicStream(self.mp3);
self.star_map.draw();
self.star_map.step(dT);
self.sprite1.step(dT);
self.sprite1.draw();
if (r.IsKeyReleased(r.KEY_SPACE)) {
r.StopMusicStream(self.mp3);
self.game_state.* = .level_1;
}
}
Argh. It’s driving me nuts. I have music playing, text, a three coloured vertical scrolling star-field and yet this f* Sprite is getting up my conk I can tell you. It’s obv. I have not yet understood ‘const’ ness WRT to Zig so, I have read and watched a lot but yeah, here we are.