themehaa.blogg.se

Lua table insert in class
Lua table insert in class







lua table insert in class

Shared pointers also don’t solve the problem of moving entities in memory: you can’t swap what’s inside the shared_ptr and have all weak_ptrs automatically update their pointers. And after all, it’s better for the lifetime of entities not to be shared and that semantic is better expressed with unique_ptr. First of all, it requires you to create all your entities as shared_ptrs which may have some significant overhead compared to unique_ptrs. After that you can use std::weak_ptr::expired function to check if the reference is still valid.

lua table insert in class

After that you’ll create all references to it with std::weak_ptrs which won’t affect your entity’s lifetime. First, you create your entities as with std::make_shared. Some of the problems with raw references can be solved with std::shared_ptr and std::weak_ptr. The latest solution (about storing references to Lua) was discovered by me not long ago and it is the point of me writing the article, but I want to show some other ways of solving the problem.

lua table insert in class

Let’s see all these problems can be solved. While it’s possible to manually update all pointers to previously referenced entities, it’s certainly will be better to do so automatically and without a chance of forgetting some pointers which will be stale. Some entities may start to occupy different addresses in memory (for example, after you reload a level and decide to create all entities from scratch). And with good design you’ll probably be able to achieve that.īut stale pointers/references give some of the worst bugs, which are difficult to track, crash your game and may not be easily identifiable. But the problem with raw pointers and references is that once you use them, you have to make sure that the entity which is being referenced stays alive and is not moved in memory without notifying objects which hold references.

lua table insert in class

Sometimes a simple pointer, reference or std::reference_wrapper is enough. Sometimes it’s child-parent relationship between the entities, sometimes it’s useful to store a reference to an object in event data, some task scheduling class and so on. The problem of handling references to game objects (I’ll call them entities) comes up very often.









Lua table insert in class