Jump to content

Variables and functions visibility


dam034

Recommended Posts

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

Link to comment
  • Moderators

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
Link to comment
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.

Link to comment
  • Moderators
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
Link to comment
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

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