How to pass and receive value in Lua to another Lua?

how to transfer value from a.luato b.lua?

let's say that in mine a.luaI have this variable code.

local value = "Hello WOrld!"
director:changeScene ("b")

My problem is, how can I go valuefrom a.luato b.lua?

thanks in advance....

+3
source share
4 answers

Assign a value to the global table ( _G), for example:

_G.value = "Hello WOrld"

In another script, you can do the following:

value = _G.value
+4
source

When you declare something local, you explicitly tell Lua not to share it between the scripts. Instead, create a non-local variable:

value = "Hello World"

In the b.lua file, you just need the a.lua file and use it, for example, in the b.lua file

local a_File  = require "a"
print(a_File.value)

You will get the result as

"Hello World"
+8

, , - lua , .

, : a.lua b.lua. b.lua a.lua. :

a.lua:

module("a", package.seeall)

local myVal = "My value in file a"
local SomeVal = 15

function GetSomeValue()
    return myVal
end

b.lua:

require "a"
print(a.GetSomeValue())  -- prints 'My value in file a'
print(a.SomeVal)         -- prints 15
print(SomeVal)           -- prints nil, unless you've declared it in b.lua

, _G. , 3 4 , , , ? :

a.Value
a.Function()

, , ,

_G["Value"] 

, , . _G , . . _G , , ...

+2

using the API, we pass the value to another lua file. and it sends data to a table or array type

The code below is useful.

from a.lua file

data="hellow"
director:changeScene({data},"levelroom")

from b.lua file

module(...,package.seeall)
new=function(params)
   localGroup=display.newGroup()
   data=params.data
   print(data)         --output:hellow
   return localGroup 
end
+1
source