Jump to content

setElementData global?


Recommended Posts

"totalPlayers" is not an element, it's a string. maybe a global variable is what you need?

anyway, it depends on element and what you mean by "global". if the element is synced, it's data obviously synced also, as i recall.

Link to comment
"totalPlayers" is not an element, it's a string. maybe a global variable is what you need?

anyway, it depends on element and what you mean by "global". if the element is synced, it's data obviously synced also, as i recall.

Yes, I do know that totalPlayers it's a string and it will never work.

setElementData works client-side, but it's only for elements, so I have to attach an element to make it work.

What I meant, is if on MTA exists setElementDataGlobal or whatever without having to put an element, so client-side I don't have to add handlers and triggers.

Hope you understand now.

Link to comment
  • Moderators

Why do you need a setElementData ?? just make a global at the top of you script for exemple PLAYERSCOUNT = getPlayerCount() but don't forget to update it because when the server is starting, there are no players so PLAYERSCOUNT = 0

If you want a setElementData then create a useless object under the map then make your setElementData on it.

Link to comment
  • Discord Moderators

I think what he's talking about is a way to create a variable server-side and have it available client-side too.

I think the easiest way to do this would be using a small triggerClientEvent, even though you want to avoid it.

  
-- client 
  
addEvent ('createClientVariable', true) 
addEventHandler ('createClientVariable', root, 
    function (arg, val) 
        _G[arg] = val 
    end 
) 
  
-- server 
  
function createGlobalArgument (arg, val) 
    _G[arg] = val 
    triggerClientEvent (root, 'createClientVariable', root, arg, val) 
end 

Link to comment

The easiest way by far is to use setElementData, as element data is synced with the client by default.

  
--Create an abstract element for storing info serverside (this has many uses). 
local infoRoot = createElement("infoRoot", "infoRoot") 
--Set the variable as element data for the info element serverside. 
setElementData(infoRoot, "totalPlayers", totalPlayers) 
  
--Clientside, retrieve the element by it's ID ("infoRoot") 
local infoRoot = getElementByID("infoRoot") 
--Retrieve the data from the element 
playercount = getElementData(infoRoot, "totalPlayers") 
  

Note that you could also use this infoRoot element to store all types of data. Also, as a rule of thumb, if element data dosent need to be synced between server and client, you should set the fourth argument of setElementData to false, to save some bandwidth.

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