C++ veteran needs help to understand some Rust concepts
Hi crab people!
After many years of C++ I want to really get used to rust and also understand how to really approach some problems in a rust way - and write a small game to learn.
What I would do in C++: Create a Game Object class that everything in the game inherits from and that has some common update etc methods. Then have a game controller class that has a data structure of all the game objects.
Now I can use pointers in the code to reference these object, e.g. I'm a unit has a target, I can store the pointer to the target game object in the unit game object. Or I represent my level as a tilemap and store at every position e.g. the pointer to the enemy or power up or whatever at this tile.
This means the controller "owns" the object, but this is something I have to do myself. As far as I understand, a rust object has only one owner and just referencing by a pointer that is saved in a class equivalent is not possible. Or possible when using unsafe, but not really the way to go in rust (I feel like I just would be writing C with extra steps)
I thought about using ids and then querying the controller for the object with this I'd, but that seems also not perfect. Either I have an array of objects that grows larger and larger the longer the game takes, (objects can be destroyed and deleted, but the list would be ever growing...) or I take something like a map and actually remove deleted entries, but then the lookup will be inefficient.
However, I am pretty sure that there is a clever way how to do this in rust that I am just too C++ blind to see. Maybe I am looking at the whole problem from a wrong angle. So, how would you tackle structures like this from a rust perspective?