Jump to content

problem with xmlFindChild


ThePope

Recommended Posts

Hello,

I have a problem that is starting to give me a headache. I'm learing Lua Scripting, so I took some scripts as examples and tried to build a carshop.

Basic idea seems simple to me, list al the cars from an xml file and then, when a player clicks buy, compare his/her money to the costs and spawn the car.

The shops are created without a fuss, also from an xml file. But when I do so with the cars, it seems to think there are no entries in the xml file?!.

Here is my a piece of my xml file:

<cars>
<car id="602" price="15000" />
<car id="496" price="15000" />
<car id="401" price="15000" />
<car id="518" price="15000" />
<car id="527" price="15000" />
<car id="589" price="15000" />
<car id="419" price="15000" />
<car id="533" price="15000" />
<car id="526" price="15000" />
       </cars>

and this is the list function:

outputChatBox( "Client: Creating car grid" )
 
oCarList   = guiCreateGridList ( 0.03 , 0.07, 0.4, 0.8, true, oShopGUI )
oColumnCarName  = guiGridListAddColumn( oCarList, "Car", 0.5 )
oColumnPrice = guiGridListAddColumn( oCarList, "Price", 0.3 )
 
oCarXml = xmlLoadFile("data\\cars.xml")
 
if ( oCarXml ) then
 
	outputChatBox( "Client: Cars.xml loaded" )
 
	--local oCarListRow = guiGridListAddRow( oCarList )
	--guiGridListSetItemText( oCarList, oCarListRow, 1, "test", false, false )
	--guiGridListSetItemText( oCarList, oCarListRow, 2, "123", false, false )
 
	local iCarIndex = 0;
 
	while ( xmlFindChild( oCarXML, "vehicle", iCarIndex ) ~= false ) do
		outputChatBox( "Client: message 2" )
		local oCar = xmlFindChild( oCarXML, "vehicle", iCarIndex )
		local iCarID = xmlNodeGetAttribute( oCar, "id" )
		local iCarPrice = xmlNodeGetAttribute( oCar, "price" )
		local sCarName = getVehicleNameFromModel( iCarID )
 
		local oCarListRow = guiGridListAddRow( oCarList )
 
		guiGridListSetItemText( oCarList, oCarListRow, 1, ""..sCarName.."", false, false )
		guiGridListSetItemText( oCarList, oCarListRow, 2, ""..iCarPice.."", false, false )
 
		iCarIndex = iCarIndex + 1
 
	end
 
	outputChatBox( "Client: cars added to list." )

as you can see, I also tried adding cars manually to the list and that worked fine. Can anyone help me, or point me in the right direction?

Link to comment

Hello,

I have a problem that is starting to give me a headache. I'm learing Lua Scripting, so I took some scripts as examples and tried to build a carshop.

Basic idea seems simple to me, list al the cars from an xml file and then, when a player clicks buy, compare his/her money to the costs and spawn the car.

The shops are created without a fuss, also from an xml file. But when I do so with the cars, it seems to think there are no entries in the xml file?!.

Here is my a piece of my xml file:

									        

and this is the list function:

outputChatBox( "Client: Creating car grid" ) 	oCarList   = guiCreateGridList ( 0.03 , 0.07, 0.4, 0.8, true, oShopGUI )	oColumnCarName  = guiGridListAddColumn( oCarList, "Car", 0.5 )	oColumnPrice = guiGridListAddColumn( oCarList, "Price", 0.3 ) 	oCarXml = xmlLoadFile("data\\cars.xml") 	if ( oCarXml ) then 		outputChatBox( "Client: Cars.xml loaded" ) 		--local oCarListRow = guiGridListAddRow( oCarList )		--guiGridListSetItemText( oCarList, oCarListRow, 1, "test", false, false )		--guiGridListSetItemText( oCarList, oCarListRow, 2, "123", false, false ) 		local iCarIndex = 0; 		while ( xmlFindChild( oCarXML, "vehicle", iCarIndex ) ~= false ) do			outputChatBox( "Client: message 2" )			local oCar = xmlFindChild( oCarXML, "vehicle", iCarIndex )			local iCarID = xmlNodeGetAttribute( oCar, "id" )			local iCarPrice = xmlNodeGetAttribute( oCar, "price" )			local sCarName = getVehicleNameFromModel( iCarID ) 			local oCarListRow = guiGridListAddRow( oCarList ) 			guiGridListSetItemText( oCarList, oCarListRow, 1, ""..sCarName.."", false, false )			guiGridListSetItemText( oCarList, oCarListRow, 2, ""..iCarPice.."", false, false ) 			iCarIndex = iCarIndex + 1 		end 		outputChatBox( "Client: cars added to list." )

as you can see, I also tried adding cars manually to the list and that worked fine. Can anyone help me, or point me in the right direction?

Link to comment

sorry, my bad. I changed the code to look for the tag but still nothing. :(

outputChatBox( "Client: Creating car grid" )
 
oCarList   = guiCreateGridList ( 0.03 , 0.07, 0.4, 0.8, true, oShopGUI )
oColumnCarName  = guiGridListAddColumn( oCarList, "Car", 0.5 )
oColumnPrice = guiGridListAddColumn( oCarList, "Price", 0.3 )
 
oCarXml = xmlLoadFile("data\\cars.xml")
 
if ( oCarXml ) then
 
outputChatBox( "Client: Cars.xml loaded" )
 
--local oCarListRow = guiGridListAddRow( oCarList )
--guiGridListSetItemText( oCarList, oCarListRow, 1, "test", false, false )
--guiGridListSetItemText( oCarList, oCarListRow, 2, "123", false, false )
 
local iCarIndex = 0;
 
while ( xmlFindChild( oCarXML, "car", iCarIndex ) ~= false ) do
outputChatBox( "Client: message 2" )
local oCar = xmlFindChild( oCarXML, "car", iCarIndex )
local iCarID = xmlNodeGetAttribute( oCar, "id" )
local iCarPrice = xmlNodeGetAttribute( oCar, "price" )
local sCarName = getVehicleNameFromModel( iCarID )
 
local oCarListRow = guiGridListAddRow( oCarList )
 
guiGridListSetItemText( oCarList, oCarListRow, 1, ""..sCarName.."", false, false )
guiGridListSetItemText( oCarList, oCarListRow, 2, ""..iCarPice.."", false, false )
 
		iCarIndex = iCarIndex + 1
 
end
 
outputChatBox( "Client: cars added to list." )
 
else
outputChatBox( "Client: Fatal error while loading xml file." )
end

Link to comment

sorry, my bad. I changed the code to look for the tag but still nothing. :(

outputChatBox( "Client: Creating car grid" )
 
oCarList   = guiCreateGridList ( 0.03 , 0.07, 0.4, 0.8, true, oShopGUI )
oColumnCarName  = guiGridListAddColumn( oCarList, "Car", 0.5 )
oColumnPrice = guiGridListAddColumn( oCarList, "Price", 0.3 )
 
oCarXml = xmlLoadFile("data\\cars.xml")
 
if ( oCarXml ) then
 
outputChatBox( "Client: Cars.xml loaded" )
 
--local oCarListRow = guiGridListAddRow( oCarList )
--guiGridListSetItemText( oCarList, oCarListRow, 1, "test", false, false )
--guiGridListSetItemText( oCarList, oCarListRow, 2, "123", false, false )
 
local iCarIndex = 0;
 
while ( xmlFindChild( oCarXML, "car", iCarIndex ) ~= false ) do
outputChatBox( "Client: message 2" )
local oCar = xmlFindChild( oCarXML, "car", iCarIndex )
local iCarID = xmlNodeGetAttribute( oCar, "id" )
local iCarPrice = xmlNodeGetAttribute( oCar, "price" )
local sCarName = getVehicleNameFromModel( iCarID )
 
local oCarListRow = guiGridListAddRow( oCarList )
 
guiGridListSetItemText( oCarList, oCarListRow, 1, ""..sCarName.."", false, false )
guiGridListSetItemText( oCarList, oCarListRow, 2, ""..iCarPice.."", false, false )
 
		iCarIndex = iCarIndex + 1
 
end
 
outputChatBox( "Client: cars added to list." )
 
else
outputChatBox( "Client: Fatal error while loading xml file." )
end

Link to comment

You went too deep through the nesting levels.

xml file:

<cars>
  <car id="602" price="15000" />
   ...
</cars>

your script:

oCarXml = xmlLoadFile("data\\cars.xml")  --loaded xml file, oCarXml is pointing at 
if ( oCarXml ) then
   ...
local iCarIndex = 0;
while ( xmlFindChild( oCarXML, "car", iCarIndex ) ~= false ) do  --found  element iCarIndex down the sequence 
--(0 is first in list, etc) - add xmlFindChid line above setting oCar and check for oCar variable validity here
           ...
local oCar = xmlFindChild( oCarXML, "car", iCarIndex )  --does not exist, you've gone a nesting level too far
--this would be looking for  - delete this line
           ...  --this code looks fine with the above changes at a glance

Link to comment

You went too deep through the nesting levels.

xml file:

       ...

your script:

oCarXml = xmlLoadFile("data\\cars.xml")  --loaded xml file, oCarXml is pointing at 
if ( oCarXml ) then
   ...
local iCarIndex = 0;
while ( xmlFindChild( oCarXML, "car", iCarIndex ) ~= false ) do  --found  element iCarIndex down the sequence 
--(0 is first in list, etc) - add xmlFindChid line above setting oCar and check for oCar variable validity here
           ...
local oCar = xmlFindChild( oCarXML, "car", iCarIndex )  --does not exist, you've gone a nesting level too far
--this would be looking for  - delete this line
           ...  --this code looks fine with the above changes at a glance

Link to comment

Actually, you may have a point. I may have misread your script. I'll re-read it and edit this post.

OK, I did, Calling xmlFindChild twice confused me. :D

Your xml lines look correct, but there are errors in your gridlist functions, and you haven't converted datatypes to match function requirements. Also, you should use outputDebugScript() and tostring() more to check contents of variables, to see if you are indeed getting the values you want. Remember you see the output with the command /debugscript 3, as admin.

http://robhol.net/guide/basics/?p=13

...
local iCarIndex = 0;
 
while ( xmlFindChild( oCarXML, "car", iCarIndex ) ~= false ) do
outputChatBox( "Client: message 2" )
local oCar = xmlFindChild( oCarXML, "car", iCarIndex )
local iCarID = xmlNodeGetAttribute( oCar, "id" )
local iCarPrice = xmlNodeGetAttribute( oCar, "price" )
 
outputDebugString("index: "..tostring(iCarIndex).." id: "..tostring(iCarID).." - price: "..tostring(iCarPrice))  --example debug string line I would use
 
--local sCarName = getVehicleNameFromModel( iCarID )  --requires a integer, not a string
local sCarName = getVehicleNameFromModel( tonumber( iCarID ) )
 
local oCarListRow = guiGridListAddRow( oCarList )
 
guiGridListSetItemText( oCarList, oCarListRow, 1, ""..sCarName.."", false, false )
--guiGridListSetItemText( oCarList, oCarListRow, 2, ""..iCarPice.."", false, false )  --misspelled iCarPrice
guiGridListSetItemText( oCarList, oCarListRow, 2, ""..iCarPrice.."", false, false )
 
        iCarIndex = iCarIndex + 1
 
end
 
outputChatBox( "Client: cars added to list." )
 
else
outputChatBox( "Client: Fatal error while loading xml file." )
end

Link to comment

Actually, you may have a point. I may have misread your script. I'll re-read it and edit this post.

OK, I did, Calling xmlFindChild twice confused me. :D

Your xml lines look correct, but there are errors in your gridlist functions, and you haven't converted datatypes to match function requirements. Also, you should use outputDebugScript() and tostring() more to check contents of variables, to see if you are indeed getting the values you want. Remember you see the output with the command /debugscript 3, as admin.

http://robhol.net/guide/basics/?p=13

...
local iCarIndex = 0;
 
while ( xmlFindChild( oCarXML, "car", iCarIndex ) ~= false ) do
outputChatBox( "Client: message 2" )
local oCar = xmlFindChild( oCarXML, "car", iCarIndex )
local iCarID = xmlNodeGetAttribute( oCar, "id" )
local iCarPrice = xmlNodeGetAttribute( oCar, "price" )
 
outputDebugString("index: "..tostring(iCarIndex).." id: "..tostring(iCarID).." - price: "..tostring(iCarPrice))  --example debug string line I would use
 
--local sCarName = getVehicleNameFromModel( iCarID )  --requires a integer, not a string
local sCarName = getVehicleNameFromModel( tonumber( iCarID ) )
 
local oCarListRow = guiGridListAddRow( oCarList )
 
guiGridListSetItemText( oCarList, oCarListRow, 1, ""..sCarName.."", false, false )
--guiGridListSetItemText( oCarList, oCarListRow, 2, ""..iCarPice.."", false, false )  --misspelled iCarPrice
guiGridListSetItemText( oCarList, oCarListRow, 2, ""..iCarPrice.."", false, false )
 
        iCarIndex = iCarIndex + 1
 
end
 
outputChatBox( "Client: cars added to list." )
 
else
outputChatBox( "Client: Fatal error while loading xml file." )
end

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...