#Item Slots
1 messages · Page 1 of 1 (latest)
Don't forget about kill your darlings, if there is a better way don't be scared to throw away old work
It does work. Java just has these things called wildcards which c# doesnt seem to have.
The problem is just the property/field, I can only delcare it as a specific generic type. In Java, you can do a wild card such that UsableSlot<?>, and it would take any type and of the derived to store it as. It's been a while but I remember it being pretty straight forward.
Ye I get what you mean, I'm not sure if there is something like it that does what you want it to, but I think using an interface could solve it
what is the functionality that you want all the items to have?
Well, they all derive from UsableObject, which at this base just has an ID/Name
and a Use(); function pure virtual
so downcasting it I would not be able to call the override function
ye you can just do that, but you want to also have the original function to run? in the class you inherit from?
Usable is technically just abstract, the reason why I've got this hierarchy overall is because my equip screen can have equippableItems, and those equippableItems can be stored in itemSlots, but not vice versa
Honestly I'm just saving myself a bunch of if statements doing this way lmao
also saves me from casting a bunch of stuff
Nah, Slots and Item Objects are separate
So you can't just have UsableObject as the class you reference and call the changes to the inherited classes from there?
using somthing like and interface or virtual void?
Yeah, technically I can do that. But, the problem isn't so much that I can't downcast and store it as UsableSlot<UsableObject>
It expects a type of UsableObject
and because of that, there's a type error even though it's a derived class, and it's giving me a null error
Technically, you use generics for when you dont really care what type it is, but in this instance I do care lol
I'm sorry I have been thinking a lot and can't come up with a solution for now, it's getting late for me 😅 , maybe give the question another try in #archived-code-advanced or #archived-code-general
I'll tell you if I come up with something
Aye, no problem. I should really upload some code but there's just way too much of it and friend currently has the git private.
You can just put any derived class in to it right without needing the <T> behind it, you can just have a variable of type UsableSlot and put a EquipmentSlot in to it if it is derived
Yep, but the overall problem is that you need to declare the type inside the generic parameters if you want to store it.
and so what I'm looking for is a way to say here, I want to store a slot of type UsableSlot, but it expects me to also give it the generic type params.
Why don't you just have a variable of type UsableSlot inside of the BaseSlot class?
And maybe add it in a constructor or just a function to assign it?
BaseSlot itself is just functions for the drag interfaces, but I may have to add a constructor somewhere, otherwise there seems to be a way to do what I want with interfaces I'm reading up on.
rather, https://stackoverflow.com/questions/116830/is-there-an-anonymous-generic-tag-in-c-like-in-java
lol
idk stuffs beyond me most of the time
ha ye, I honestly thing that the thing you're trying to achieve should be done way different in Unity C#, I myself am working with Unreal a lot and doing things there like I do in Unity bites me in the ass every day.
I'd honestly avoid all this if I could, but doing this way would help me not cast everything when I type comparison.
I would probably have a overhanging class like ItemManager or InventroyManager that includes a array or 2d array of all type ItemSlot, and give the ItemSlot the functionality to add or remove items, and just using simple checks if the item is allowed in the slot, and you can expand way more on that.
less code = more pretty work ;p
I don't think you need to cast anything
Then using 4 different classes for just different slot types would be more code in my opinion
I could avoid inheritance all together, sure. But currently I've only got one class that contains all eventdata interfaces which is a ton of code already.
It all really just comes down to the whole items can go into item slots, but equips can go into item slots AND equipmentslots
and all items/abilities can go into action bar slots
everything is reusable