How to define a variable from one function to another?

My question is how do I make a1 global and apply it to the next function, do I simply remove local? If so doesn’t it only apply within the function?


function a()

local a1 = "123"

end

function b()

print(a1)

end

removing local will work, as that simply keeps it local to the function.

you could also use your variable as a argument if you’re calling b() from a()

Is this correct?


function a()

local x = 1
local y = 1
Marker(x, y)

end

function Marker(x1,y1)

print(x1,y1)

end

When i’m globalizing the variable within a function into the next function it becomes nil, not sure why.

If you’re working in a single file you can localize it at the top then define it in seperate functions.

[lua]local varA,varB

function defineVars(a,b)
varA = a
varB = b
end

function getVars()
print("varA: "…varA)
print("varB: "…varB)
end[/lua]

This is also terrible coding because if getVars is called before DefineVars they will be empty. (Attempt to concentrate string with nil)

The best way to do this would be to use return in your function:

[lua]function a()
local x, y = 1, 1
return x, y
end

function Marker()
print(a())
end[/lua]

Here is a concrete example:

[lua]
local function GetBestPlayer()
local ply, kills = nil, 0
for _, v in pairs(player.GetAll()) do
if v:Frags() > kills then
ply = v
end
end
return ply, kills
end

function GiveWinnerMoney()
local ply, frags = GetBestPlayer()
if not IsValid(ply) or not ply.AddMoney then return end
ply:AddMoney(frags * 100)
end[/lua]

The reason you can’t access the other variable is because of scope. A variable stops “existing” when it is out of scope (the exception is upvalues). So when you call that function, as soon as the last “end” is hit, all local variables in that function stop existing.
Also the first function is localized because the only function that will be using it is GiveWinnerMoney, other functions from other scopes need not access it.

How does any of this help the OP? He just wanted to know how to define variables properly. KarmaLord’s example isn’t “terrible”, it’s a very clear example that shows exactly how to do what the OP wants to.

I attempted to actually explain the working behind the mechanics instead of just throwing an example which they may or may not understand because they don’t have a concrete understanding of scope and other things inside Lua. I posted relavant Lua documentation which could provoke further reading and understanding of topics that clearly the OP asked for help with. I used an example with relevance to show how this method can be used and I didn’t post an example which would break with the slightest nudge. I also tried to contribute to this topic which is more than you can say.

The problem is people seem happy to throw random examples at people and expect them to truely understand what’s going on without actually explaining the example to them. They go happily on their way, but a day later they come back with a similar question because they didn’t actually understand the subject. Give a man a fish or teach them to fish…