2021-02-09 02:35:28 +01:00
|
|
|
//
|
2022-07-31 21:57:20 +02:00
|
|
|
// As with integers, you can pass a pointer to a struct when you
|
|
|
|
// will wish to modify that struct. Pointers are also useful when
|
|
|
|
// you need to store a reference to a struct (a "link" to it).
|
2021-02-09 02:35:28 +01:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
2022-07-31 21:57:20 +02:00
|
|
|
// We can write functions that take pointers to structs as
|
|
|
|
// arguments. This foo() function modifies struct v:
|
2021-02-09 02:35:28 +01:00
|
|
|
//
|
|
|
|
// fn foo(v: *Vertex) void {
|
|
|
|
// v.x += 2;
|
|
|
|
// v.y += 3;
|
|
|
|
// v.z += 7;
|
|
|
|
// }
|
|
|
|
//
|
2022-07-31 21:57:20 +02:00
|
|
|
// And call them like so:
|
2021-02-09 02:35:28 +01:00
|
|
|
//
|
|
|
|
// foo(&v1);
|
|
|
|
//
|
|
|
|
// Let's revisit our RPG example and make a printCharacter() function
|
2022-07-31 21:57:20 +02:00
|
|
|
// that takes a Character by reference and prints it...*and*
|
|
|
|
// prints a linked "mentor" Character, if there is one.
|
2021-02-09 02:35:28 +01:00
|
|
|
//
|
|
|
|
const std = @import("std");
|
|
|
|
|
2021-02-15 22:55:44 +01:00
|
|
|
const Class = enum {
|
2021-02-09 02:35:28 +01:00
|
|
|
wizard,
|
|
|
|
thief,
|
|
|
|
bard,
|
|
|
|
warrior,
|
|
|
|
};
|
|
|
|
|
2021-02-15 22:55:44 +01:00
|
|
|
const Character = struct {
|
2021-02-09 02:35:28 +01:00
|
|
|
class: Class,
|
|
|
|
gold: u32,
|
2022-07-31 21:57:20 +02:00
|
|
|
health: u8 = 100, // You can provide default values
|
2021-02-09 02:35:28 +01:00
|
|
|
experience: u32,
|
2022-07-31 21:57:20 +02:00
|
|
|
|
|
|
|
// I need to use the '?' here to allow for a null value. But
|
|
|
|
// I don't explain it until later. Please don't tell anyone.
|
|
|
|
mentor: ?*Character = null,
|
2021-02-09 02:35:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
pub fn main() void {
|
2022-07-31 21:57:20 +02:00
|
|
|
var mighty_krodor = Character{
|
|
|
|
.class = Class.wizard,
|
|
|
|
.gold = 10000,
|
|
|
|
.experience = 2340,
|
|
|
|
};
|
|
|
|
|
|
|
|
var glorp = Character{ // Glorp!
|
2021-02-15 22:55:44 +01:00
|
|
|
.class = Class.wizard,
|
|
|
|
.gold = 10,
|
2021-02-09 02:35:28 +01:00
|
|
|
.experience = 20,
|
2022-07-31 21:57:20 +02:00
|
|
|
.mentor = &mighty_krodor, // Glorp's mentor is the Mighty Krodor
|
2021-02-09 02:35:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// FIX ME!
|
2022-07-31 21:57:20 +02:00
|
|
|
// Please pass Glorp to printCharacter():
|
2021-02-15 22:55:44 +01:00
|
|
|
printCharacter(???);
|
2021-02-09 02:35:28 +01: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 22:55:44 +01:00
|
|
|
.wizard => "Wizard",
|
|
|
|
.thief => "Thief",
|
|
|
|
.bard => "Bard",
|
2021-02-09 02:35:28 +01:00
|
|
|
.warrior => "Warrior",
|
|
|
|
};
|
|
|
|
|
2021-04-04 22:23:27 +02:00
|
|
|
std.debug.print("{s} (G:{} H:{} XP:{})\n", .{
|
2021-02-09 02:35:28 +01:00
|
|
|
class_name,
|
|
|
|
c.gold,
|
|
|
|
c.health,
|
|
|
|
c.experience,
|
|
|
|
});
|
2022-07-31 21:57:20 +02:00
|
|
|
|
|
|
|
// Checking an "optional" value and capturing it will be
|
|
|
|
// explained later (this pairs with the '?' mentioned above.)
|
|
|
|
if (c.mentor) |mentor| {
|
|
|
|
std.debug.print(" Mentor: ", .{});
|
|
|
|
printCharacter(mentor);
|
|
|
|
}
|
2021-02-09 02:35:28 +01:00
|
|
|
}
|