Jump to content

Using table instead of setElementData


srslyyyy

Recommended Posts

  • Scripting Moderators

Hey once again. I just thought how can i improve performance on my server. My server is based on destroying cars (which have elementData with needed values), however as far i know setElementData synchronizes with all the players and server + elementData which I'm talking about right now was changing almost all the time (onClientVehicleDamage).

So i found this topic:

https://forum.multitheftauto.com/topic/65726-howwhy-to-stop-using-setelementdata-and-getelementdata/

And i already created some base functions of it:

vehicleData = { -- outside function
first_value = {}
}

-- on player vehicle spawn
car = createVehicle(model, x, y, z, 0, 0, rz)
carCol = createColSphere(x, y, z, 3.75)
attachElements(carCol, car, 0, 1, 0) -- attach colsphere to car, without it probably i can't do anything.
setElementData(car, "data", carCol) -- set colsphere data to element data of car.
vehicleData.first_value[carCol] = math.random(1, 20) -- set some data
-- vehicle have also like 5 more elementData, but they don't change later at all

-- check data command
addCommandHandler("data", function(plr, command)
	local vehicle = getPedOccupiedVehicle(plr)
	local parent = getElementData(vehicle, "data")
	if parent and vehicle then
		outputChatBox("Data of vehicle: "..vehicleData.first_value[parent], plr)
	end
end)

-- check table
addCommandHandler("table", function(plr, command)
	for k, v in pairs(vehicleData.first_value) do
		outputChatBox(v, plr)
	end
end)

-- clearing data after vehicle explode
function testFunction()
	local parent = getElementData(source, "data")
	if parent then
		vehicleData.first_value[parent] = nil
		outputDebugString("Data cleared.")
	end
end
addEventHandler("onVehicleExplode", getRootElement(), testFunction)

 

So here are my questions:

- elementData have any affect on player FPS? (if changing this value for example: on onClientVehicleDamage when on server are 30+ players online?)

- Should this system improve efficiency of script? If yes, then it should works much better?

- When i need also clear data, excluding vehicle explode, and script restart (table re-creates at resource restart?), i don't need to save that data anywhere, it should be saved only to moment when vehicle explode, because anyway player spawn a new car)

- How can i export this table for use in other resource? I understand this a bit, by using export, but how i can later get first_value data? Can someone give me a example of it? Export + get/set another value for this data in another resource? Based on it:

<export function="twerk" type="server"/>

table = {} 
function twerk () 
    return table 
end
  
table = exports.resourceName:twerk() 

 

Edited by majqq
Link to comment

Exports are slow too, and it's much faster to setElementData rather than creating a custom function, and adding it to the meta.xml.
If I need to share a table with all server resources, I set the element data to the 'root', and disable data client synchronization to save bandwidth since the data is not needed client side.

local mytable = { a = 1, b = 2, c = 3 }
setElementData(root, "mytable", mytable, false) -- Disable sync with fourth argument

-- On the other resource
local mytable = getElementData(root, "mytable")

 

Edited by Feche1320
Link to comment

I've heard some say that custom event triggers may be slower than element data when it comes to syncing up with clients unless you only do it when necessary rather than on each change.

If you're intending on sharing the data on the server-side only, you should simply set the 4th argument to false in setElementData. Exported functions travel from one LuaVM through C/C++ into a different LuaVM and back to your resource via C/C++. This is slower than simply using element data which only goes from LuaVM to C/C++ and back to your LuaVM (LuaVM is an instance of Lua being run under the hood, separating Lua contexts between resources). As long as you don't sync set element data, it would provide better speed than custom system with exports. A custom system would probably run faster if you keep everything in one resource though, but that's at the price of modularity.

Edited by Investor
  • Like 1
Link to comment
  • Scripting Moderators
8 minutes ago, Investor said:

I've heard some say that custom event triggers may be slower than element data when it comes to syncing up with clients unless you only do it when necessary rather than on each change.

If you're intending on sharing the data on the server-side only, you should simply set the 4th argument to false in setElementData. Exported functions travel from one LuaVM through C/C++ into a different LuaVM and back to your resource via C/C++. This is slower than simply using element data which only goes from LuaVM to C/C++ and back to your LuaVM (LuaVM is an instance of Lua being run under the hood, separating Lua contexts between resources). As long as you don't sync set element data, it would provide better speed than custom system with exports. A custom system would probably run faster if you keep everything in one resource though, but that's at the price of modularity.

 

I will need an export only for set this data once.

So if i use the system above for same resource (example: change this data when player damage a car (damage function it's in the same resource)) it should works faster? Plus i can't disable synchronization for client, because client handle damage.

Edited by majqq
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...