Adding an XML attribute

This is a moderated forum that collects tutorials, guides, and references for creating Transcendence extensions and scripts.
Post Reply
relanat
Militia Captain
Militia Captain
Posts: 941
Joined: Tue Nov 05, 2013 9:56 am

1.8b2
A basic example mod of using 'xmlAddAttrib'. 'xmlSetAttrib'. (Apologies: I messed up the function name)
(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;"
to the second one "XML Attrib Added Dockscreen" so it shows the Point Juno image instead of the default playership image.

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))
Then press 'Enter' and the debug screen will print

Code: Select all

(name unid)
These are the attributes of the first dockscreen '&dsD789XMLStandardScreen;' (0xD7890001 is the hexadecimal equivalent of '&dsD789XMLStandardScreen' as can be seen in the ENTITY code at the beginning of the mod), named "Standard Example Dockscreen". They can be seen in the first two lines of the dockscreen code, 'UNID=' and 'name='.
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;")
This code adds an attribute called 'backgroundID' to 'theScreenXML'. This attribute has the value of "&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))
This shows us a list of the attributes for the second dockscreen which we just altered.
Press 'Enter' and the debug screen will print

Code: Select all

(name unid backgroundID)
which shows the added attribute 'backgroundID'.

To check the value of 'backgroundID' we use this code

Code: Select all

(xmlGetAttrib (typGetXML 0xD7890002) 'backgroundID)
which gives

Code: Select all

1048672
This is the decimal equivalent of '&rsPointJunoHero;' which we can check by typing

Code: Select all

(unvEntity 1048672)
into the debug screen. Pressing Enter will show 'rsPointJunoHero'.

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.
Attachments
XMLAddAttrib.xml
(3.49 KiB) Downloaded 311 times
Stupid code. Do what I want, not what I typed in!
Post Reply