Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warning when creating new class object with a player element #5

Open
ghost opened this issue Apr 9, 2016 · 3 comments
Open

Warning when creating new class object with a player element #5

ghost opened this issue Apr 9, 2016 · 3 comments

Comments

@ghost
Copy link

ghost commented Apr 9, 2016

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
Copy link
Author

ghost commented Apr 9, 2016

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 ghost closed this as completed Apr 9, 2016
@sbx320
Copy link
Owner

sbx320 commented Apr 9, 2016

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 sbx320 reopened this Apr 9, 2016
@ghost
Copy link
Author

ghost commented Apr 10, 2016

I see, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant