ThePope Posted January 10, 2010 Share Posted January 10, 2010 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
ThePope Posted January 10, 2010 Author Share Posted January 10, 2010 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
robhol Posted January 10, 2010 Share Posted January 10, 2010 Well, your script is looking for a tag, which I don't see anywhere? Link to comment
robhol Posted January 10, 2010 Share Posted January 10, 2010 Well, your script is looking for a tag, which I don't see anywhere? Link to comment
ThePope Posted January 10, 2010 Author Share Posted January 10, 2010 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
ThePope Posted January 10, 2010 Author Share Posted January 10, 2010 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
subenji99 Posted January 10, 2010 Share Posted January 10, 2010 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
subenji99 Posted January 10, 2010 Share Posted January 10, 2010 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
ThePope Posted January 10, 2010 Author Share Posted January 10, 2010 but how is that one level to deep? I used oCarXML as root element, which is still pointing at ? Link to comment
ThePope Posted January 10, 2010 Author Share Posted January 10, 2010 but how is that one level to deep? I used oCarXML as root element, which is still pointing at ? Link to comment
subenji99 Posted January 10, 2010 Share Posted January 10, 2010 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. 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
subenji99 Posted January 10, 2010 Share Posted January 10, 2010 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. 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
ThePope Posted January 10, 2010 Author Share Posted January 10, 2010 well, it changed the situation. Now the script won't react to the event when I walk into the sphere I created. I'm going to look into it, but not right now. Thanks for the help so far! Link to comment
ThePope Posted January 10, 2010 Author Share Posted January 10, 2010 well, it changed the situation. Now the script won't react to the event when I walk into the sphere I created. I'm going to look into it, but not right now. Thanks for the help so far! Link to comment
subenji99 Posted January 10, 2010 Share Posted January 10, 2010 That's likely to be an error parsing the file, preventing any of the code from executing. Again, check Debugscript - it'll report an error if there is one, and on what line it ran into trouble. Link to comment
subenji99 Posted January 10, 2010 Share Posted January 10, 2010 That's likely to be an error parsing the file, preventing any of the code from executing. Again, check Debugscript - it'll report an error if there is one, and on what line it ran into trouble. Link to comment
ThePope Posted January 10, 2010 Author Share Posted January 10, 2010 thank you for the tip, I'll look into it tomorrow. Found some things about debugging on the wiki so it should be interesting. Link to comment
ThePope Posted January 10, 2010 Author Share Posted January 10, 2010 thank you for the tip, I'll look into it tomorrow. Found some things about debugging on the wiki so it should be interesting. Link to comment
ThePope Posted January 11, 2010 Author Share Posted January 11, 2010 problem is solved, thank you very much! there appeared to be one "end" to many between an if and else ^^ Link to comment
ThePope Posted January 11, 2010 Author Share Posted January 11, 2010 problem is solved, thank you very much! there appeared to be one "end" to many between an if and else ^^ Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now