#Help newbie out, global-state stuff

1 messages · Page 1 of 1 (latest)

trail steppe
#

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?

thorny stream
trail steppe
#

so if you got that in some other file and import it in multiple other files it will all the same value even if one changes it right? im calling createChunk from multiple files but it seems to not update the var chunks. is it because its not in the main file?

thorny stream
#

wait actually: yes for the first question

#

looking at your code it should work

trail steppe
#

main.zig:

const std = @import("std");
const chunkManager = @import("world/Chunk.zig");
const tt = @import("world/TileType.zig");
const renderer = @import("Renderer.zig");

pub fn main() void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gpa.allocator();
    chunkManager.init(allocator); // setup chunk thingie
    _ = chunkManager.getTileType(0, 0); // works fine
    var data: [chunkManager.chunkSize*chunkManager.chunkSize]tt.TileType = undefined;
    for(0..chunkManager.chunkSize*chunkManager.chunkSize) |i| {
        data[i] = tt.TileType.Grass;
    }
    // create 4 chunks
    _ = chunkManager.createChunk(chunkManager.Chunk{.offsetX = 0, .offsetY = 0, .data = data});
    _ = chunkManager.createChunk(chunkManager.Chunk{.offsetX = 1, .offsetY = 0, .data = data});
    _ = chunkManager.createChunk(chunkManager.Chunk{.offsetX = 0, .offsetY = 1, .data = data});
    _ = chunkManager.createChunk(chunkManager.Chunk{.offsetX = 1, .offsetY = 1, .data = data});
    // _ = chunkManager.getCount(); // 4 (acurate)
    renderer.RenderBase();
}

renderer.zig:

const chunkManager = @import("World/Chunk.zig");
pub fn RenderBase() void {
    _ = chunkManager.getCount(); // call it but crashes because chunks isn't set up. (but i set it up in main.zig)
}

chunk.zig:


pub 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);
}

pub fn getCount() u32 {
    return chunks.?.count();
}
#

this is what i got now.
main.zig inits the chunk, creates 4 and then calls a function from renderer.zig
renderer.zig tries to do stuff with chunk but fails cus its not set up yet?

why would it not be set up

#

i cant see whats wrong with it since it chunks looks to me like a global variable?

desert sedge
#

can you print the address of chunks in both contexts

trail steppe
#

main: ?hash_map.HashMap(world.Chunk.ChunkKey,world.Chunk.Chunk,hash_map.AutoContext(world.Chunk.ChunkKey),80)@7ff7f4a291c8
renderer: ?hash_map.HashMap(World.Chunk.ChunkKey,World.Chunk.Chunk,hash_map.AutoContext(World.Chunk.ChunkKey),80)@7ff7f4a29200

#

omfg

#

main: ?hash_map.HashMap(World.Chunk.ChunkKey,World.Chunk.Chunk,hash_map.AutoContext(World.Chunk.ChunkKey),80)@7ff7603e91c8
renderer: ?hash_map.HashMap(World.Chunk.ChunkKey,World.Chunk.Chunk,hash_map.AutoContext(World.Chunk.ChunkKey),80)@7ff7603e91c8

#

7ff7603e91c8
7ff7603e91c8

#

now its the same after i changed the import from
const chunkManager = @import("world/Chunk.zig");
to
const chunkManager = @import("World/Chunk.zig");

#

there is only one World directory inside my src/

thorny stream
trail steppe
#

weird

trail steppe
#

thx btw, can finally continue lol