Author Topic: Inventory  (Read 3583 times)

0 Members and 1 Guest are viewing this topic.

Offline eddy498

  • Newbie
  • Posts: 25
    • View Profile
Inventory
« on: March 17, 2019, 06:42:41 pm »
Hey guys i am looking for a way to hold an inventory in a text based game. I have looked around but cant quite understand other peoples ways of making it. if any of you know a tutorial i can use that explains how i can make inventory, and much better if it has an actual design or layout not just typed but like how you see in games let me know thanks.

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Inventory
« Reply #1 on: March 17, 2019, 07:51:17 pm »
just a thought, what about a structure that would have all the elements of the character including items.

Offline commandvom

  • Newbie
  • Posts: 10
    • View Profile
Re: Inventory
« Reply #2 on: March 18, 2019, 06:46:58 am »
Hi, I can show you the inventory system (and other more) that I use in my project , whose source code you can download from here:

https://www.qb64.org/forum/index.php?topic=241.msg1268#msg1268

basically I use an array, where I keep the code of the item, and some other player property/characteristics . I save it on disk with a format that you can find in '\ server \' inside the files '1.txt', '2.txt', '3.txt' etc, and so for each user.

to raise it to the array I use the function 'serverenviarinventario'

and to show it on the screen 'mostrarinventario'

Of course I use other functions for the screen design and how to manipulate the inventory

any questions are welcome!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Inventory
« Reply #3 on: March 18, 2019, 10:41:43 am »
Hi eddy498,

To follow up on what jack said, do you know about using TYPE... END TYPE definitions, also known as User Defined Types or Structures or Record Structures for databasing:
Code: QB64: [Select]
  1.  TYPE ContactInfo
  2.    First AS STRING * 10
  3.    Last AS STRING * 15
  4.    Address1 AS STRING * 30
  5.    Address2 AS STRING * 30
  6.    City AS STRING * 15
  7.    State AS STRING * 2
  8.    Zip AS LONG   ' (4 bytes)
  9.    Phone AS STRING * 12
  10.  
Sample from Wiki.

A playerType might be setup with any and everything you might want to track with each player.

With a Type defined, you can then setup an array of the playerType for all the players and track their status. Such an array can be saved to file to continue a game later or be displayed as needed during a game.

Say to just track x and y locations of each player:
TYPE playerType
   x AS INTEGER
   y AS INTEGER
   pRadius AS INTEGER
   pColor AS _UNSIGNED LONG
END TYPE

DIM SHARED nPlayers
nPlayers = 10
DIM SHARED player(1 to nPlayers) as playerType

move player 5 right 1 place:
player(5).x = player(5).x + 1

display all players as circles for which images could be replaced later.
FOR p = 1 TO nPlayers
   CIRCLE (player(i).x, player(i),y), player(i).pRadius, player(i).pColor
next 

The advantage of this system is that it keeps all data about player(i) under one labeled location. 

« Last Edit: March 18, 2019, 10:50:05 am by bplus »

Offline eddy498

  • Newbie
  • Posts: 25
    • View Profile
Re: Inventory
« Reply #4 on: March 18, 2019, 09:22:42 pm »
Commandvom - Thanks ill look into that if i have questions ill let you know :)

bplus, no i didnt know about that type...end type definitions. But ill look into it since it seems like it will help greatly with creating save files in my games and such thanks.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Inventory
« Reply #5 on: March 18, 2019, 11:15:03 pm »
Keep in mind with TYPEs that we still do not have the availability of arrays in user defined types, despite myself along with several others begging and pleading FOR YEARS! to have it added.

so you can not;
Code: QB64: [Select]
  1. Type Playerdata
  2.  BackPack(32) as _byte
  3.  ...
  4.  
  5. Dim Player as Playerdata
  6.  

you can do
Code: QB64: [Select]
  1. Type Inventory
  2.  ItemID as _BYTE
  3.  ItemCount as _BYTE
  4.  ...
  5.  
  6. Dim PlayersInventory(32) as Inventory
  7.  
Granted after becoming radioactive I only have a half-life!

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Inventory
« Reply #6 on: March 19, 2019, 11:30:40 am »
Cobalt, I see what you're saying. I assume this backback can only hold so many items? Maybe 10? Then you can set up item1, item2, etc etc?
Also, you could have one string of say 100 characters and have a list of item #s separated by some character, say like this
"23|16|114|1|12" and have a function that reads them all?

Another way that might not be efficient would be to have a type called item that holds a number for a player id and a description and have an array of them, then parse the array for player N when you want to see items they own?
QB64 is the best!