Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

  • 0
zeonidas

[Help!] Persistent Item Data inside Inventory System

Question

Hey guys!, i've been trying to solve a problem i stumbled upon and i know for sure armedunity folks hav encountered this in their proyects, So let's get started!

 

Ill go straight to the problem, what i need to achieve, and then how my system works (kindda like the awefulmedia inv sys.) + pickup, equip....

 

[i m not asking for complete code i am just asking if any of u can just tell me how everyone does this usually, EVERY multiplayer game with items and guns should have some sort of a system that can actually keep persistent information inside Items...] right?

 

The Problem / Situation that is happening and need to fix: 

  • You pick a gun with 5 bullets inside, you equip it and shoot 5 bullets, then you drop it and the gun still have the 5 bullets (witch is the default)
  •  
  • You have a gun in your inventory with 10 bullets, you pick up from the floor the same type of gun (same prefab , same everything) that has 4 bullets, now both in your inventory hav 4 bullets
  •  
  • Each time you switch/switch out your gun u start having all the bullets again
  •  

Goal:   I need to be able to save/update inside each object/item information like Current Ammo Count, Attachments,  EVEN if more than one of the SAME object exists inside the inventory.

 

You pick a gun from the floor, you have it now on your inventory, you equip it, you shoot with it till theres no more bullets, then you drop it again to the floor.

If someone else/yourself pick that gun, there should be none bullets left on it.

 

I know you guys know how to do this, but i just cant figure it out...  what am doing is i have TWO prefabs, one for the items on the floor that have an id, then when you pick them up you use that id to choose from the item database the correct item and add it to an inventory list of items , then if you choose one of those items on the inventory i find the other prefab (already selected as an prefab at the item database ) thats is the correct size and tag and a gunscript and Instantiate it on my players hand.

 

The thing is obvously each time i am instantiating or adding any object to my inventory and players hand, am always adding a pre-made default prefab saved at the resource files that has all the STANDARD variables.

 

 

 

A little bit about the code behind.

 

ITEM.CS

Please login or register to see this code.

ITEM DATABASE

Please login or register to see this code.

INVENTORY SYSTEM

Please login or register to see this code.

SWITCH ITEMS...

Please login or register to see this code.

If you took the time to read my problem and want to give a tought or two, thanks in advance!

 

~Zeo

Edited by zeonidas

Share this post


Link to post
Share on other sites

9 answers to this question

Recommended Posts

  • 0

First off, nicely formatted post  :YMAPPLAUSE:

 

second of all, 

  • You pick a gun with 5 bullets inside, you equip it and shoot 5 bullets, then you drop it and the gun still have the 5 bullets (witch is the default)

I can't see the code for where you drop the gun on the ground again but I would give that guns Item values the same ones as your gun while instantiating it

 

Please login or register to see this spoiler.

 

  • Each time you switch/switch out your gun u start having all the bullets again

I'm not too sure about this one, in your switch items function you instantiate the prefab, why not just use SetActive to activate it and disable it? Same values, same gun, no instantiation and it's easier in my opinion

 

hope you find some of this useful :P

Edited by lcomstive
  • Like 1

Share this post


Link to post
Share on other sites

  • 0
Please login or register to see this quote.

 

First of all, thanks for that fast response, thats the type of feedback i need.

 

Well,  i tried to mantain a clean format so you could understand me, while not being a native english speaker am having quite a hard time to explain what do i need, and indeed your first 2 points are actually the stuff am trying to avoid, these are under my problems list, stuff that are actually happening in my code right now..  that i DONT want to keep happening, that being said , about your 3rd point, i do need to keep my bullets, instead of having them all again, i want them to run out and then no more bullets anymore even if you switch or drop and pick the same gun.

 

Yes, i thought about activating and de-activating the guns so all the information will be kept between the switch, but that dosnt fix my problem completely, i need to be able to even have 4 exactly the same guns inside the inventory and all of them be able to have their OWN ammo count.

 

i just want a normal system like unt(u)rned, or day(z), or any other games where u can give your gun to someone and the gun will Mantain attachments and ammo... 

 

Thanks for helping !!!! thats was a fast protoype right there ;)

Share this post


Link to post
Share on other sites
  • 0

Well in that case why dont you just have your Item class derive from Monobehaviour ( Item : MonoBehaviour) and have it on your gun, then just access it whenever you need it. The variables will stay the same and you could just keep a list of gameobjects instead of items to access each (List<GameObject>). This is probably a bit useless but just my opinion :)

Share this post


Link to post
Share on other sites
  • 0

So you have your gun GameObject and you have Item attached to it with the values you want, and when you pick up the gun then you add the attached Item or the gun gameobject to a list of picked up items. Pretty much I'm saying dont have Item as a seperate class, maybe have it attached to your guns

  • Like 1

Share this post


Link to post
Share on other sites
  • 0

Storing gameobject list sounds a good idea at first, but it's not, it is a very bad solution and isnt OOP like it should be.

One object will have 3 scripts, second object 6 and third one 3 others scripts, youll end up with tens of if else statements for each gameobject so theres really no point of list itself, not even talking about performance here.

Data templates, storing, scene objects or renderers should be separate handlers.

Share this post


Link to post
Share on other sites
  • 0

Single items list will get you nowhere, this is a pure object managament question you are asking about, which, like i said, should be as oop and logical as possible.

Instead create separate lists for weapons, amno and so on.

  • Like 1

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

 

So, avoid the gameobject list... but create lists for weapons, ammo... but those lists will contain the object itself? how will it interact with the inventory? and how they could be unique ?

Share this post


Link to post
Share on other sites
  • 0

Array is indexes structure, instead of id you can use array index itself, and threat -1 as empty. If you want unsigned id to be able to use more per variable size, you can also threat null as empty or hash/ number id yourself. It really depends on the inventory, if it's item per slot - its easy, as it's node per slot. If certain item is more than one slot, it's much harder, but nothing impossible.

 

Your thinking should be modular, the more features you want, more harder and challenging it will become.

 

Example

 

  • I want pistol list, pistol can use certain mags, (for example glock 17 normal and extended mag), pistol can also use certain silencers.
  • Mag can have different ammo types - normal, armor piercing and so on.
  • Different silencer types, longer, shorter, louder, silent, maybe different camos

If including only basic properties, it should something like this.
 

bd72243962b1b0ca4bdd2a920bf509cb.png

 

Game contains 3 lists, handgun list, cartridge list, and ammo list. Each of those lists contain index (what is currently equipped) to other list, you could search it up.

In c# you can access list just like array list[index];

 

For each item you must define what can be equipped and what is currently is equipped.

Maybe for that you could event use single class template.

Please login or register to see this code. Please login or register to see this code. Please login or register to see this code.

Now player wants to equip certain mag to certain handgun, we simply check the choice, if true proceed and get the item;

Please login or register to see this code.

You will have to access & know the index from scene and inventory.

For initialed scene objects you can hold single simple script.

Please login or register to see this code.

You will need this on scene item pickup, to validate & render what you've picked up.
 Simply getcomponent and .getIndex();

 

Now, when you know the index you know everything.

 

There's a lot more than this, but this way of thinking will start you going in right direction.

 

 

This type of structures are very easy when types share similar or same fields or properties, it gets really challenging when 1 item holds 3 variables, second 6 and 3rd item 3 other variables. But they technically same type, i am actually building this kind of system right now for my mmo project, and i can tell its hard, but interesting. And i found a very optimized solution to build it without defining everything for every object.

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...