I’ve created a little game per exercise 36 and I’ve had loads of fun.
I’m in the middle of debugging it right now, so s’cuse the bugs please.
I need suggestions of how I can globally view my ‘inventory’. I know that I could write code to every if statement from each def function, but I want to be able to write one piece of code and then call it at anytime without having to write that piece of code all over the place.
So for instance - I’d like it to where when a user is playing the game, they could just type ‘inventory’ and they could check/see the inventory list at any given time. And I was looking to achieve that with just one piece of code I write once but works globally. Is that possible?
I wrote this in python 3.x
I hope you all like the game, I’m doing more things to it by adding more dialog and making the battle scenes more intuitive with more decisions while fighting. I could figure that part out myself.
@Pauly Because inventory is a list it is automatically globaly visible. You can access and update it from everywhere also from inside functions. Lists have not the same scope as variables.
Totally understand. I’ve tried that before and it works, but I think I failed to explain more.
What I really need (and I"ll update the description), is when someone is playing the game, they could just type ‘inventory’ and they’ll could see the inventory list at any given time. And I was looking to achieve that with just one global piece of code. Is that possible?
I’m targeting the check inventory part to work similar to the way Zorg does, where the player can just type “inventory” from any part of the game to view their entire inventory
But I’m trying to avoid having to code check_inventory() function within each if statement. So I was assuming that their was some sort of way to create a function or variable where the developer just codes it once, and the player can just type ‘inventory’ to check their inventory from the console, but from anywhere
So, I created a function called check_inventory() that I could use to ask the player if they’d like to review their inventory, but found I have to add that function to every input() function and if statements, then the check_inventory() function gets executed and shows the list.
Honestly, in this game you have to do it this way. At the end of the book you do a game that allows you to parse the input from the user and then do what you need before dealing with room mechanics. That’s the game where you can do this.