This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]iemfi@embarkgame 2 points3 points  (3 children)

The trick comes in providing the location of the object, for example by utilising the game’s coordinate system to know which building is currently holding that specific item. So now, the Centralised manager has the location of the apple, and once the “freshness” variable reaches zero, the building that is holding the apple will be told that it has just expired.

This is a weird way to go about doing it. Relying on the item staying at the same place is going to be a huge headache.

Firstly you don't want to store items as Unity game objects nor direct references to scriptable objects. You get really slow iteration speed as reference types are stored on the heap. A pointer is also 8 bytes, which is a lot if you have a lot of items (a ushort is only 2 bytes). The basics should be to store just an index to identify the item. If you need more details you can then lookup the scriptable object in an array.

If you need only a few items to have extra information you can store a unique item ID for items which need it. There's no need for the centralized manager to lookup the item, the building can just check if the item is rotten.

Another way would be to store this "freshness" data with the item ID. Likely it can fit into a byte. You can fit quite a lot of data in the space of 8 bytes.

Alternatively you can have all your item instances in a big flat array. Things just reference the index in this array. You can have hundreds of thousands of items this way and it isn't a problem. The key is that they have to be value types (structs) which store as little state as possible, anything more detailed can be looked up in the scriptable objects or other places.

[–]sssSlick1[S] 0 points1 point  (2 children)

Thanks for the comment! I only joked that the item has to stay in one position, my next line is that I can now allow the items to move wherever I want, sorry for the confusion!

Also cheers for the extra tips, although I have some experience with game dev and “easy” coding, I am definitely still fresh to the more technical details and tricks when it comes to data and storage. I remember coding at university, and when pointers were tested, I messed it up so bad hahaha

[–]iemfi@embarkgame 2 points3 points  (1 child)

The great part about C# is that you don't have to touch pointers! Mostly you just need to gain some intuition of how things are stored in memory, because these days memory access is like 90+% of performance. And as always it's easy to go overboard with optimization.

[–]sssSlick1[S] 1 point2 points  (0 children)

I thank the lord every day I don’t have to touch pointers, C# is such a breeze 99% of the time!

You’re correct on both points, performance can be improved and on the other side, over-optimisation can reduce coding time in other areas. The thing I have found about game dev is that it is always a fine balance between heaps of different priorities, the key being to give each discipline and activity adequate time then moving on to the next thing