Difference between revisions of "XmlNodeGetAttribute"

From Multi Theft Auto: Wiki
m (Returns: added default return value if attribute does not exist)
Line 13: Line 13:
Returns the attribute in string form.
Returns the attribute in string form or ''false'', if the attribute is not defined.

Revision as of 18:16, 25 March 2010

This function is used to return an attribute of a node in a configuration file.


string xmlNodeGetAttribute ( xmlnode node, string name )             

Required Arguments

  • node: The node from which you wish to return the attribute
  • name: The name of the attribute.


Returns the attribute in string form or false, if the attribute is not defined.


Suppose we have a gametype where only one type of car is used, and this type should not depend on the map but rather be set in an external configuration file and be used in all maps. Here's an example where the configuration file is an XML document:


<car model="528" posX="123.4" posY="456.7" posZ="12.3" rot="90.0" />

Lua code

local xml = getResourceConfig("settings.xml")      -- load XML file and get its root element
local carmodel = xmlNodeGetAttribute(xml, "model")    -- get attribute of root element
local carX = xmlNodeGetAttribute(xml, "posX")
local carY = xmlNodeGetAttribute(xml, "posY")
local carZ = xmlNodeGetAttribute(xml, "posZ")
local carA = xmlNodeGetAttribute(xml, "rot")
createVehicle(carmodel, carX, carY, carZ, 0.0, 0.0, carA)

See Also