C
C#2y ago
Fremko

Store different structs in a list?

I have a world that have different types of items, each of these items have data and each type of item has his own struct, i am trying to loop through all the items in a world, but i dont understand how can i store different types of struct in a list. like i cant add door struct in a window struct list, and if i make List<object> I would defeat the whole point of using struct to maintain efficiency as this would be stored on the heap
16 Replies
Chiyoko_S
Chiyoko_S2y ago
struct is not a magical way to save allocations
pip
pip2y ago
Matt Watson
Stackify
Why Premature Optimization Is the Root of All Evil
The performance and scalability of your application are important. Understand what to avoid and what to prioritize when building your app.
Chiyoko_S
Chiyoko_S2y ago
if you are storing structs in a list, you already have them in the heap and you don't get to use free things like inheritance while being very limited in terms of what you do
trunksvn
trunksvn2y ago
i'll let Aoba cook
Chiyoko_S
Chiyoko_S2y ago
also. structs shouldn't generally be used if it's bigger than something like 16 bytes; being by-val means the CPU has to do more work copying the whole struct around if it has to be assigned to a variable / passed as a parameter
Fremko
FremkoOP2y ago
lol i feel dumb do you recommend using List<object>? cant i use ref keyword?
Chiyoko_S
Chiyoko_S2y ago
nah, not raw objects try coming up with a way to figure out what kind of things are shared and could be represented with inheritance or something or a dictionary or something
Fremko
FremkoOP2y ago
how to store such a json object?
{ "items": {"Pos 10 50" : {"class":"BrokenDoor", "id":115, "itemtype":13, "animations": true, "direction":0, "sprite":true}, mote items...}}
{ "items": {"Pos 10 50" : {"class":"BrokenDoor", "id":115, "itemtype":13, "animations": true, "direction":0, "sprite":true}, mote items...}}
should i use classes then?
Chiyoko_S
Chiyoko_S2y ago
it's hard to tell with just that. But for me it's too late to take a look at the whole thing >.> Don't use structs unless you know what you are doing classes are fine
pip
pip2y ago
unless we're working with absurdly large data sets where these kinds of optimizations would start mattering, not even worth the thought in my opinion
Chiyoko_S
Chiyoko_S2y ago
ref is something you use to work around the whole copying problem, but I believe it still is a wrong approach to be used here
Fremko
FremkoOP2y ago
if i use classes i still end up with the same problem i still have to put them in a list
Chiyoko_S
Chiyoko_S2y ago
at least you now have inheritances
Fremko
FremkoOP2y ago
should i create an interface?
Chiyoko_S
Chiyoko_S2y ago
define a base item type that has all the properties all items have names, ids, that kind of stuffs then if there are properties that only certain types have then you can define properties for that in a derived type ....obviously there are times where this isn't a viable solution because the structures are very dynamic, but it is still way better than having an object that you'll need to write thousands of if-else / switch statements for
Fremko
FremkoOP2y ago
ah ig i will use this approach thanks 👍
Want results from more Discord servers?
Add your server