(another) Stupid Question... Or [Fun with IF Statements!]

Everything else that doesn't fall into one of the other PB categories.
Zach
Addict
Addict
Posts: 1656
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

(another) Stupid Question... Or [Fun with IF Statements!]

Post by Zach »

Hi,

This is me being curious again, about a behavior I observed while writing and testing some code.
I am just about finished writing another XML parsing routine, to load another section of data from my XML database and something I like to do as I code is test in very small increments..

For example, I write a few lines to grab an XML node pointer, and then debug some data about it (node path, node name, node data, etc) and then continue on if its working..
So in the process of writing this, I was writing a nested While loop setup (2 loops), and mistakenly I put my debug statement inside the loops; Inside one of the loops there is an IF/ENDIF block of logic that I use to identify the node I am in and this is where I noticed the weird behavior..

Basically the loop tests for a node, if its not there, it tests for the other. I probably lack the experience to know a more elegant solution, but in this case both IF conditions will eventually be true and code will be executed, grabbing element values and assigning them to variables.

Here is the section, for reference:

Code: Select all

Procedure Load_AreaMap()
  ;// Load the Area Map for matching Area location strings
  ;// to room address coordinates, to be printed as part of
  ;// the room title in the game.
  
  nodepath$ = "/GameDatabase/AreaMap/"                    ;// Define the node path to search for Subnodes from.
  *MainNode = MainXMLNode(XML_File)                       ;// Establish the root XML node of the database XML file.
  *MainNode = XMLNodeFromPath(*MainNode, nodepath$)       ;// Set the node path to search for Area Names in the Database from.
  Debug XMLNodePath(*MainNode)                            ;// Echo the Node Path to the Debug Window for verification.
  
  *ChildNode  = ChildXMLNode(*MainNode)
  Debug GetXMLNodeName(*ChildNode)
  
  While *ChildNode <> 0                                   ;// Do stuff every time we fine a <Area> XML Element.
    *SubNode = ChildXMLNode(*ChildNode)                   ;// Navigating down to the <Name> Element we need to load.
    
    While *SubNode <> 0                                   ;// Still more navigating.
      CurrentNode.s = GetXMLNodeName(*SubNode)            ;// Get the name of the current Node we're in.
      If CurrentNode = "id"                               ;// Once we have the Node we need...
        AreaID.s = GetXMLNodeText(*SubNode)               ;// Area ID doubles as Map Key
      
      
      ElseIf CurrentNode = "Name"                         ;// Get the next node we need.
        AreaName.s = GetXMLNodeText(*SubNode)             ;// Get the name of the Area we are assigning to the Area ID.
      EndIf
      
       
      
      *SubNode = NextXMLNode(*SubNode)  
    ;Debug "Area ID: #" + AreaID + " = " + AreaName
    Wend
    *ChildNode = NextXMLNode(*ChildNode)
    
    AreaMap(AreaID) = AreaName
  Wend
  ForEach AreaMap()
    Debug "Area Name is: " + AreaMap() + ", and it is Area ID#: " +MapKey(AreaMap())
  Next
  
EndProcedure
As you can see, I left the original mistake Debug line in, but commented it out. Replacing it with a ForEach in the proper spot outside of the loop. It basically just tells me the values of the Map Key:Value pair, so I can verify they were assigned correctly from my test data in the XML file. If the original were uncommented, I would get funny readouts where the debug repeats twice for each Key:Value pair. And this is where I noticed something funny.. :lol:

Code: Select all

/GameDatabase/AreaMap
Area
Area ID: #1 = 
Area ID: #1 = Kirkhill
Area ID: #2 = Kirkhill
Area ID: #2 = The Sleeping Forest
Area ID: #3 = The Sleeping Forest
Area ID: #3 = Secret Base
Area Name is: Kirkhill, and it is Area ID#: 1
Area Name is: The Sleeping Forest, and it is Area ID#: 2
Area Name is: Secret Base, and it is Area ID#: 3
I was wondering, does this indicate there is something "wrong" in my logic or is it simply an artifact created by the fact my improperly placed debug code was executing every loop? I thought it was strange that the value of the previous loop is assigned to the next result even though the "final" result is assigned correctly..

Anyone else ever done something stupid like this? :oops:
Image
User avatar
spikey
Enthusiast
Enthusiast
Posts: 586
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: (another) Stupid Question... Or [Fun with IF Statements!

Post by spikey »

Joseph Wood Krutch wrote: Logic is the art of going wrong with confidence.
All the time. That's why good debugging tools are vital. I tried out another 'alternative' language a few years ago before I happened across PureBasic - but eventually dropped it because its debugging tools were inadequate, which was a shame because otherwise it had many good features.

You didn't show the XML which gives rise to this behaviour but I'd guess its caused by another node in the XML tree which isn't either 'id' or 'name' giving rise to the initial blank. The next lines are 'half out of sync' because the data only makes sense once a node pair has been processed.

The output is unaffected because you only action these two nodes and you only action them once you have looped through all of the 'subnodes'.

Bear in mind that malformed input XML would cause a similar bug at runtime though. So I suppose there is something wrong with your code in the sense that it isn't robust enough to cope with potentially malformed input XML. Depends how likely this is to occur... That's why large scale XML based projects will utilize XML schema enforcement.
Zach
Addict
Addict
Posts: 1656
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: (another) Stupid Question... Or [Fun with IF Statements!

Post by Zach »

Oh yeah, I already know from experienced typos in my XML will screw things up royally.

I chose not to put in error handling because this is a temporary database system. Once development gets further along, I am going to replace it with SQLite stuff.

edit: Here is the XML file

Code: Select all

<?xml version="1.0"?>
<GameDatabase>

<AreaMap>

<Area>
	<id>1</id>
	<Name>Kirkhill</Name>
</Area>

<Area>
	<id>2</id>
	<Name>The Sleeping Forest</Name>
</Area>

<Area>
	<id>3</id>
	<Name>Secret Base</Name>
</Area>

</AreaMap>

<GameMap>
	<Room>
		<id>1,5,3</id>
		<room_title>Town Center</room_title>
		<room_description>
		The center of town is filled with the quiet hustle and bustle of people going about their daily routines. The square is full of merchants, their carts lining the streets neatly in rows, as they show off various goods for sale, enticing customers with slogans and special prices as they browse near. In the center of the square stands a tall oak tree, next to it a human-sized statue of the man who planted it, the founder of the town, Erick Darkan. There are several benches close to the tree, full of mindful parents who glance up ocassionally to check on the small children playing about the square, creating a small park-like atmosphere.
		</room_description>
		<exits>
			<nw>none</nw>
			<n>1,5,2</n>
			<ne>none</ne>
			<e>none</e>
			<se>none</se>
			<s>1,5,4</s>
			<sw>none</sw>
			<w>none</w>
			<up>none</up>
			<down>none</down>
			<out>none</out>
		</exits>
	</Room>

	<Room>
		<id>1,5,2</id>
		<room_title>Darbo Street</room_title>
		<room_description>
		The street dead-ends to the north, leaving you only three options of where to go.  To the west you see that
		the road continues briefly until it reaches another dead end, but the way east appears to be open for quite
		some distance.  From the east you hear the familiar sounds of men shouting orders accompanied by various grunts
		and acknowledgements, the stamping of boots in unison marching creates an ominous sound that can be heard well
		far off from its origin.  One could easily conclude that they are soldiers from the East Battery, busy with their
		daily training. To the south, the familiar sounds of the Town Square echo in your ears.
		</room_description>
		<exits>
			<nw>none</nw>
			<n>none</n>
			<ne>none</ne>
			<e>none</e>
			<se>none</se>
			<s>1,5,3</s>
			<sw>none</sw>
			<w>1,4,2</w>
			<up>none</up>
			<down>none</down>
			<out>none</out>
		</exits>
	</Room>
	
	<Room>
		<id>1,4,2</id>
		<room_title>Darbo Street West</room_title>
		<room_description>
		As you continue westward down the street you notice the road begin to deteriorate. Patches of already broken cobblestone continue to further disintegrate under the weight of your feet, producing an odd crunch-like sound that reminds you of something you can't quite put your finger on.
		</room_description>
		<exits>
			<nw>none</nw>
			<n>none</n>
			<ne>none</ne>
			<e>1,5,2</e>
			<se>none</se>
			<s>none</s>
			<sw>none</sw>
			<w>1,3,2</w>
			<up>none</up>
			<down>none</down>
			<out>none</out>
		</exits>
	</Room>
	
	<Room>
		<id>1,3,2</id>
		<room_title>Darbo Street West</room_title>
		<room_description>
		The street comes to an abrupt halt and soon you find a dirt and grass trail, picking up where the cobblestone left off, guiding you in more of a northernly direction.  Eventually you come to stand before a fixed up old building, which appears to be operating as a makeshift clinic of some sort.  The building itself looks weathered and run-down, but you still see signs that it was once a more prestigious landmark for the area.  You notice out of the corner of your eye a dilapidated sign with faded writing which reads "C..nt.r.l ..ank", over which new words have been scrawled, "Doctor".  Whomever occupies the place now, they sure aren't in the money-lending business.
		</room_description>
		<exits>
			<nw>none</nw>
			<n>none</n>
			<ne>none</ne>
			<e>1,4,2</e>
			<se>none</se>
			<s>none</s>
			<sw>none</sw>
			<w>none</w>
			<up>none</up>
			<down>none</down>
			<out>none</out>
		</exits>
	</Room>
	
	<Room>
		<id>1,6,2</id>
		<room_title>Darbo Street East, Outside the Bank</room_title>
		<room_description>
		As you move east down the street you come to a pause outside a large building.  Looking up you spot a large sign, indicating this building as the town bank.  All manner of people come and go from the large oak doors, which are currently open and provide a brief glimpse into a large, brightly lit lobby.
		</room_description>
		<exits>
			<nw>none</nw>
			<n>none</n>
			<ne>none</ne>
			<e>1,7,2</e>
			<se>none</se>
			<s>none</s>
			<sw>none</sw>
			<w>1,5,2</w>
			<up>none</up>
			<down>none</down>
			<out>none</out>
		</exits>
	</Room>
	
</GameMap>
</GameDatabase>
Image
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: (another) Stupid Question... Or [Fun with IF Statements!

Post by PMV »

Zach wrote:[..] or is it simply an artifact created by the fact my improperly placed debug code was executing every loop? [...]
Thats it. :wink:
The values of the (temp)variables will hold the last assigned value until it is
changed. As there are 2 nodes/ values to read, it will take 2 loops to have
the right debug-line. In between it is just a snapshot of the program-state,
but nothing to work with and of course nothing to worry. :lol:

Parsing XML and debugging it at the right line is a little bit harder as you will
always go a few times through a loop. When you put a comment in to
your <area> nodes, you will even have another line that is identical with
the line before. :wink:
Thats why you have always to check for the notetype. :)

Code: Select all

If XMLNodeType(*Node) = #PB_XML_Normal
Try it out. :wink:

MFG PMV
Zach
Addict
Addict
Posts: 1656
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: (another) Stupid Question... Or [Fun with IF Statements!

Post by Zach »

Thanks for the tip
Image
Post Reply