#Basic struct question - error: no field or member function named 'getInput' in 'game.Game'

1 messages · Page 1 of 1 (latest)

little canopy
#

Hi all, another basic question. I'm trying to import a Struct from another file, and access the member methods to run a simple game loop.

At the moment this is an empty struct, but it doesn't seem to be importing, and any attempt at interacting with the struct instance results in an error.

What syntax am I missing?

main.zig and game.zig code screenshots attached.

languid elm
#

First question, did you write the file? Second, I feel like Game.isRunning should be a field, not a static variable.

#

Third, missing one screenshot

little canopy
#

I did write the file. Sorry let me attach the screenshot for main.zig

languid elm
#

No problem :)

little canopy
ashen aspen
#

Please post code as text, not images. Format as code with triple back tick lines.

runic linden
amber oasis
#

functions only become methods when the first argument is the container type

frigid patrol
#

you need to have a self parameter:

pub const Game = struct {
  const Self = @This();

  pub fn getInput(self: Self) void {}
  ...
};```
runic linden
#

You have a Game struct in the game.zig file

languid elm
#

Okay, you'd want isRunning to be a field, not a static, and the three functions should take a *Game

runic linden
#

Also, Zig files are implicitly structs. You don't NEED to write it in this specific way

little canopy
frigid patrol
#

it's something I do personally

#

you could just have Game instead of Self

#

and ofc make it a pointer (only if) you need to mutate it

languid elm
#

I.e.

pub const Game = struct {
    is_running: bool = true,

    pub fn getInput(game: *Game) void {
        _ = game; // Supress unused error
    }

    pub fn update(game: *Game) void {
        _ = game; // Supress unused error
    }
    
    // Const pointer because you don't mutate while rendering
    pub fn render(game: *const Game) void {
        _ = game; // Supress unused error
    }
}
runic linden
#

It's your own choice on whether to use @This or the struct's name

little canopy
#

ty all, really appreciate the help!