Jay_

Passing meta settings XML to client

Recommended Posts

Hello,

I'm trying to pass my server side settings defined in my meta.xml to the client. However, when passing the table of XML data to the client, the data does not persist.

Here's my server side code:

 

addEvent("onClientRequestResourceSettings", true)
addEventHandler("onClientRequestResourceSettings", resourceRoot,
	function()		

		local xml = xmlLoadFile("meta.xml")             	
		local settingsNode = xmlFindChild(xml, "settings", 0)
		
		if(settingsNode) then
			local settings = xmlNodeGetChildren(settingsNode)   
			triggerClientEvent(client, "onServerProvideResourceSettings", resourceRoot, settings)
		end 
		
        xmlUnloadFile(xml)    
	end 
)

That above event is called client side when the resource starts.

 

Here's the client side code:

 

addEvent("onServerProvideResourceSettings", true)
addEventHandler("onServerProvideResourceSettings", resourceRoot,
	function(theSettings)
		iprint("Settings provided: "..inspect(theSettings)) --Output: "Settings provided: { }"
	end
)

 

However my settings table is not being passed to the client. When printing the output, it appears to be an empty table. I'm aware there is a limitation of passing xml data to the client, however as this is in fact a table data type, I wouldn't of thought there would be an issue?

Can anyone point me in the right direction, or perhaps provide some better solutions for passing settings data from the meta.xml to the client?

Cheers.

 

Edited by Jay_

Share this post


Link to post

The table is fine, but the nodes aren't. Those nodes are nothing more than a reference to the real nodes in the xml, which are only available on the side where the xml is loaded.

Loop through the table and get the real data with https://wiki.multitheftauto.com/wiki/XmlNodeGetAttributes

https://wiki.multitheftauto.com/wiki/XmlNodeGetValue

And send that.

Edited by IIYAMA
  • Like 1

Share this post


Link to post
    addEvent("onClientRequestResourceSettings", true)
    addEventHandler("onClientRequestResourceSettings", resourceRoot,
    	function()		

 		local xml = xmlLoadFile("meta.xml") 
		local settingsNode = xmlFindChild(xml, "settings",0) 
		if (settingsNode) then 
		for i =  0,1 do 	
         local Node = xmlFindChild(settingsNode, "setting",i) 
	local settings =  xmlNodeGetAttributes(Node)
    			triggerClientEvent(client, "onServerProvideResourceSettings", resourceRoot, settings)
                                    end 
								end
					  xmlUnloadFile(xml)    
				end 
       	)
		
		

 

Share this post


Link to post

I personally would use setElementData for this, simply save the resource settings as element data to 'resource' (predefined variable) and then you can use the same element on the client to access these data. No need to use events in this case.

You may have to use resourceRoot, I'm not sure which one is correct in this case.

Edited by MIKI785

Share this post


Link to post
5 hours ago, IIYAMA said:

The table is fine, but the nodes aren't. Those nodes are nothing more than a reference to the real nodes in the xml, which are only available on the side where the xml is loaded.

Loop through the table and get the real data with https://wiki.multitheftauto.com/wiki/XmlNodeGetAttributes

https://wiki.multitheftauto.com/wiki/XmlNodeGetValue

And send that.

Thanks. This makes perfect sense, I'll go with that.

 

4 hours ago, Ahmed Ly said:

    addEvent("onClientRequestResourceSettings", true)
    addEventHandler("onClientRequestResourceSettings", resourceRoot,
    	function()		

 		local xml = xmlLoadFile("meta.xml") 
		local settingsNode = xmlFindChild(xml, "settings",0) 
		if (settingsNode) then 
		for i =  0,1 do 	
         local Node = xmlFindChild(settingsNode, "setting",i) 
	local settings =  xmlNodeGetAttributes(Node)
    			triggerClientEvent(client, "onServerProvideResourceSettings", resourceRoot, settings)
                                    end 
								end
					  xmlUnloadFile(xml)    
				end 
       	)
		
		

 

Ahmed Ly, your code doesn't really make sense to me. I'm not sure why you're explicitly calling xmlFindChild during a strange iteration, when my above method of using xmlNodeGetChildren will retrieve all of the children in a table? 

4 hours ago, MIKI785 said:

I personally would use setElementData for this, simply save the resource settings as element data to 'resource' (predefined variable) and then you can use the same element on the client to access these data. No need to use events in this case.

You may have to use resourceRoot, I'm not sure which one is correct in this case.

Given that there is potential scope for there to be a large number of settings defined in the XML, I don't agree with this. setElementData is really only designed to sync small amounts of data.

I only need to provide this information once during initialisation of the resource to a single client. setElementData really wouldn't be an efficient way of providing this information over a single event call. It also raises concerns over security as I only want this information to be accessible from within the resource. 

Edited by Jay_
  • Like 1

Share this post


Link to post

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.