Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Inventory System

Author
Message
icemann
21
Years of Service
User Offline
Joined: 15th Apr 2004
Location:
Posted: 2nd Jul 2004 14:23
Hey all

I`ve been considering doing a remake for the remakes 2004 competition over at remakes.org, and my remake was to be of one of the "Fighting Fantasy" books remade into text adventure form. And so I began coding away, but then hit one little road block (since I`m still very new to programming), and that was in how to incorporate a inventory system in a text adventure.

For example, a player picks up a rock, a scissors and a piece of paper. The player types in "inventory" which should then show all items he currently has. Now as far as that bit goes I think an array would do the trick there. But if the player was to later type in "use paper" or "cut paper" etc, how exactly could you do all that without having to implement an extensive if statement system for each new section (which was the only way I can correctly think of it working).

Thankyou for any help that you can give me on this.

Two stones, two crosses, the rest is just icing. -7th Guest
Arkheii
21
Years of Service
User Offline
Joined: 15th Jun 2003
Location: QC, Philippines
Posted: 2nd Jul 2004 21:11
Yep, you've got the right idea about arrays. Unfortunately, a for loop with an if statement inside is still the only way to go.

What do you mean by "each new section"? Like, "use paper", "eat paper" or "throw paper"? Well, you could try parsing the string (using functions like left$, mid$, or right$), so if the first few characters of the input spells out "eat" then check the word that comes after that, "paper". Then you'd check a data table or array to see if "paper" can accept the command "eat".

If you think if statements are getting too long, you might wanna try a select statement instead. You'll encounter a lot of conditional processing in programming anyway. What good is the computer if it executes all programs the same way?

Phaelax
DBPro Master
22
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 3rd Jul 2004 07:46
If you're referring to removing an item from the inventory, you should keep all items in an arraylist.

"eureka" - Archimedes
icemann
21
Years of Service
User Offline
Joined: 15th Apr 2004
Location:
Posted: 3rd Jul 2004 17:27
arkheii: By "each new section" I meant new areas. For example, the player begins in a cave, where he may do a few things (pick up a rock, turn on a torch etc). He then has the choice of going north to the entrance of the cave where he may do other things (read a sign etc). This new north area is one example of a new section in the game, with its own options of things that can and cannot be done incomparison to the section/area before it.

I had guessed before creating this thread that an array would do the trick as far as inventory goes which I`m glad to hear was right. So if the player "picked up" a rock it would go into the area in the first position (eg inventory(1)), and if he later picked up scissors it would go into the second (eg inventory(2)). Say he later typed "use scissors" how exactly would you do that correctly incode.

The only way I can think of is via an if statment system such as:

if command="use scissors"
if havescissors=1
print "You use the scissors"
else
print "You do not have scissors"
endif

Or is there a better way inwhich a search of the entire inventory array (in this example) for the entry "scissors" could be done?

Two stones, two crosses, the rest is just icing. -7th Guest
Arkheii
21
Years of Service
User Offline
Joined: 15th Jun 2003
Location: QC, Philippines
Posted: 4th Jul 2004 09:04 Edited at: 4th Jul 2004 09:05
Yes, you will have to search the inventory array. Of course, it's better if you represent your items with numbers instead, like scissors is 1, rock is 2, etc.

So, you would have something like:



That's how the framework might go, and with some fixing you can put them in functions to make it easier on yourself. Of course, there is another, more hard-coded way of doing it...



Since text based games are usually scripted very strictly, the second one is more efficient. But the first one can be used if you want to print the items that the person has in the order which they were acquired. I typed this right here so I'm unsure about the code, but you get the idea.

icemann
21
Years of Service
User Offline
Joined: 15th Apr 2004
Location:
Posted: 4th Aug 2004 03:04
I know its been a while, since the above code was posted but I finally got the chance to test out the code, which worked well. But after alot of trial and error attempts created my own system which is a slightly altered version of the above code. Rather that giving each item type (eg scissor, rock) a specific number, this simply adds the item to the array.

Item pickup code:


Inventory display code:


Two stones, two crosses, the rest is just icing. -7th Guest

Login to post a reply

Server time is: 2025-05-25 17:44:02
Your offset time is: 2025-05-25 17:44:02