lua_utils icon indicating copy to clipboard operation
lua_utils copied to clipboard

Warning when creating new class object with a player element

Open ghost opened this issue 9 years ago • 3 comments

Whenever I try to create a new class object with enew of a player element, it throws a warning to the debug log, saying that onElementDestroy event is already being handled by the function, how is this possible? MTA restricts listening to certain player events?

enew( getPlayerFromName( 'Socialz' ), classes.player )

WARNING: classlib.lua:59: Bad usage @ 'addEventHandler' ['onElementDestroy' with this function is already handled]

I added getElementType(element) ~= 'player' to my version, but if I understand it right, we should never ignore this event as it deletes stuff from memory and if it's never deleted we have a memory leak.

Using onPlayerQuit instead of onElementDestroy did not resolve the warning.

ghost avatar Apr 09 '16 21:04 ghost

Okay, I had to change my logic a little bit.

player = enew( player, Classes.Player )
player:spawn( 0, 0, 0, 0 )
player:fadeCamera( true )
player:setCameraTarget( player )

Works only if you spawn after you have created the class object with enew for the player.

If you spawn before, it will throw warnings. Order is very critical!

ghost avatar Apr 09 '16 23:04 ghost

The underlying issue is that by accessing any property of an element, you call an implicit enew. https://github.com/sbx320/lua_utils/blob/master/classlib.lua#L309

Therefore with your enew after :spawn there's an additional onElementDestroy handler attached to the element, causing the warning. I think we should change enew to avoid creating a new table entry if there's already an entry for that element.

In general I'd suggest you use registerElementClass like this: registerElementClass("player", Classes.player)

This automatically makes all player elements become a Classes.player upon first access of any property.

sbx320 avatar Apr 09 '16 23:04 sbx320

I see, thank you!

ghost avatar Apr 10 '16 10:04 ghost