Jump to content

Variables and functions visibility


dam034

Recommended Posts

Posted

Dear users,

if in a resource I create many lua files, one for functions, one for variables, one for events, ecc..., are they all visible between them?

If I declare a variable in a.lua, can I edit it in b.lua and output in c.lua?

I need some particular declaration?

 

Thanks

Posted

players can see  only "client" side script, but you can check it. go to MTA\mods\deathmatch\resources, pick any resources, and you can see it.

I think you need to compile the "shared" file too.

  • Moderators
Posted

Variables are shared.

 

Unless

 

They are declared as locals.

local localVariable

 

Or if they are parameters. (which are also locals)

function thisFunction (parameter1, parameter2)
  
end

 

 

A local variable can not leave it's block, neither it's file(which becomes it's block).

http://lua-users.org/wiki/ScopeTutorial

 

-- start file block

function exampleFunction ()
	-- start function block
	if true then
		-- start IF block

		-- ...
    
		-- end IF block
	elseif true then
		-- start ELSEIF block

		-- ...
    
		-- end ELSEIF block	
	else
		-- start ELSE block

		-- ...
    
		-- end ELSE block	    
	end
	-- end function block
end

-- end file block

 

-- start file block

--
function exampleFunction ()
    -- start function block
    if true then
        -- start IF block

        -- ...
    
        -- end IF block

    elseif true then
        -- start ELSEIF block

        -- ...
    
        -- end ELSEIF block    

    else
        -- start ELSE block

        -- ...
    
        -- end ELSE block      
 
    end
    -- end function block
end

--

-- end file block

local variable -- I can not leave :(

 

 

 

 

  • Like 2
Posted

Functions are visible between files btw. Unless they're local. You can make one file with useful functions and then another one with the main code. You can use the functions from the first file in the main one without a problem. 

Posted

Thanks for the explanation I understood.

Now, I want to know if a variable/function declared in a lua file is visible:

  • in a lua file of another resource
  • in a http file of the same resource

 

Thanks

Posted
9 minutes ago, dam034 said:

I want to know if a variable/function declared in a lua file is visible:

  • in a lua file of another resource

Only if you export it (for a function): 

I think variables can't be used in another resource but not sure about it.

Posted

Today I have another question: if I declare a variable in a lua file executed clientside, can I read it in another lua executed serverside in the same resource?

 

Thanks

  • Moderators
Posted (edited)
7 minutes ago, dam034 said:

Today I have another question: if I declare a variable in a lua file executed clientside, can I read it in another lua executed serverside in the same resource?

 

Thanks

As I said in my previous post.

You just asked the exact same question as in your first post.

Edited by IIYAMA
  • Haha 1
Posted

Since I don't understand good the english, can you tell me if a variable declared clientside I can (and how) read serverside?

 

Thanks

Posted
25 minutes ago, dam034 said:

Since I don't understand good the english, can you tell me if a variable declared clientside I can (and how) read serverside?

 

Thanks

You can send variables's values only via events:

https://wiki.multitheftauto.com/wiki/TriggerServerEvent

https://wiki.multitheftauto.com/wiki/TriggerClientEvent

https://wiki.multitheftauto.com/wiki/TriggerEvent

or element data:

https://wiki.multitheftauto.com/wiki/SetElementData

https://wiki.multitheftauto.com/wiki/GetElementData

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