OnResourceStart: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(17 intermediate revisions by 11 users not shown)
Line 1: Line 1:
This event is triggered when a resource is loaded.
{{Server event}}
__NOTOC__
This event is triggered when a resource is started.


'''Important:''' It is important to note that this event is called when any resource starts, not just the resource your script is running inside. As such, most of the time you will want to check that the name of the resource passed to this event matches the name returned by [[getThisResource]] before doing anything.
'''Important:''' If you attach this event to the root element it will called when ''any'' resource starts, not just the resource your script is running inside. As such, most of the time you will want to check that the resource passed to this event matches your resource (compare with the value returned by [[getThisResource]] before doing anything. Alternatively you can attach the event to resourceRoot.


==Syntax==  
==Parameters==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
void onResourceStart ( string name )
resource startedResource
</syntaxhighlight>  
</syntaxhighlight>  


==Variables==
*'''startedResource''': the [[resource]] that was started.
* The source of this event refers to the loaded resource.
 
*'''name''': A string representing the name of the resource
==Source==
The [[event system#Event source|source]] of this event is the root [[element]] in the resource that started.
 
==Cancel effect==
If this event is [[Event system#Canceling|canceled]], the resource starting is aborted and is stopped again.


==Example==
==Example==
This example displays a message in the textbox when you load a map containing this lua
<section name="Example 1" class="server" show="true">
This code will output the name of any resource that is started.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addEventHandler ( "onResourceStart", getRootElement(), "mapLoadChatBoxOutput" )
function displayLoadedRes ( res )
function mapLoadChatBoxOutput ( name )
outputChatBox ( "Resource " .. getResourceName(res) .. " loaded", root, 255, 255, 255 )
outputChatBox ( "Map "..name.." Loaded", root, 255, 255, 255 )
end
end
addEventHandler ( "onResourceStart", root, displayLoadedRes )
</syntaxhighlight>
</syntaxhighlight>
</section>


==See Also==
{{See also/Server event|Resource events}}
{{Event_functions}}

Latest revision as of 14:02, 2 June 2021

This event is triggered when a resource is started.

Important: If you attach this event to the root element it will called when any resource starts, not just the resource your script is running inside. As such, most of the time you will want to check that the resource passed to this event matches your resource (compare with the value returned by getThisResource before doing anything. Alternatively you can attach the event to resourceRoot.

Parameters

resource startedResource
  • startedResource: the resource that was started.

Source

The source of this event is the root element in the resource that started.

Cancel effect

If this event is canceled, the resource starting is aborted and is stopped again.

Example

Click to collapse [-]
Example 1

This code will output the name of any resource that is started.

function displayLoadedRes ( res )
	outputChatBox ( "Resource " .. getResourceName(res) .. " loaded", root, 255, 255, 255 )
end
addEventHandler ( "onResourceStart", root, displayLoadedRes )

See Also

Resource events


Event functions