Feche1320 Posted May 9, 2011 Share Posted May 9, 2011 Is there any setElementData global? Like: local count = getPlayerCount() setElementData("totalPlayers", count) Thanks Link to comment
Aibo Posted May 9, 2011 Share Posted May 9, 2011 "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
DiSaMe Posted May 9, 2011 Share Posted May 9, 2011 You can try setting data of root element, but I don't know if this works. Alternatively, you can create an abstract element with createElement and set its data. Link to comment
Feche1320 Posted May 9, 2011 Author Share Posted May 9, 2011 "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 Citizen Posted May 12, 2011 Moderators Share Posted May 12, 2011 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 Zango Posted May 12, 2011 Discord Moderators Share Posted May 12, 2011 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
AGENT_STEELMEAT Posted May 12, 2011 Share Posted May 12, 2011 (edited) 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 May 13, 2011 by Guest Link to comment
Castillo Posted May 12, 2011 Share Posted May 12, 2011 (edited) setElementData(InfoRoot, "totalPlayers", totalPlayers) Had a typo in that line Edited May 13, 2011 by Guest Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now