I'm working on a small text adventure as a way to practice Zig. In it, I want the player to be able to move from one Location to another, with the locations stored within the World Map. While I can add a line to Main that accomplishes what I'm trying to do, trying to add a function to the World Map struct will not compile.
pub const WorldMap = struct {
currentLocation: *const Location = undefined,
locations: []const Location,
pub fn setLocation(self: WorldMap, loc: *Location) void {
self.currentLocation = &loc;
}
pub fn printLocationBrief(self: WorldMap) void {
self.currentLocation.print();
}
//TODO: Add method for traversing from the current location to a new one.
//pub fn travelToFirstConn(self: WorldMap) void {
// self.currentLocation = self.currentLocation.connections[0].dest;
//}
};
The commented out section will not compile. Meanwhile if I add essentially the same line in Main...
try stdout.print("Testing first move...\n", .{});
Map.currentLocation = Map.currentLocation.connections[0].dest;
Map.printLocationBrief();
It works just fine. Why?