Jump to content

event handlers not working after resource restart


itsHasaN

Recommended Posts

hi, i have an issue with custom made events, i added an event myself, now when the resource which has the addEvent in it get restarted, the other resources that had handlers to that events wont get called, i guess the event get recreated, and because of resources start order, the older handlers would not work anymore, now how can i avoid this? for example i made an onPlayerFirstSpawn event on my utilities resource, imagine i have to update it and i need to restart the resource, then the handlers will stop working on other resources!

Link to comment
  • Moderators
20 hours ago, itsHasaN said:

then the handlers will stop working on other resources!

When the resource starts a new resourceRoot element will be created. This element is destroyed when the resource stops.

It is therefore not a good idea to attach eventHandlers to the resourceRoot's of other resources. (Unless you keep track of start/stop resources)

Tre.png

 

In your current example you mentioned the eventName onPlayerFirstSpawn.

The player makes it's first spawn. From a semantic perspective, the player is therefore the one activating the event. And I expect it therefore to be used as the source of that event. The element above a player is the root element and should be used for the eventHandler of that event. The root element is not deleted when restarting resources.

But never the less, here is how to solve the resource validation issue:

addEventHandler("<eventName>", root, 
function ()
    local resource = getResourceFromName( "<resourceName>" )
    if not resource then return end
    
    if source == getResourceRootElement(resource) then
		-- The source is from the correct resource
    end
end)

 

Another way to keep the resource start up in order is to use the tag: <include resource="resourceName"/>

https://wiki.multitheftauto.com/wiki/Resources

 

 

 

 

 

 

  • Thanks 1
Link to comment
On 22/03/2023 at 01:19, itsHasaN said:

hi, i have an issue with custom made events, i added an event myself, now when the resource which has the addEvent in it get restarted, the other resources that had handlers to that events wont get called, i guess the event get recreated, and because of resources start order, the older handlers would not work anymore, now how can i avoid this? for example i made an onPlayerFirstSpawn event on my utilities resource, imagine i have to update it and i need to restart the resource, then the handlers will stop working on other resources!

add 'addEvent' in resources when used your 'onPlayerFirstSpawn'

  • Thanks 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...