Twitch

Lesson Description
The "Role-Playing Game Exercise" Lesson is part of the full, Complete Go for Professional Developers course featured in this preview video. Here's what you'd learn in this lesson:
Students are instructed to implement three functions for a role-playing game. The functions are PickUpItem, DropItem, and UseItem.
Preview
CloseTranscript from the "Role-Playing Game Exercise" Lesson
>> Melkey: Now we're actually going to do an exercise. So I'm gonna ask you guys to write some code without me kind of yapping and writing it for you. So let's go here, we're gonna make just a new directory, we wanna call it exercise, okay? Within exercise, make a new file called exercise.go, okay?
Here we're gonna make a package called exercise. Again, we have a lot more freedom now that we've made our own package. And if you notice by my voice, I'm gonna get a little more excited because this is where I would assume the training was gonna come off because we have a lot more control with our go code.
We were all writing everything in the main code, that is It's pretty rigid, here we have a lot more freedom. So here's the exercise, I'm gonna read it out. I think I'm gonna give some code. But basically, we're gonna create, this says a new main.go, but exercise.go is fine.
I wanna make a falling RP game. And I guess you can create this in your main.go if you wanna execute this. But basically I want you guys to create a player struck that has a name and inventory field. I want you to create an item struct that has a name and type field.
And I want these to have certain methods, okay? So I want these methods to all be on the player struct. I want method one to be pick up item. It adds an item to a player's inventory. DropItem removes the item from an inventory by name. And then use item, this one is, you could hard code this depending on what the item name is.
You can just do, let's say, the item you pick up is a potion. Using the potion, we can hard code, if potion regain five health. But you don't need to actually have a player health bar, you just can print differences between your items. Okay, so here I'm gonna paste some just boiler plate code here.
>> Melkey: So this is kind of the preliminary setup I think would be appropriate, right? We have an items truck, a players truck and three methods. So kind of already set up the the boulder plate for those methods here. Try it out if you don't get it, honestly, that's not a big deal. But yeah, explore it, see how far you can go.
>> Melkey: So we left off by trying out an exercise where we basically are trying to mimic the very, very simple functionality of an MMORPG or something like that. Where you have a player, an item, you can pick items up, drop items and use items. This is just an intended exercise for us to get used to how things work in Go. And so I'm going to quickly just show the solutions for some of these here. I'm not gonna spend too much time exploring the intricacies of them. We can revisit this at the end, but we're gonna just simply show how I personally implemented these methods, right? So for PickUpItem, all I really did was, I know I have my p.Inventory, so I'm gonna just quickly do import, for me, PickUpItem. I know I have my p.Inventory, and I'm just going to append the item that I pick up into my inventory. And add this little print statement that says, I picked up an item. Okay, so I think that one was maybe the most straightforward implementation. Next will be a drop item. So drop item, you can see my editor's suggesting a functionality with replace append with slices.delete, which we were talking about, that starting in Go 1.21, there is the standard library that now provides some packages that help with slices and maps. I think those are phenomenal, I think those are great, they're very helpful. But nothing wrong doing things kind of under the hood, cuz I think the slices.delete function does the exact same as this append implementation here. But basically, the secret, I guess, of drop item is we iterate through our inventory. If we find the name, the item, and in our inventory that matches the item we are dropping, we are basically going to create a combination of our slices. So here we are appending, our p.Inventory up to i. And i is the index of the item that we found ranging over our inventory. And then we are going to append it to all the values after, so i+1. So remember back when we were exploring slices, we had the ellipsis here that contains all the values from another slice, which is essentially what we're doing here. We're creating two slices from our original slice, and just append those together to get this new slice, this new inventory, and then I have a return here. And just for a safe catch, if the value does not exist, my inventory, I just quickly say does not have in inventory. So the name of the player, Milky, does not have, let's say, ax in the inventory. Okay, and then UseItem.
>> Melkey: This one, I think, is probably the one that has the most room for debate, I suppose, cuz lots of different ways I was even talking to Michael just earlier. And I didn't even consider using the drop method or drop item, I kind of just reimplemented the same append functionality again. But yeah, I think the way I initially explained it, to use an item, I just range over that item again. If I find it and I hard coded the name potion and all I just say is I feel good, I feel rejuvenated, let me remove the item after use.
>> Speaker 2: So with removing an item like this, I mean, at first it would seem like this is pretty heavy on allocating new memory cuz it seems like you'd be wiping away the old one while not wiping it. Well yeah, wiping it the way the old inventory, reallocating a new one, is that what's happening under the hood?
>> Melkey: It's wiping out the old memory and making a new one. The context of the append method, again, it depends on the capacity and the length of the underlying array. Because here, I think what you're referencing is here we're basically taking the existing one, splitting it up, adding that, and here's a new inventory, right? The thing is, if you look, we're not explicitly reinstantiating a new slice, right? Here we have this inventory and we're just reassigning it a value, that's why there's single equals operation here. And so, what's happening is we are actually reusing the length and capacity of the original slice, we're just replacing those values. So we're not recreating the underlying data structure, the slice or the array. What we're doing is just wiping out all the values. But let's say this append method is, every time we use it, we're adding, I'm gonna say 20 or 100 new items. That would be pretty inefficient, because then, in that case, we would be basically battling the doubling capacity of the append method in Go under the hood. Does that answer your question? Does that make sense?
>> Speaker 2: I think so.
>> Melkey: Cool.
>> Melkey: So this is kind of the preliminary setup I think would be appropriate, right? We have an items truck, a players truck and three methods. So kind of already set up the the boulder plate for those methods here. Try it out if you don't get it, honestly, that's not a big deal. But yeah, explore it, see how far you can go.
>> Melkey: So we left off by trying out an exercise where we basically are trying to mimic the very, very simple functionality of an MMORPG or something like that. Where you have a player, an item, you can pick items up, drop items and use items. This is just an intended exercise for us to get used to how things work in Go. And so I'm going to quickly just show the solutions for some of these here. I'm not gonna spend too much time exploring the intricacies of them. We can revisit this at the end, but we're gonna just simply show how I personally implemented these methods, right? So for PickUpItem, all I really did was, I know I have my p.Inventory, so I'm gonna just quickly do import, for me, PickUpItem. I know I have my p.Inventory, and I'm just going to append the item that I pick up into my inventory. And add this little print statement that says, I picked up an item. Okay, so I think that one was maybe the most straightforward implementation. Next will be a drop item. So drop item, you can see my editor's suggesting a functionality with replace append with slices.delete, which we were talking about, that starting in Go 1.21, there is the standard library that now provides some packages that help with slices and maps. I think those are phenomenal, I think those are great, they're very helpful. But nothing wrong doing things kind of under the hood, cuz I think the slices.delete function does the exact same as this append implementation here. But basically, the secret, I guess, of drop item is we iterate through our inventory. If we find the name, the item, and in our inventory that matches the item we are dropping, we are basically going to create a combination of our slices. So here we are appending, our p.Inventory up to i. And i is the index of the item that we found ranging over our inventory. And then we are going to append it to all the values after, so i+1. So remember back when we were exploring slices, we had the ellipsis here that contains all the values from another slice, which is essentially what we're doing here. We're creating two slices from our original slice, and just append those together to get this new slice, this new inventory, and then I have a return here. And just for a safe catch, if the value does not exist, my inventory, I just quickly say does not have in inventory. So the name of the player, Milky, does not have, let's say, ax in the inventory. Okay, and then UseItem.
>> Melkey: This one, I think, is probably the one that has the most room for debate, I suppose, cuz lots of different ways I was even talking to Michael just earlier. And I didn't even consider using the drop method or drop item, I kind of just reimplemented the same append functionality again. But yeah, I think the way I initially explained it, to use an item, I just range over that item again. If I find it and I hard coded the name potion and all I just say is I feel good, I feel rejuvenated, let me remove the item after use.
>> Speaker 2: So with removing an item like this, I mean, at first it would seem like this is pretty heavy on allocating new memory cuz it seems like you'd be wiping away the old one while not wiping it. Well yeah, wiping it the way the old inventory, reallocating a new one, is that what's happening under the hood?
>> Melkey: It's wiping out the old memory and making a new one. The context of the append method, again, it depends on the capacity and the length of the underlying array. Because here, I think what you're referencing is here we're basically taking the existing one, splitting it up, adding that, and here's a new inventory, right? The thing is, if you look, we're not explicitly reinstantiating a new slice, right? Here we have this inventory and we're just reassigning it a value, that's why there's single equals operation here. And so, what's happening is we are actually reusing the length and capacity of the original slice, we're just replacing those values. So we're not recreating the underlying data structure, the slice or the array. What we're doing is just wiping out all the values. But let's say this append method is, every time we use it, we're adding, I'm gonna say 20 or 100 new items. That would be pretty inefficient, because then, in that case, we would be basically battling the doubling capacity of the append method in Go under the hood. Does that answer your question? Does that make sense?
>> Speaker 2: I think so.
>> Melkey: Cool.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops