XmlNodeSetAttribute: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
m (Fixed example)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Server client function}}
{{Server client function}}
{{Needs_Checking|Example outdated. xmlFindSubNode no longer exists.}}
This function is used to edit an attribute of a node in a configuration file.
This function is used to edit an attribute of a node in a configuration file.


Line 33: Line 32:


function changeConfigMarkerColor(thePlayer, command, r, g, b)
function changeConfigMarkerColor(thePlayer, command, r, g, b)
     local markernode = xmlFindSubNode(config, "markers", 0)
     local markernode = xmlFindChild(config, "markers", 0)
     xmlNodeSetAttribute(markernode, "color", r .. "," .. g .. "," .. b)
     xmlNodeSetAttribute(markernode, "color", r .. "," .. g .. "," .. b)
end
end

Revision as of 21:34, 16 November 2011

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

Syntax

bool xmlNodeSetAttribute ( xmlnode node, string name, string/float value )             

Required Arguments

  • node: The node of which you wish to edit an attribute.
  • name: The name of the attribute.
  • value: The value which you wish to change the attribute to.

Returns

Returns true if the attribute was set successfully, false if the node and/or attribute do not exist, or if they're faulty.

Example

Click to collapse [-]
Server

In a gamemode, we want a command to change the marker color in the configuration file.

config.xml:

<config>
    <markers color="255,100,0" />
</config>

Lua code:

config = xmlLoadFile("config.xml")

function changeConfigMarkerColor(thePlayer, command, r, g, b)
    local markernode = xmlFindChild(config, "markers", 0)
    xmlNodeSetAttribute(markernode, "color", r .. "," .. g .. "," .. b)
end
addCommandHandler("markercolor", changeConfigMarkerColor)

Note that this example only changes the in-memory XML document. If you want to save the changed document back to the hard drive, use xmlSaveFile.

See Also