file: Fruit.zig
pub fn init_random(rows: u32, columns: u32) Self {
var this = init(0, 0);
if (this.rand == null) {
var __rand = std.rand.DefaultPrng.init(blk: {
var seed: u64 = undefined;
std.posix.getrandom(std.mem.asBytes(&seed)) catch |err| {
std.debug.print("Got error: {}", .{err});
};
break :blk seed;
});
this.rand = __rand.random();
}
const rand_x = this.rand.intRangeLessThan(usize, 0, columns);
const rand_y = this.rand.intRangeLessThan(usize, 0, rows);
this.x = rand_x;
this.y = rand_y;
std.debug.print("{any}\n", .{this});
return this;
}
Writing this code about i get the following error:
/home/ledrake/.local/share/zig-linux-x86_64-0.13.0/lib/std/posix.zig:581:43: error: comptime call of extern function
const rc = std.c.getrandom(buf.ptr, buf.len, 0);
~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
src/Fruit.zig:22:32: note: called from here
std.posix.getrandom(std.mem.asBytes(&seed)) catch {};
~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
src/Grid.zig:21:33: note: called from here
fruit: Fruit = Fruit.init_random(Self.rows, Self.columns),
~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
Self: src/Grid.zig:8:14
Grid: src/Grid.zig:21:45
remaining reference traces hidden; use '-freference-trace' to see all reference traces
error: the following command failed with 1 compilation errors:
/home/ledrake/.local/share/zig-linux-x86_64-0.13.0/zig build-exe /home/ledrake/files/zig/snake_clone_zig/.zig-cache/o/c05909e159268da4caa80e86e9fd89a3/libraylib.a -ODebug -I /home/ledrake/files/zig/snake_clone_zig/.zig-cache/o/8edfb742c363db90f6c4bd0d23f41e18 --dep config --dep raylib --dep raygui -Mroot=/home/ledrake/files/zig/snake_clone_zig/src/main.zig -Mconfig=/home/ledrake/files/zig/snake_clone_zig/.zig-cache/c/f4f0e9ac1319d5626a0e4d187455046a/options.zig -ODebug -Mraylib=/home/ledrake/.cache/zig/p/12202f8c415153088be8df39a51e0a4c9d402afd403422a0dcc9afdd417e437a6fdb/lib/raylib.zig -ODebug --dep raylib-zig=raylib -Mraygui=/home/ledrake/.cache/zig/p/12202f8c415153088be8df39a51e0a4c9d402afd403422a0dcc9afdd417e437a6fdb/lib/raygui.zig -lGL -lX11 -lc --cache-dir /home/ledrake/files/zig/snake_clone_zig/.zig-cache --global-cache-dir /home/ledrake/.cache/zig --name snake_clone --listen=-
Build Summary: 4/9 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run snake_clone transitive failure
├─ zig build-exe snake_clone Debug native 1 errors
└─ install transitive failure
└─ install snake_clone transitive failure
└─ zig build-exe snake_clone Debug native (+3 more reused dependencies)
error: the following build command failed with exit code 1:
/home/ledrake/files/zig/snake_clone_zig/.zig-cache/o/99df77fd6fb92261294e5d94736265af/build /home/ledrake/.local/share/zig-linux-x86_64-0.13.0/zig /home/ledrake/files/zig/snake_clone_zig /home/ledrake/files/zig/snake_clone_zig/.zig-cache /home/ledrake/.cache/zig --seed 0x2fb23b69 -Z99df11b0e0ecfff9 run
And i don’t really understand what it means with error: comptime call of extern function
When i run it in its own file like this, on its own project
file: main.zig
const std = @import("std");
pub fn main() !void {
var __rand = std.rand.DefaultPrng.init(blk: {
var seed: u64 = undefined;
std.posix.getrandom(std.mem.asBytes(&seed)) catch |err| {
std.debug.print("Got error: {}", .{err});
};
break :blk seed;
});
const rand = __rand.random();
const rand_x = rand.intRangeLessThan(usize, 0, 10);
const rand_y = rand.intRangeLessThan(usize, 0, 9);
std.debug.print("x:{},y:{}\n", .{ rand_x, rand_y });
}
I get the correct output:
x:1,y:8
Note: I don’t know if it’s important but I am running things on WSL