Jump to content

Problem with xml


Aron800

Recommended Posts

Hello,

I started with scripting 2 weeks ago and i want to try something but it doesn't work.

My script.lua:

gResRoot = getResourceRootElement(getThisResource())
function loadCars ()
local root = xmlLoadFile ("cars.xml")
local carRoot = xmlFindChild (root,"parked",0)
if (carRoot) then
   allCars = {}
for i,v in ipairs (xmlNodeGetChildren(carRoot)) do
 
local carmodel = xmlNodeGetAttribute(carRoot, "model")
local carX = xmlNodeGetAttribute(carRoot, "posX")
local carY = xmlNodeGetAttribute(carRoot, "posY")
local carZ = xmlNodeGetAttribute(carRoot, "posZ")
local carA = xmlNodeGetAttribute(carRoot, "rot")
createVehicle(carmodel, carX, carY, carZ, 0.0, 0.0, carA)
 
end
end
end
addEventHandler("onResourceStart",gResRoot, loadCars)

It needs to spawn some vehicles that are in my cars.xml:

<cars>
<parked>
<car num="0" model="411" posX="0" posY="-30" posZ="3" rot="90.0" />
<car num="1" model="432" posX="-30" posY="0" posZ="3" rot="90.0" />
parked>
cars>

But it returns: WARNING: car_spawn\server.lua:14: Bad argument @ 'createVehicle'

So what is wrong with it?

Link to comment

Thanks it works. And one other question:

If i want to give the car a name and an ID how do i give it the name car0 and car1?

Because this doesn't work:

local carName = "car" + xmlNodeGetAttribute(v, "num")
carName=createVehicle(carmodel, carX, carY, carZ, 0.0, 0.0, carA)
setElementID(carName,carName)

Link to comment

should work:

local carName = "car" .. xmlNodeGetAttribute(v, "num")
local car = createVehicle(carmodel, carX, carY, carZ, 0.0, 0.0, carA)
setElementID(car, carName)

When you want to join strings together in Lua you use .. not +

You also had set carName to the string, and then overwrote it with the return from createVehicle() thats the reason it wasnt working.

Link to comment
What's the carA?

The rotation of the car.

But my last question i think:

gResRoot = getResourceRootElement(getThisResource())
function loadCars ()
local root = xmlLoadFile ("cars.xml")
local carRoot = xmlFindChild (root,"parked",0)
if (carRoot) then
   allCars = {}
for i,v in ipairs (xmlNodeGetChildren(carRoot)) do
local carmodel = xmlNodeGetAttribute(v, "model")
local carX = xmlNodeGetAttribute(v, "posX")
local carY = xmlNodeGetAttribute(v, "posY")
local carZ = xmlNodeGetAttribute(v, "posZ")
local carA = xmlNodeGetAttribute(v, "rot")
local carName = "car" .. xmlNodeGetAttribute(v, "num")
local car = createVehicle(carmodel, carX, carY, carZ, 0.0, 0.0, carA)
setElementID(car, carName)
end
end
end
addEventHandler("onResourceStart",gResRoot, loadCars)
 
addCommandHandler ("park",
function(thePlayer,command)
local theVehicle = getPedOccupiedVehicle (thePlayer)
local root = xmlLoadFile ("cars.xml")
local carRoot = xmlFindChild (root,"parked",0)
local carNumber = getElementID(theVehicle)
outputChatBox ("Het is: " .. carNumber,thePlayer,255,0,0,false)  
if (carNumber == "") then
if (getPedOccupiedVehicleSeat (thePlayer) == 0) then
local x,y,z = getElementPosition (theVehicle)
local carModel = getElementModel (theVehicle)
local carRot = getVehicleRotation (theVehicle)
local carCount = tonumber(getCarCount())
local newCar = xmlCreateChild (carRoot,"car")
xmlNodeSetAttribute (newCar,"num",carCount)
xmlNodeSetAttribute (newCar,"model",carModel)
xmlNodeSetAttribute (newCar,"posX",x)
xmlNodeSetAttribute (newCar,"posY",y)
xmlNodeSetAttribute (newCar,"posZ",z)
xmlNodeSetAttribute (newCar,"rot",carRot)
xmlNodeSetAttribute (newCar,"owner",thePlayer)
local carName = "car" .. carCount
setElementID(theVehicle, carName)
outputChatBox ("Your car is parked!",thePlayer,255,0,0,false)  
xmlSaveFile (root)   
xmlUnloadFile (root)
else
outputChatBox("You must be the driver of a car to do this!",thePlayer,255,0,0,false)
end
else
if (getPedOccupiedVehicleSeat (thePlayer) == 0) then
outputChatBox ("Je auto is al geparkeerd!",thePlayer,255,0,0,false)
else
outputChatBox ("You must be the driver of a car to do this!",thePlayer,255,0,0,false)
end
end
end)
 
function getCarCount ()
local root = xmlLoadFile ("cars.xml")
local carRoot = xmlFindChild (root,"parked",0)
local allCars = xmlNodeGetChildren (carRoot)
 cars = 0
for i,v in ipairs (allCars) do
   cars = cars+1
end
xmlUnloadFile (root)
return cars
end

But this is not working:

xmlNodeSetAttribute (newCar,"owner",thePlayer) -- it returns [2010-08-03 11:29:56] WARNING: car_respawn\server.lua:48: Bad argument @ 'xmlNodeSetAttribute'

And how can i find out who the owner is?

Link to comment

OK, I missed your first post.

I suppose you don't understand what variable types are... If you look at xmlNodeSetAttribute wiki page then you'll see you can only use string or a number for the attribute value. Player element is not string nor number. You can getPlayerName and use it as attribute value.

Bad argument means you passed an invalid data type to the function. https://wiki.multitheftauto.com/wiki/Debugging

Edited by Guest
Link to comment

You can use setElementData and set vehicle's owner. Then you can use getElementData to check if driver's name is owner or not.. I would suggest you use account names instead of player name because player name can change but account name will always stay the same even if player will change his name his account name will be the same.

Link to comment

Every vehicle should have an id in xml file so that you can find it with a for loop, xmlNodeGetChildren and xmlNodeGetAttribute (to get the id from the file and compare it with id of vehicle - not the model but the id that you loaded from xml file earlier) and then change the attributes with xmlNodeSetAttribute.

Link to comment

Again another problem:

I made a script, you can lock and unlock your car but you can only have one car now.

So i want to make a player menu. And in the player menu you need to select your car.

But when i try it it returns an error.

My script:

function startWindow(thePlayer)
 
local X = 0.375
local Y = 0.375
local Width = 600
local Height = 330
playerMenu = guiCreateWindow(X, Y, Width, Height, "Player Menu (M)", false)
 
tabWidget = guiCreateTabPanel(10, 25, 581, 291, false, playerMenu)
tabCars = guiCreateTab("Cars", tabWidget)
 
listWidget = guiCreateGridList(12, 30, 551, 192, false, tabCars)
guiGridListSetSortingEnabled(listWidget, false)
 
listWidget_col = guiGridListAddColumn(listWidget, "Your cars:", 0.85)
local listWidget_row = nil
 
label = guiCreateLabel(220, 10, 151, 16, "Select a car and press Ok", false, tabCars)
guiLabelSetHorizontalAlign(label, "left", false)
guiLabelSetVerticalAlign(label, "center")
 
btnOk = guiCreateButton(390, 230, 75, 23, "Ok", false, tabCars)
btnCancel = guiCreateButton(470, 230, 75, 23, "Cancel", false, tabCars)
 
tabTaxi = guiCreateTab("Taxi", tabWidget)
 
local root = xmlLoadFile ("cars.xml")
local carRoot = xmlFindChild (root,"parked",0)
for i,v in ipairs (xmlNodeGetChildren(carRoot)) do
local carOwner = xmlNodeGetAttribute(v, "owner")
local num = xmlNodeGetAttribute(v, "num")
local carName = "car" .. xmlNodeGetAttribute(v, "num")
if (carOwner == thePlayer) then
			listWidget_row = guiGridListAddRow(listWidget)
guiGridListSetItemText(listWidget, listWidget_row, listWidget_col, carName, false, false )
end
end
xmlUnloadFile (root)
guiSetVisible (gui, true)
showCursor(true)
 
 
end
addEvent( "startWindow", true )
addEventHandler( "startWindow", getRootElement(), startWindow )

And my server.lua:

function PlayerJoin ( )
bindKey ( source, "m", "down", playerMenu )
end
addEventHandler ( "onPlayerJoin", getRootElement(), PlayerJoin )
 
function playerMenu(thePlayer)
local play = getPlayerAccount(thePlayer)
local player = getAccountName(play)
triggerClientEvent ( "startWindow", getRootElement(), player)
end

local carRoot = xmlFindChild (root,"parked",0) -- This line retruns: Bad argument @ 'xmlFindChild'
for i,v in ipairs (xmlNodeGetChildren(carRoot)) do -- This line returns: Bad argument @ 'xmlNodeGetChildren'

And i cant find out what is wrong because this works server side and it says on the wiki server and client side.

Link to comment

Is cars.xml the same as it was, like this?

...

After this:

local root = xmlLoadFile ("cars.xml")

Put this:

outputDebugString("cars xml test: " .. tostring(root))

I see cars.xml is being opened by the client side script, is the file in the right place. I had a "problem" myself I was writing an xml file, and looking for it server side but I was writing it from the client side so I was looking in the wrong place for it, thinking it wasnt working ...... :roll:

Link to comment
Is cars.xml the same as it was, like this?

...

After this:

local root = xmlLoadFile ("cars.xml")

Put this:

outputDebugString("cars xml test: " .. tostring(root))

I see cars.xml is being opened by the client side script, is the file in the right place. I had a "problem" myself I was writing an xml file, and looking for it server side but I was writing it from the client side so I was looking in the wrong place for it, thinking it wasnt working ...... :roll:

Yes cars.xml is the same

And it returns false

But my map looks like this:

* cars.xml

* meta.xml

* help.xml

* spawn.lua

* playerMenu.lua

Link to comment

in meta.xml have you got a line like:

You can put two lines in:

so both sides can use the file. But dont forget if you update the file on the server side (or client side), the client (or server) will only have the original version. You can use some triggerClientEvent / triggerServerEvent to keep them in sync.

Link to comment
in meta.xml have you got a line like:

You can put two lines in:

so both sides can use the file. But dont forget if you update the file on the server side (or client side), the client (or server) will only have the original version. You can use some triggerClientEvent / triggerServerEvent to keep them in sync.

Almost everything you said is lack of knowledge.

- You can't have server-side

- If you edit file in resource folder (server-side), it will be re-downloaded by players when the resource is reloaded/restarted

- There is no need to triggerClientEvent or triggerServerEvent to keep them in sync

If you want the file to be on both sides the same then use instead but if the script will change the file then it will be edited on 1 side until resource restarts. I recommend you use for all your xml files because if xmlLoadFile fails, you can still use getResourceConfig to load the file. I don't see a point of having 1 file for server and client. You can load the file server-side and then send data needed to client with triggerClientEvent. Show us your meta.xml.

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...