Read 2D array from XML

Voyhkah

Undead
Joined
Apr 25, 2009
Messages
1,444
Location
Earth
I am trying to read the following tags from XML:

Code:
<CivicCommerceChanges>
				<CivicCommerceChange>
					<Civic>CIVIC_PAGANISM</Civic>
					<Commerces>
						<iCommerce>0</iCommerce>
						<iCommerce>0</iCommerce>
						<iCommerce>2</iCommerce>
						<iCommerce>0</iCommerce>
					</Commerces>
				</CivicCommerceChange>
			</CivicCommerceChanges>

How do I do it? I can handle the functionality after I get the data into the DLL.

Also, how does one read/write such an array?
 
Unless I am missing something important, that tutorial doesn't even MENTION 2D arrays.
 
Unless I am missing something important, that tutorial doesn't even MENTION 2D arrays.
It does not mention it directly but it decribes the functions you need.
In the end you need two nested for loops (or one for loop and the SetCommerce method) to read it. Consider the XML as a tree and use the methods provided to traverse it.

For the reading/writing into a stream (serialization) write the number of objects in the array before you write the array. Since your inner array always has the same number of objects (the number of commerce types), you don't even need to write the number there.
 
Take a look at the source code of my custom DLL. look for a function called SetVariableListTagPairForYields. It almost does what you want, except it does it for yields instead of commerce.

It uses SetYields() internally, so you might be able to simply swap that for SetCommerce, although I'm not completely sure these methods do the same.
 
Unless I am missing something important, that tutorial doesn't even MENTION 2D arrays.

Whoops. It's been a while since I read the tutorial, and since I knew he covered arrays themselves, I thought he also touched on 2D arrays. Sorry about that. :blush:
 
How do I read and write it from savegames?
 
Never mind, I got it.
 
I got a crash. When I debugged it, I found that the array was never initialized. Any suggestions on why? Asaf, the code I used to read the XML a modded version of some in your custom DLL.
 
You might have not copied the allocation code of the array. Look for the original variable name from which you copied and find lines with 'new' in them (and complete these with SAFE_DELETE or delete).
 
Like this?

Code:
SAFE_DELETE_ARRAY(m_ppiCivicCommerceChanges)
	m_ppiCivicCommerceChanges = new int*[GC.getNumCivicInfos()];
	for(i = 0; i < GC.getNumCivicInfos(); i++)
	{
		m_ppiCivicCommerceChanges[i]  = new int[NUM_COMMERCE_TYPES];
		stream->Read(NUM_COMMERCE_TYPES, m_ppiCivicCommerceChanges[i]);
	}
 
For example, yes. I'm not sure it's everything, but just see if it works.

You also need another
Code:
SAFE_DELETE_ARRAY(m_ppiCivicCommerceChanges)

When destroying the info object, to prevent memory leaks.
 
It's what I already have. But this code is not called when the game starts. I checked. When are the variables normally initialized?
 
Like this?

Code:
SAFE_DELETE_ARRAY(m_ppiCivicCommerceChanges)
	m_ppiCivicCommerceChanges = new int*[GC.getNumCivicInfos()];
	for(i = 0; i < GC.getNumCivicInfos(); i++)
	{
		m_ppiCivicCommerceChanges[i]  = new int[NUM_COMMERCE_TYPES];
		stream->Read(NUM_COMMERCE_TYPES, m_ppiCivicCommerceChanges[i]);
	}

I'm assuming this is in the read() function for whatever class this belongs to? Are the other variables in this class initialized?
 
Of course it's in Read(). And yes, the other variables are initialized. But strangely, when I set a breaktrap in this code, it was never tripped. This code is not executed when the game starts.
 
OH GOD PLEASE HELP!

The Member Variables for CvBuildingInfo are not allocated by read(FDataStreamBase* stream) (From Savegame) when the game first loads because the game does not call that method. Where are they allocated, and why isn't this happening for my array?
 
I believe it's allocated in read(CvXMLLoadUtility* pXML). Try looking at m_ppaiSpecialistYieldChange, which appears to be a 2D array.
 
read(CvXMLLoadUtility* pXML) is used when loading the info from XML.
I haven't seen read(FDataStreamBase* stream) called, but I'm guessing it's related to the cache, so you should update this (and write()) as well.
 
I updated all three BUT IT'S NOT WORKING!!

AAARG!

(Sorry, but it's been around two weeks since I could last mod.)
 
Back
Top Bottom