yo im really new to zig comming from c# and c++ is there such a thing as global variable? like c# static variables in a (static)class or cpp where u can import a header file in any other file and all have a reference to the same variable?
if zig doesnt really have that do i have to pass around structs/ptrs?
const std = @import("std");
const tt = @import("TileType.zig");
pub const chunkSize: i32 = 32;
pub const ChunkKey = packed struct {
x: i32,
y: i32,
};
pub const Chunk = struct {
offsetX: i32,
offsetY: i32,
data: [chunkSize*chunkSize]tt.TileType,
};
var chunks: ?std.hash_map.AutoHashMap(ChunkKey, Chunk) = null;
pub fn init(allocator: std.mem.Allocator) void {
chunks = std.hash_map.AutoHashMap(ChunkKey, Chunk).init(allocator);
}
pub fn createChunk(chunk: Chunk) ?*Chunk {
const key = ChunkKey{.x = chunk.offsetX, .y = chunk.offsetY};
chunks.?.put(key, chunk) catch |err| {
std.debug.print("Chunk {any} not saved on error: {any}", .{key, err});
return null;
};
return chunks.?.getPtr(key);
}
i tried some ways this is one of them. whenever i import it in multiple files it does not share its state. is that by design?