GetResourceConfig: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(updated to new 1.0 syntax)
Line 5: Line 5:
==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
xmlnode getResourceConfig ( [resource theResource = getThisResource()], string filename )
xmlnode getResourceConfig ( string filePath )
</syntaxhighlight>  
</syntaxhighlight>  


===Required Arguments===  
===Required Arguments===  
*'''filename:''' the exact file name of the configuration file. (e.g. "file.xml" )
*'''filePath:''' the path of the file in the following format: '''":resourceName/path"'''. 'resourceName' is the name of the resource the file is in, and 'path' is the path from the root directory of the resource to the file.
 
:For example, if there is a file named 'settings.xml' in the resource 'ctf', it can be accessed from another resource this way: ''getResourceConfig(":ctf/settings.xml")''.
===Optional Arguments===
:If the file is in the current resource, only the file path is necessary, e.g. ''getResourceConfig("settings.xml")''.
*'''theResource:''' the resource to retrieve the configuration file from. Note: the resource has to be running, otherwise this function will fail.


===Returns===
===Returns===
Line 22: Line 21:
function resourceStart ( theResource )                        -- When a resource is started
function resourceStart ( theResource )                        -- When a resource is started
     if ( theResource == getThisResource () ) then              -- and the resource is this one
     if ( theResource == getThisResource () ) then              -- and the resource is this one
         node = getResourceConfig( theResource, "config.xml" )  -- get the configuration file
         node = getResourceConfig( ":" .. getResourceName(theResource) .. "/config.xml" )  -- get the configuration file
         local subNode = xmlFindSubNode( node, "group", 1 )      -- get a subnode in it
         local subNode = xmlFindSubNode( node, "group", 1 )      -- get a subnode in it
         outputChatBox( xmlNodeGetAttribute( subNode, "attr" ) )    -- output its attributes value to chatbox
         outputChatBox( xmlNodeGetAttribute( subNode, "attr" ) )    -- output its attributes value to chatbox

Revision as of 02:30, 30 July 2009

This function is used to return the root node of a configuration file. Config files must be predefined in a resource's meta file. An alternative way to load XML files is to use xmlLoadFile.

Syntax

xmlnode getResourceConfig ( string filePath )

Required Arguments

  • filePath: the path of the file in the following format: ":resourceName/path". 'resourceName' is the name of the resource the file is in, and 'path' is the path from the root directory of the resource to the file.
For example, if there is a file named 'settings.xml' in the resource 'ctf', it can be accessed from another resource this way: getResourceConfig(":ctf/settings.xml").
If the file is in the current resource, only the file path is necessary, e.g. getResourceConfig("settings.xml").

Returns

Returns the root node of the specified configuration file. If the file is corrupted, not defined in the meta file or doesn't exist, returns false.

Example

This example opens a configuration file and prints the value of the 'attr' attribute of the first 'group' node.

function resourceStart ( theResource )                         -- When a resource is started
    if ( theResource == getThisResource () ) then              -- and the resource is this one
        node = getResourceConfig( ":" .. getResourceName(theResource) .. "/config.xml" )  -- get the configuration file
        local subNode = xmlFindSubNode( node, "group", 1 )      -- get a subnode in it
        outputChatBox( xmlNodeGetAttribute( subNode, "attr" ) )    -- output its attributes value to chatbox
    end
end
addEventHandler ( "onResourceStart", getRootElement(), resourceStart )

See Also