Jump to content

I don't know, how shared.lua file works?


Recommended Posts

Hello, I didn't understand how shared side works.

I tried to do it:

-- shared:

SCRIPT_activity = true
SCRIPT_text = "Random text"


-- server

if SCRIPT_activity then
  outputChatBox(SCRIPT_text)
end

That didn't work, why? But when I did this:

-- shared:

SCRIPT_activity = true
SCRIPT_text = "Random text"


-- server

addEventHandler("onResourceStart",resourceRoot,function()
    if SCRIPT_activity then
      outputChatBox(SCRIPT_text)
      end
end)

That absolutely worked. Why it only works when I use an event handler? 

 

I tried to do it with Command Handler and it worked too:

-- shared:

SCRIPT_activity = true
SCRIPT_text = "Random text"


-- server

addCommandHandler("writeText",function()
    if SCRIPT_activity then
      outputChatBox(SCRIPT_text)
      end
end)

That works. But why it doesnt work when I use it without any command/event handler? Please explain how shared side works?

Link to comment

The shared side is a way to share variables and functions between the client and server in the MTA. However, it is important to understand how it works to avoid errors and unexpected behavior.

When you define a variable on the shared side, it is shared between the client and the server. However, the variable is not automatically synchronized between the two. This means that if you change the variable on the server, it will not automatically be updated on the client, and vice versa.

To synchronize shared variables, you need to use a synchronization mechanism, such as an event handler or a command. This is necessary because the MTA does not automatically synchronize shared variables between the client and the server.

In your example, you defined the SCRIPT_activity and SCRIPT_text variables on the shared side and tried to access them on the server without using an event handler or command. This did not work because the variables were not synchronized between the client and the server.

When you used an event handler (onResourceStart) or a command (writeText), you synchronized the shared variables between the client and the server. This allowed the variables to be accessed correctly on the server. 

  • Like 1
  • Thanks 1
Link to comment
  • Moderators

Additional context:

  • Serverside is code executed on the server.
    • The application all players are connected to.
       
  • Clientside is code executed on each client/player his game.
    • There are as many clientsides as there are players in the server. They all run a copy of all clientside scripts.
    • Each player downloads the clientside code from the server. When a player joins the server, in most cases the copy of the code will only run until the download is finished. (with an exception if priority is given to a specific resource)
       
  • Shared is code executed
    • On the server
    • And a copy of the code is executed on each client / player his game

      ⚠️
    • Nothing is shared between them, except for running the same copy of the code.

 

  • Thanks 1
Link to comment
15 hours ago, Laxante101 said:

The shared side is a way to share variables and functions between the client and server in the MTA. However, it is important to understand how it works to avoid errors and unexpected behavior.

When you define a variable on the shared side, it is shared between the client and the server. However, the variable is not automatically synchronized between the two. This means that if you change the variable on the server, it will not automatically be updated on the client, and vice versa.

To synchronize shared variables, you need to use a synchronization mechanism, such as an event handler or a command. This is necessary because the MTA does not automatically synchronize shared variables between the client and the server.

In your example, you defined the SCRIPT_activity and SCRIPT_text variables on the shared side and tried to access them on the server without using an event handler or command. This did not work because the variables were not synchronized between the client and the server.

When you used an event handler (onResourceStart) or a command (writeText), you synchronized the shared variables between the client and the server. This allowed the variables to be accessed correctly on the server. 

Thank you. I tried creating two server side lua file (I put a variable in first server.lua file, I tried to use it in second file like I tried to use variable without synchronizing in shared side file) and I had to synchronize it in server side too.

 

So, I understood that I have to synchronize variables to use between other script files.

But, can you tell me all synchronizing methods? Event Handlers, Commands, and what else?

 

 

7 hours ago, IIYAMA said:

Additional context:

  • Serverside is code executed on the server.
    • The application all players are connected to.
       
  • Clientside is code executed on each client/player his game.
    • There are as many clientsides as there are players in the server. They all run a copy of all clientside scripts.
    • Each player downloads the clientside code from the server. When a player joins the server, in most cases the copy of the code will only run until the download is finished. (with an exception if priority is given to a specific resource)
       
  • Shared is code executed
    • On the server
    • And a copy of the code is executed on each client / player his game

      ⚠️
    • Nothing is shared between them, except for running the same copy of the code.

 

Yes, thank you. Even I tried to use it in shared side:

-- shared

outputChatBox("Hello MTA World!")

And that has been sent to chatbox twice. Because it worked in server and client side, right?

  • Like 1
Link to comment

Todos os métodos de tratamento de eventos

onResourceStart

onPlayerJoin
onPlayerLeave
onPlayerDeath
onVehicleSpawn
onVehicleDestroy

Sincronização de dados

setElementData
obterElementData

Você pode usar tabelas globais para armazenar variáveis e acessá-las em diferentes arquivos de script. Você também pode criar módulos que contenham funções e variáveis que podem ser acessadas em diferentes arquivos de script.

Edited by Laxante101
Link to comment
  • Moderators
1 hour ago, ThanaReal said:

And that has been sent to chatbox twice. Because it worked in server and client side, right?

Correct!

 

51 minutes ago, Laxante101 said:

Todos os métodos de tratamento de eventos

Please mind that the primary language of this section is English. It is fine to add additional translations for clarification.

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