Jump to content

Tables and shared Lua files


Recommended Posts

So I'm interested into replacing element data functions to more efficient store data into tables but with this comes a problem, syncing the data between client and server so I taught of creating a shared Lua file and building the system there so in this way the functions will work both client/server.

But will this work?

Or should I use a make around with the event system?

Link to comment
  • Scripting Moderators
12 minutes ago, Tekken said:

So I'm interested into replacing element data functions to more efficient store data into tables but with this comes a problem, syncing the data between client and server so I taught of creating a shared Lua file and building the system there so in this way the functions will work both client/server.

But will this work?

Or should I use a make around with the event system?

Shared won't work.

Example:

-- Client

local clientTable = {}

--

function onClientDataSync(serverData)
	clientTable = serverData
end
addEvent("onClientDataSync", true)
addEventHandler("onClientDataSync", resourceRoot, onClientDataSync)

--

function onClientReceiveVehicleData(...)
	local receivedData = {...}
	local vehicleUserdata = receivedData[1]
	local vehicleData = receivedData[2]

	clientTable[vehicleUserdata] = vehicleData

	outputConsole(inspect(clientTable))
end
addEvent("onClientReceiveVehicleData", true)
addEventHandler("onClientReceiveVehicleData", resourceRoot, onClientReceiveVehicleData)
-- Server

local serverTable = {}
local newVehicle = createVehicle(411, 0, 0, 3)

--

serverTable[newVehicle] = "Some data you want to pass"

About sync:

--[[Sync for everyone]]

triggerClientEvent(root, "onClientReceiveVehicleData", resourceRoot, newVehicle, serverTable[newVehicle])

--[[When player latejoined - pass data f.e when he logins]]

triggerClientEvent(source, "onClientDataSync", resourceRoot, serverTable)

 

  • Thanks 1
Link to comment
  • Scripting Moderators
1 minute ago, Tekken said:

But why shared will not work?

Tables aren't synced by default. If you create tables via shared script, they do not have same data (unless you will sync it). Take a look at this topic, might be helpful.

 

  • Like 1
Link to comment
  • Moderators
21 minutes ago, Tekken said:

But why shared will not work?

 

If you enable shared, this is what will happen:

<script src="file.Lua" type="shared"/>

becomes

<script src="file.Lua" type="client"/>
<script src="file.Lua" type="server"/>

 

The file is now used for serverside as well as clientside. But they are still loaded on 2 different applications. And as you know, you can't share your memory across the internet without taking the connection delay `ping` in consideration.

 

The concept behind `shared` is a feature to make (utility) functionalities that work on both sides (client/server).

Main purpose: Less maintenance

The following content could be useful for in a shared file:

https://wiki.multitheftauto.com/wiki/FindRotation

https://wiki.multitheftauto.com/wiki/Table.random

https://wiki.multitheftauto.com/wiki/Table.compare

etc.

 

 

 

 

Edited by IIYAMA
  • Thanks 1
Link to comment

It's really easy to create alternative setElementData function what uses tables instead with automatic syncing.

https://github.com/rivor/ttt-mta/blob/master/terrortown/scripts/server/playerdata.Lua

this is the way i did back when i was still making my own stuff on mta, you can check client side, which is in client files but it's exact same code just reversed. Of course for better security, i recommend it so that you wouldn't use syncing from client side to server for obvious reasons.

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