So, the problem is: I’m pulling player data from MySQL database and place it in array. How can I access this array from another resource without pulling data from MySQL DB again?
Use events to communicate between resources, rather than using the horrible library that came with essentialshit?
Wow, so harsh! No need
Database is essential (pun intended) for RP project.
Use db in server-side (essentialmode) and do your stuff then send your array with an event to the client
Yes, obviously So no sharing of variables besides events?
The problem is that I just can’t save it to a variable once: every time I call an event, it will pull data from DB again and again.
An array within a server script only has one instance. You should have an event that pulls data from the database and stores them in an array, then access the data from the array.
Can you give simple example of saving and pulling that data? Seems like I lack some knowledge about this topic, thus can’t find a reference.
You create a global variable playerData = {}
The code below is only ran if a player joins and the server is empty.
local executed_query = MySQL:executeQuery("SELECT * FROM players WHERE steamid = '@steam'", {['@steam'] = steam})
playerData = MySQL:getResults(executed_query, {'name', 'steamid', 'model'}, "steamid")
playerData
is a table/array that you can iterate through.
That doesn’t mean you should be using a query to fetch data that already exists, nor that you should use the MySQL.lua that came with essentialmode - you should use PerformHttpRequest instead, as it is the only method that a) doesn’t freeze your server (causing split sessions/instancing) and b) is guaranteed to work on future server releases.
So, first you take playerID from client with
local player = GetPlayerPed(-1)
then transfer it to server, and get steamID with
local identifiers = GetPlayerIdentifiers(player)
and then pull the data from DB. Is that right? Or is there a simplier and more effective way?
By this do you mean we should be sending the HTTP request to another server (probably a webserver) so that they can handle all things to do with SQL? Or, do you mean we should be sending the request to the HTTP server that is created by CitizenMP?
If you mean the latter, could you give some examples as to how that workflow would work (e.g. how would we handle the request).
Okey, there is a problem: if I call an Event (with callback) to get player array data on server side (from another resource) - everything is okey. But if I call it from client - I get nil value.
That’s because you can’t have a call back on the client be called by the server (they’re separate from each other).
For example, the code below won’t work
-- Server
RegisterServerEvent("someEvent")
AddEventHandler("someEvent", function(cb)
cb("hello")
end)
-- Client
TriggerServerEvent("someEvent", function(data)
Citizen.Trace(data)
end)
Because the server and client are different machines. Instead, you will have to trigger a client event from the server.
E.g.
-- Server
RegisterServerEvent("someEvent")
AddEventHandler("someEvent", function()
TriggerClientEvent("clientSomeEvent", source, "hello")
end)
-- Client
TriggerServerEvent("someEvent")
RegisterNetEvent("clientSomeEvent")
AddEventHandlerr("clientSomeEvent", function(data)
Citizen.Trace(data) -- prints "hello"
end)
Okey, this make a lot of sense now, thank you. Today I’ve learned some important things