Jump to content

Root Elements & Things


Recommended Posts

There are several concerns (Or questions) I have regarding the whole basic concept of MTA's version of Lua...

First, I just open MTA and select the Host Server option selecting only the resource I'm working on to test my work/progress which I assume is the best/easiest way to do this. But the scripts don't always load correctly for some reason, I usually have to repeat the Host Server thing a 2nd time before it finally loads correctly. I've altered the scripts to run one at a time right after each other instead of all starting up at the same time, but this didn't solve that problem. Is this normal or perphaps its something I'm doing wrong?

Second, I've started using GetElementData/SetElementData tied to the "root" as suggested from threads found here in order to use global vars from diffirent scripts (Basically Client/Server scripts) which only seem to work sometimes. I have data set on the server side script as soon as the script loads, for example:

setElementData(root,"Test",100) 

Then when a player connects, a client side script is loaded that needs to know the data I've set on the server side script. So then I use getElementData for client side which only works if the data has been changed (While the player is connected?) by the server side script (onPlayerJoin). My guess, the player's client may only get this root element data from the server side if that data was first changed (known to the client?). My solution for this was to add, then subtract from every one of these Data Elements when the player connects, so that when it goes to the client side script, it knows this Data exists, otherwise it doesn't seem to know the data even exists, the getElementData doesn't seem to check/update that status alone...

(More Coming, this post is too long to continue)

Link to comment

Perphaps if I understood the root more, so what is the root exactly? I know it's the very bottom of a tree and whatnot, but I don't know what the bottom of that tree is, the script itself maybe?

Now even after all this is fixed by changing all the data just so the client acknowledges that the data exists, if I have to call the client side script again, then it seem to forget that data all over again. I guess to make this in more simplier terms for the professional Lua Scripters here to understand the question:

If SetElementData(root,"Test",100) is declared on the server side script,

then why doesn't GetElementData(root,"Test") work correctly on the client side script?

When I repeat the client side script with something like Perform(), why does it seem to forget ElementData(root,"Test")? Is it because "root" might be pointing to something else?

(I might just have to put up the entire code if I failed to paint the picture correctly)

Link to comment

I've decided that it's probably best to provide an example code, so:

FIRST EXAMPLE:

Server.lua

  
function SetData() 
setElementData(root,"Test",100) 
end 
  
function PlayerSetup() 
triggerClientEvent("onClientStart",source) 
end 
  
addEventHandler("onResourceStart",getRootElement(),SetData) 
addEventHandler("onPlayerJoin",getRootElement(),PlayerSetup) 

Client.lua

  
function Perform() 
Test=GetElementData(root,"Test") 
outputConsole(Test) 
end 
  
addEvent("onClientStart",true) 
addEventHandler("onClientStart",getRootElement(),Perform) 
  

This would print "false" in the Console Window instead of "100"

SECOND EXAMPLE:

Server.lua

  
function SetData() 
setElementData(root,"Test",100) 
end 
  
function PlayerSetup() 
setElementData(root,"Test",getElementData(root,"Test")+1) -- NEWLY ADDED 
setElementData(root,"Test",getElementData(root,"Test")-1) -- NEWLY ADDED 
triggerClientEvent("onClientStart",source) 
end 
  
addEventHandler("onResourceStart",getRootElement(),SetData) 
addEventHandler("onPlayerJoin",getRootElement(),PlayerSetup) 

Client.lua

  
function Perform() 
Test=GetElementData(root,"Test") 
outputConsole(Test) 
end 
  
-- The entire function below is NEWLY ADDED 
function ButtonClicked() -- The gui code isn't actually listed here, but exists 
Perform() 
end 
  
addEvent("onClientStart",true) 
addEventHandler("onClientStart",getRootElement(),Perform) 
  

This would print "100" in the Console Window,

but then would print "false" in the Console Window (If that gui button were clicked) "2nd Time Around"

Now you'll have a much better picture of my problem, so how could I resolve this?

Edited by Guest
Link to comment

Thank you for the response, I've been to the Wiki and even quite a few threads regarding these 2 functions and also Globals.

What I don't understand is how it works the first time, but not the second time and only works if the data was changed (In the 2 examples I've just posted)

UPDATE: Actually, I think I'm seeing a pattern here, at least why it doesnt work in the 2nd example the 2nd time it is run (Because its not serversidetriggered etc.), but still doesn't explain why it doesn't work in the 1st example at all

Edited by Guest
Link to comment
Lua is Case-Sensitive.

Thank you...

Okay, please ignore the simple mistakes (Such as "Set" instead of "set")

I'm fixing these up though as I noticed them, but this isn't my actual code, just used as examples

(Another words, I can assure you that's not the reason it doesn't work) :P

Link to comment
using root will affect every element, but not elements created after the function was used, such as a new player connection, he won't have the element data.

Thank you for the response, that's what I was thinking too. Because by changing the values of the data, only then does it appear correctly when using getElementData (Meaning the new player element is made aware of the data when it changes)

But when the getElementData is called for a 2nd time around, it seems that the data is lost. Now that is something I haven't figured out at all yet. It works before (First time around), but then all of a sudden the data isnt availible anymore for that player client the second time around, I don't understand why: What would have caused it to lose the data? Perphaps the link or connection (communication) between the Server/Client is broken after all functions have been completed (Therefore I mean before clicking on any gui button)?

Link to comment
you can't set the element's data on serverside then 'get' it on clientside.

... yes you can. All element data in synced by default, unless the fourth argument is set to false. It is very important to note what sort of data needs to be synced. In general, element data should be used for syncing data about an element (it can be used for many thing, but this is just a general rule).

Also, element data propagates down the element tree - if you call setElementData(root, "cupSize", "DD"), then getElementData(element, "cupSize", true) will always return "DD", as getElementData will move up the element tree until it finds a value for "cupSize". If you set the last argument of getElementData to false, it will not propagate up the element tree, therefore returning false.

Link to comment
you can't set the element's data on serverside then 'get' it on clientside.

... yes you can. All element data in synced by default, unless the fourth argument is set to false. It is very important to note what sort of data needs to be synced. In general, element data should be used for syncing data about an element (it can be used for many thing, but this is just a general rule).

Also, element data propagates down the element tree - if you call setElementData(root, "cupSize", "DD"), then getElementData(element, "cupSize", true) will always return "DD", as getElementData will move up the element tree until it finds a value for "cupSize". If you set the last argument of getElementData to false, it will not propagate up the element tree, therefore returning false.

In my first example using this method, it still returns false regardless (The client doesn't seem to acknowledge the data)

But in my second example using this method, it returns 100 as it should using getElementData(root,"Test",true)

However, the 2nd time around (When the gui button is clicked), it doesn't return 100 (my script crashes on the output)

In conclusion: It doesn't matter if I use that extra argument (true) or not in the function, I still get the same results

Link to comment
use this , you could set it @ server side then get it from client side ..
  
setElementData(getResourceRootElement(getThisResource()),"Test",100) --- or just resourceRoot 
  

Thank you, this works out perfectly even for my first example. :):D

Now with this resolved, I can finally move forward...

There is still my first concern though, when I launch MTA and choose 'Host Game' to start a local server used to test scripts I'm currently working on, it freezes or just doesn't load up the server correctly the first time. But when I go back to the main menu after it fails the first time and repeat those same steps, it loads successfully. Can anyone explain this? Perphaps the scripts are compiled with the first attempt creating a cache although it fails to loads correctly, then the second time around it works because its cached or something? Is there a way around this though? It's rather annoying having to load the server two times when I need to test my scripts...

Link to comment
use this , you could set it @ server side then get it from client side ..
  
setElementData(getResourceRootElement(getThisResource()),"Test",100) --- or just resourceRoot 
  

Thank you, this works out perfectly even for my first example. :):D

Now with this resolved, I can finally move forward...

There is still my first concern though, when I launch MTA and choose 'Host Game' to start a local server used to test scripts I'm currently working on, it freezes or just doesn't load up the server correctly the first time. But when I go back to the main menu after it fails the first time and repeat those same steps, it loads successfully. Can anyone explain this? Perphaps the scripts are compiled with the first attempt creating a cache although it fails to loads correctly, then the second time around it works because its cached or something? Is there a way around this though? It's rather annoying having to load the server two times when I need to test my scripts...

go to C:\Program Files\MTA San Andreas 1.3\server , and launch MTA Server.exe , i think this is better than starting the server from inside MTA , you will see the error's and etc ..

Link to comment
go to C:\Program Files\MTA San Andreas 1.3\server , and launch MTA Server.exe , i think this is better than starting the server from inside MTA , you will see the error's and etc ..

Thank you again, this seems to resolve that issue (Loads correctly on my first connection attempt this way)

Link to comment
go to C:\Program Files\MTA San Andreas 1.3\server , and launch MTA Server.exe , i think this is better than starting the server from inside MTA , you will see the error's and etc ..

Thank you again, this seems to resolve that issue (Loads correctly on my first connection attempt this way)

Any time :) , also when starting server from inside MTA 'Host Game' you can see server process and errors in F8.

Link to comment
  • 2 weeks later...

I've been away for a bit of time, my apologizies... My first attempt with connecting to my local server was successful, but it is actually quite rare. I must still connect 2 times before my game client works properly. It doesn't matter if I host from the game client or start the server seperately, this still occurs. But now I know its a connection issue, not a start up problem

Running MTA 1.3 with Windows XP

Has anyone experienced this before or knows how to resolve this issue?

It's rather annoying having to start, wait, then restart before it loads correctly each time for whats suppose to be quick testing... :P

Link to comment

false, you can use setElementData serversided and use getElementData to get it client sided.

You can also use setElementData client sided and set the last optional argument to true to synchronize data with server, and then you should be able to use getElementData to get it on server sided scripts.

Link to comment
I've been away for a bit of time, my apologizies... My first attempt with connecting to my local server was successful, but it is actually quite rare. I must still connect 2 times before my game client works properly. It doesn't matter if I host from the game client or start the server seperately, this still occurs. But now I know its a connection issue, not a start up problem

Running MTA 1.3 with Windows XP

Has anyone experienced this before or knows how to resolve this issue?

It's rather annoying having to start, wait, then restart before it loads correctly each time for whats suppose to be quick testing... :P

I've resolved my concerns with SetElementData (Server/Client)

My quote above is what I'm still having issues with...

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