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.

Newcomers DBPro Corner / Item Types/Inventory System

Author
Message
Alaror
14
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 30th May 2011 20:46
I'm at the point of development where I need to start thinking about items, which can be placed in the world, picked up by the player, stored in an inventory and used on other items in the world. The item types can range from resources found in the world (stone, ore, etc) to building items (doors, tables, etc) to weapons (guns, magical items, etc).

Now, each variety of item (world, building, weapons and the rest) will have a set of features unique to them. By this I mean that stone blocks will have a health level to decide when it should be destroyed during harvesting, while a gun will have a strength level to decide how much damage it does. Stone does not need a strength level, just as a gun does not need a health level. I could go on and list a bunch more examples but I'm sure you get the general idea; every item type will have a unique set of attributes not shared by any other item type.

I've heard that user defined types are the best way to create lists of items since you can give them special attributes. Wouldn't doing this force me to list every single possible attribute for each item? As an example, stone blocks would have the strength attribute as well as the gun having a health attribute.

Ideally I'd have a parent user defined type called "Item," with sub user defined types such as "WorldBlock" and "Weapon." Is this possible while still allowing for those items to be placed in an inventory system? Is there a simpler way of going about this?


On a somewhat related note, how would you give each item a unique ID on creation? I want there to be more than one of each item in the world, each having a unique set of attributes (10 blocks of stone each have their own health).
enderleit
17
Years of Service
User Offline
Joined: 30th May 2007
Location: Denmark
Posted: 31st May 2011 00:01
Here's a quick example of how it can be done...
This is not runnable though, just showing how it can be set up.



Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 31st May 2011 00:11
Yeah you could have an item UDT that has an item type and a general UDT that mean different things based on item type.

type ItemData
ItemType as byte
Health as integer
Power as integer
endtype

ItemType:
1 = Weapons
Health = Weapon condition (0=broken)
Power = Max Damage

2 = Armor
Health = Armor condition (0=broken)
Power = Max Protection

3 = Blocks
Health = Time till destroyed
Power = Harvest Level

4 = Food
Health = Hit Points gained on consuming
Power = Quantity of food (for stackable items)

Generally there are two lists for items. A master list that you use to copy the initial items stats to the world item list. The world item list changes based on what the user does to the items in the world list. The master item list is never changed and there's only one of each item in the master list.

Alaror
14
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 31st May 2011 17:05
Thanks for the examples. Still not fully understanding all this but it is making more sense.

I'm a bit confused as to what this part of your code does. Is the array used to store every item created of that specific item type, or is it used to store the attributes of each item so they can be called easily when they need to be created?



Is there a preferred method for declaring the values of each item so the program knows what attributes to give an item when it's told to create one? Is it possible to have some attributes set in stone (such as item value or weight) with others being variables that a function can alter (position and the like)?
Alaror
14
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 1st Jun 2011 17:36
I've been giving this all some more thought and think I understand how it works, but I'd appreciate it if someone would be willing to look through what I wrote below and let me know if I'm on the right track.

My plan is to create a separate file for every item type, where the item attributes will be saved. Each item will be listed in at least one of those files, but not more than one. On starting the game the file data will be read and placed into an array; there will be one array per file. A "CreateObject" function will be used with the following input options: object type, object number, x position, y position, number of objects to create. Within that function the program will first check to see what item type has been called, and then search for the item number within that specific array. So it might search for item number 25 in the Weapon array or 25 in the Consumable array.

After the item has been found in the array, a UDT (one of these per object type) will be used to create the object, pulling the information it needs from the array or using the input information from the function (x position, y position, etc). This will loop for as many of that object as needs to be created.
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 1st Jun 2011 19:26
Yeah you're on the right track.

Alaror
14
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 2nd Jun 2011 18:50
A few more questions came up as I've been working on this. It's kind of a hodge podge of different things but all relates to items


1. I don't think saving the item information to an array will work because I want to be able to save both integers and strings. Would it make more sense to open the file where all the item information is saved every single time I need to make an item, reading the information directly from the file? If not I'm drawing a blank as to how else I can list all the info for reading when I need it.

2. How exactly does DarkBasic Pro handle sprite interactions? By this I mean that if I have a piece of code that deletes a sprite of a specific type when another sprite collides with it, and the room is filled with 100 versions of the first sprite, does the program know to delete only the sprite that gets collided with, or will it delete all 100? Sorry if my wording of that question is confusing

3. What method is usually used to link sprites and the data they represent? For example, if I wanted to implement a system that randomized weapon stats and I had 5 weapons on the screen, how can I make the game tell the difference between each one?
enderleit
17
Years of Service
User Offline
Joined: 30th May 2007
Location: Denmark
Posted: 2nd Jun 2011 19:44
1. Not sure I'm understanding this right, but...
If you make a UDT for a specific object type like weapons, the UDT can contain any kinds of data like integers, float, and strings, all mixed together. When you save/load you just have to save each individual member of an UDT separately:


also you can make an array using a UDT:



2. You can make many sprites using the same image, so it will look like there is 100 of the same sprite, but it's actually different sprites using the same image. When you delete a sprite you just delete that one sprite, not the image. So the rest of the sprites that are using the same image are still on screen.

3. You might create 5 weapon instances and in the WeaponList at each weapon you can add an element called SPR which will hold the sprite number used for this weapon. Might also be a good idea to have an IMG element incase you want different looking weapons.

Alaror
14
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 2nd Jun 2011 23:27 Edited at: 2nd Jun 2011 23:28
@enderleit

Thanks for the reply!


1. My question was more relating to how I should save all of the item attributes so they can be called upon when they need to be created. My plan is to create a separate .dat file for every item type (WorldBlock, Consumable, Weapon, etc). From there when I need to create an item I would first search for its ID number in the file, and then pull the rest of the information (Strength, Health, Weight, etc).

2. Wow, I had a completely different understanding of sprites. No wonder I was confused! In my mine "sprite" and "image" were interchangeable haha.

3. That, along with your answer to #2, clears it up nicely; thanks!


I think I understand the individual pieces, so now it's a matter of putting them together. I tried working on a function that would create objects but I'm drawing a blank. From my understanding the following needs to be done when an object/sprite is created:

A. The sprite is created and added to the screen using SPRITE. The sprite number should be a global variable that is increased by 1 every time a sprite is created (at the start of the game it's 1 and by the end it may be into the tens of thousands; it only increases, never decreases).

B. The item attribute information is pulled from an external file. This information is then added to an array which lists every object/sprite in existence. On collision of two sprites this array is checked to find the what needs to be done with it. The if an object/sprite is deleted it is removed from the array.


It seems like I can do all that without even touching a UDT. Are they even necessary, or am I not understanding this all correctly. Example code for the item creation part would be a tremendous help.
enderleit
17
Years of Service
User Offline
Joined: 30th May 2007
Location: Denmark
Posted: 3rd Jun 2011 00:39
A. Sprite number does not necessarily have to be a global. The variable type doesn't matter, only the value.

You can create sprites in any order you would like. So you could create sprite 504 and then sprite 12 and then sprite 134 if you need to. But you need to make sure the sprite number is available, and is not already in use by another sprite.
There are some great functions for this in the matrix1utils plugin which you can download from this forum.

Here's another option:


Here's an example of how one could create items:


Yeah... I got carried away...

Alaror
14
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 3rd Jun 2011 17:29
Don't have much time to look over your post in the next few days but I did want to say thank you for taking the time to write all that out!
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 3rd Jun 2011 18:23
Quote: "It seems like I can do all that without even touching a UDT. Are they even necessary, or am I not understanding this all correctly. Example code for the item creation part would be a tremendous help."


Consider a UDT (User Defined Type) as a blueprint for data. It can either be a basis for an array, variable, or database structure in a file. You can use a UDT in one of those ways or all of them at the same time.

Alaror
14
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 16th Jun 2011 22:06
I've progressed a good bit since originally posting this topic but I did have a question about the code enderleit posted. In the following code sample you didn't set a size for the array when you DIMed it. Won't this cause problems, or does the program automatically increase/decrease the size of the list since you used "array insert at bottom" later in the code?


Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 16th Jun 2011 22:47
I'm not Enderleit of course but it's perfectly ok to do that and won't cause errors. When you treat arrays that way their called dynamic arrays. Like for large databases when the programmer doesn't really know how big of a database the user will need they use dynamic arrays because it grows/shrinks the array size as it's needed rather than allocating enough memory to have a fixed array size which limits the program.

BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 17th Jun 2011 16:04
If I'm not too late to help out I have two tutorials on this subject:

Asset Management

Scalable Assets and Templates

Alaror
14
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 18th Jun 2011 19:29
@Grog Grueslayer One of my arrays returns errors unless I've entered some sort of value there (using 0 right now). It says "Array does not exist or array subscript out of bounds at line ..."


@BatVink Thanks for the links! This is essentially what I ended up doing, so it's nice to see I'm on the right track haha.
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 19th Jun 2011 00:10
Show us some code of the troubled area (and the UDT and DIM statement) and we'll try to track down the problem.

Alaror
14
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 19th Jun 2011 05:15 Edited at: 19th Jun 2011 05:16
Alright, here's all the code that relates to the player arrays in question. It's broken up into different areas of the code but I've posted it below in the order it appears. Sorry for the messed up positioning of some of the code.


Player defined type.



Where the arrays are DIMed



Where the _Created and _Data arrays interact with each other.



Where the _Data values are declared and where the error message is pointing to.
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 19th Jun 2011 10:12
When you start an array with nothing you have to use a command like ARRAY INSERT AT BOTTOM right before adding to the array otherwise you get the out of bounds error. When you do something like "DIM Test()" you basically tell the computer "DIM Test(-1)" which is an array without even a zero element. Using "ARRAY INSERT AT BOTTOM Test()" adds one element to the bottom of the array so it's as if you dimensionalized it like "DIM Test(0)". Use "ARRAY INSERT AT BOTTOM Test(),10" adds another 10 as if you said "DIM Test(10)".



Alaror
14
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 20th Jun 2011 16:27
Hah, of course. I had used that for every other array but that one. Thanks for the help
2Beastmode4u
14
Years of Service
User Offline
Joined: 14th Feb 2011
Location: Loading...
Posted: 27th Jul 2011 06:07
Hey guys, here is my data I was setting up for an inventory:



So, how would I have a game read this data for an inventory that they would open would show the stats of the weapon (if in player possession) and when I attack it would read the info and when I move it would read the info.

I looked at the one by

Quote: "REM - Examples of getting item info.
if Items(id).itemtype = BLOCK
Block(Items(id).itemid).health = 100
endif
if Items(id).itemtype = WEAPON
Weapon(Items(id).itemid).strength = 10
endif
"


So how do I use this for my own code? Also, I have a variable for "WeaponID" but I don't know where to put it. Do I put it with the other stats?
Thanks for any help.


Cheers.

God help me, Please.
enderleit
17
Years of Service
User Offline
Joined: 30th May 2007
Location: Denmark
Posted: 29th Jul 2011 08:02 Edited at: 29th Jul 2011 08:09


Hope that was understandable...

Another note:
After using "array insert at bottom Weapon()" you can refer to the newly created Weapon() with empty braces. This just means that it will use the current array index, which is the newly create one.
Elsewhere in your code you should put a valid index number in the braces to get the weapon you want.

Phaelax
DBPro Master
22
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 31st Jul 2011 21:14
Quote: "Another note:
After using "array insert at bottom Weapon()" you can refer to the newly created Weapon() with empty braces. This just means that it will use the current array index, which is the newly create one."


That's a good tip to know, saves from having to call array count.

Login to post a reply

Server time is: 2025-05-18 01:47:58
Your offset time is: 2025-05-18 01:47:58