2021-02-08 20:35:28 -05:00
|
|
|
//
|
|
|
|
// Passing integer pointers around is generally not something you're going
|
|
|
|
// to do. Integers are cheap to copy.
|
|
|
|
//
|
|
|
|
// But you know what IS useful? Pointers to structs:
|
|
|
|
//
|
|
|
|
// const Vertex = struct{ x: u32, y: u32, z: u32 };
|
|
|
|
//
|
|
|
|
// var v1 = Vertex{ .x=3, .y=2, .z=5 };
|
|
|
|
//
|
|
|
|
// var pv: *Vertex = &v1; // <-- a pointer to our struct
|
|
|
|
//
|
|
|
|
// Note that you don't need to dereference the "pv" pointer to access
|
|
|
|
// the struct's fields:
|
|
|
|
//
|
|
|
|
// YES: pv.x
|
|
|
|
// NO: pv.*.x
|
|
|
|
//
|
|
|
|
// We can write functions that take pointer arguments:
|
|
|
|
//
|
|
|
|
// fn foo(v: *Vertex) void {
|
|
|
|
// v.x += 2;
|
|
|
|
// v.y += 3;
|
|
|
|
// v.z += 7;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// And pass references to them:
|
|
|
|
//
|
|
|
|
// foo(&v1);
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Let's revisit our RPG example and make a printCharacter() function
|
|
|
|
// that takes a Character pointer.
|
|
|
|
//
|
|
|
|
const std = @import("std");
|
|
|
|
|
2021-02-15 16:55:44 -05:00
|
|
|
const Class = enum {
|
2021-02-08 20:35:28 -05:00
|
|
|
wizard,
|
|
|
|
thief,
|
|
|
|
bard,
|
|
|
|
warrior,
|
|
|
|
};
|
|
|
|
|
2021-02-15 16:55:44 -05:00
|
|
|
const Character = struct {
|
2021-02-08 20:35:28 -05:00
|
|
|
class: Class,
|
|
|
|
gold: u32,
|
2021-02-15 16:55:44 -05:00
|
|
|
health: u8 = 100, // <--- You can also provide fields a default value!
|
2021-02-08 20:35:28 -05:00
|
|
|
experience: u32,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn main() void {
|
|
|
|
var glorp = Character{
|
2021-02-15 16:55:44 -05:00
|
|
|
.class = Class.wizard,
|
|
|
|
.gold = 10,
|
2021-02-08 20:35:28 -05:00
|
|
|
.experience = 20,
|
|
|
|
};
|
|
|
|
|
|
|
|
// FIX ME!
|
|
|
|
// Please pass our Character "glorp" to printCharacter():
|
2021-02-15 16:55:44 -05:00
|
|
|
printCharacter(???);
|
2021-02-08 20:35:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note how this function's "c" parameter is a pointer to a Character struct.
|
|
|
|
fn printCharacter(c: *Character) void {
|
|
|
|
// Here's something you haven't seen before: when switching an enum, you
|
|
|
|
// don't have to write the full enum name. Zig understands that ".wizard"
|
|
|
|
// means "Class.wizard" when we switch on a Class enum value:
|
|
|
|
const class_name = switch (c.class) {
|
2021-02-15 16:55:44 -05:00
|
|
|
.wizard => "Wizard",
|
|
|
|
.thief => "Thief",
|
|
|
|
.bard => "Bard",
|
2021-02-08 20:35:28 -05:00
|
|
|
.warrior => "Warrior",
|
|
|
|
};
|
|
|
|
|
2021-04-04 16:23:27 -04:00
|
|
|
std.debug.print("{s} (G:{} H:{} XP:{})\n", .{
|
2021-02-08 20:35:28 -05:00
|
|
|
class_name,
|
|
|
|
c.gold,
|
|
|
|
c.health,
|
|
|
|
c.experience,
|
|
|
|
});
|
|
|
|
}
|