Answer the question
In order to leave comments, you need to log in
Can shared_ptr be used in games?
Good afternoon. I am writing a game in c++ and lua. There was a need to create an object in lua and pass it to pluses. The problem is that the garbage collector in lua destroys my object and it is no longer possible to use it in the plus code. The problem is solved through shared_ptr, but I don’t know how correct this solution is, because then I will have to rewrite everything to use this smart pointer in pluses. Hence the questions, is it possible to use shared_ptr in games, because the performance will drop compared to ordinary pointers, and are there any other solutions for transferring objects between lua and c ++?
Answer the question
In order to leave comments, you need to log in
Your question is so clumsily composed that it is not even clear how to answer at all.
1. The transfer of an object from one environment to another does not have to be solved by locking exactly the object that exists in the "world" of lua.
2. Even if you block the lua object, why are you going to do it with a shared_ptr?? Does it somehow fumble ownership with the lua memory manager? Haven't heard of this possibility. Those. I do not think that
3. Why rewrite everything to use shared_ptr?? do you want to start writing pluses like lua? even if your assumptions in paragraph 1 and paragraph 2 magically turned out to be true, shared_ptr and garbage collection are very different things, without understanding any of them to the end, you thought that this is the same thing because it "looks like ".
4.
because the performance will drop
Just keep a Lua variable with the value of the table (object) in some space so that the garbage collector doesn't delete it. There is also a lua_ref() function that can add any Lua value to the internal registry.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question