A basic example mod of using '
(xmlSetAttrib xml attrib value) -> value
We use xml functions to add a background image to a dockscreen.
There are two dockscreens in the attached mod. They are the same dockscreen but we will add the XML attribute
Code: Select all
backgroundID="&rsPointJunoHero;"
The xml code is run in an <OnGlobalTypesInit> event. This mod uses a <Type> UNID for this event and also uses <OnGlobalPaneInit> to add some dockscreen actions so we can see what the code has done.
The code we are modifying is the XML attributes. In this screen they are 'UNID' and 'name'. We will add 'backgroundID'.
The XML attributes of a UNID can be seen by using the function 'xmlGetAttribList'.
Start a game in debug mode with the attached mod selected.
In the debug screen type:
Code: Select all
(xmlGetAttribList (typGetXML 0xD7890001))
Code: Select all
(name unid)
The attributes of the second dockscreen '&dsD789XMLAddAttrib;' are the same as it is a copy of the same dockscreen but called by a different name.
Now to the code.
In <OnGlobalTypesInit> we get the xml of the second dockscreen '&dsD789XMLAddAttrib;' using 'typGetXML' and call this XML 'theScreenXML'. This is all the code from this UNID. It starts at '<DockScreen UNID=...' and finishes at '</Dockscreen>'.
We then use 'xmlSetAttrib' to add the new backgroundID XML.
Code: Select all
(xmlSetAttrib theScreenXML 'backgroundID "&rsPointJunoHero;")
So the end result will be a line of XML code 'backgroundID="&rsPointJunoHero;"' being added to 'theScreenXML'. We don't need to add the '=', the code does this automatically.
The last line of code uses 'typCreate' to save the changed XML into the dockscreen UNID '&dsD789XMLAddAttrib;'.
This dockscreen has now been changed to include the background ID code. So the dockscreen now shows the Point Juno image instead of the default playership image.
We can see this by selecting the added actions in the Ship's Interior dockscreen.
In the debug screen type
Code: Select all
(xmlGetAttribList (typGetXML 0xD7890002))
Press 'Enter' and the debug screen will print
Code: Select all
(name unid backgroundID)
To check the value of 'backgroundID' we use this code
Code: Select all
(xmlGetAttrib (typGetXML 0xD7890002) 'backgroundID)
Code: Select all
1048672
Code: Select all
(unvEntity 1048672)
In this example we added an XML attribute. 'xmlSetAttrib' can also be used to change existing attributes. So we could alter the name of these dockscreens or change values of attributes in other UNIDs.