How to fix the value of a variable LUA - lua

I'm a little doubt, I think it is easy to solve, but I do not know ...
I'm needing to save the value of a scene to another, in a game.
For example: the player is playing, and the money variable increases, this value would stay for all scenes, which is what I want. However its value is supposedly being wiped when I enter the main menu, as if it has never existed...
Can someone tell me how to set value for all my scenes?

Are you declaring your variable as a local?
For example, is the beginning of your function similar to this?:
local gamescore = score;
If so, you'll need to remove the local, like so:
gamescore = score;
This is because the local definition only declares variables in the local block of code that it resides (i.e, it cannot be called anywhere else).
For example, if I had this function:
function = Test(score)
local sc = 0+score;
return sc;
end
The variable sc wouldn't be available anywhere else, apart from inside this function, whilst:
function = Test(score)
sc = 0+score;
return sc;
end
this sc would, as it is not local to that specific function.
Don't use local if you want your variable to be used elsewhere, but be careful that you don't overwrite it in some other function, it's easy to do, as I've done it myself a few times.

Related

How do I use parameters?

I've tried really hard to understand how parameters work in Lua, but I didn't understand yet.
This is de code:
It basically kill someone when touched.
function onTouch(part)
local player = part.Parent:FindFirstChild("Humanoid")
if(player ~= nil) then
player.Health = 0
end
end
script.Parent.Touched:Connect(onTouch)
My question is, how "part" is a valid parameter? What the Script does to use part as a parameter?
In my mind there is no sense, part is not even defined, and this Script works.
Thanks so much!
A parameter is a local variable whose initial value is an argument passed to the function when it's called. Parameters are declared as part of the function declaration.
I don't know Roblox, but in this code, it seems that onTouch is a callback: The function gets stored by script.Parent.Touched:Connect(onTouch) and later gets called somewhere we can't see, similarly to onTouch(myPart).

Need to declare local functions or variables inside a local table?

In lua if I have
local table={}
table.variableA=1 -- this is local?
local table2=
{
local variable2=2 -- this is the same that above?
}
and when I declare functions inside a local table, they are all locals?
Yes, but no.
Functions aren't local, they are just values. It's variables that are local or not. For example, imagine this:
function foo_global() print 'foo' end
local foo_local = foo_global
You have two variables, one global and one local, and they both contain the same function value.
When you store a function into a table, it's neither global nor local, as it's not a variable. The table can be stored in a local variable, but that doesn't really affect the function itself, only where you can access it from.
Inside a table constructor, that is, between curly braces { } you cannot use the local keyword, because it makes no sense there.

Lua - Getting a Localized Variable or Function without using the Debug Library with 2 seperate scripts

Ok so I am using a object orientated environment, I can make several scripts that may or may not connect with each other, all depending on if I want them to connect or not, and I want to get a function that is localized local myfunc = function() end from a different object that can handle code, from this point forward I will be calling these objects "Scripts" as they are what they are called in the game and are easily used for me to tell people what I am talking about even if they are not formally used as a name to suggest such a thing.
So lets say I have Script 1 with this code:
local myfunc = function() return true end
and I have Script 2 with a blank sheet, I want to make it so I can get myfunc without touching the debug library, making the original script a module script and returning the function, and this has to stay in 2 separate scripts. That is all for the requirements if you are wondering. I expect this can be done and I hope someone out there has the knowledge on how to do something like this clean and efficiently!
The whole point of a local variable is that it's local; other people can't touch it. The traditional means of having one script access data from another are modules or global variables. Both options which you have declared that you can't/won't do.
Your requirements reduce the set of possible solutions to zero.
Lua chunks can have return statements. To return a single function:
return function()
return true
end
To return a table with multiple functions:
return {
myFunc = function()
return true
end,
myOtherFunc = function()
return false
end,
}

How to pass variable by reference in Corona

I'm using Corona SDK.
I want to write a function which receives component as parameter and removes it like that:
function removeComponent(component)
if component then component:removeSelf() end
component = nil
end
Well, it works but my parameter does not get nil after using this function. Probably I have to pass it by reference, but I'm not sure it is possible with Corona.
This does not really make sense as presented in your example.
What exactly are you trying to accomplish? Is component a global ? Or a key in a table?
In your example, component is the name of a local variable in your function. Your component = nil only removes the value from the local variable, and thus will be lost.
If you want to have global effects, you'd need to pass the name of the variable you'd want to eliminate as string:
function removeComponent(component)
if _G[component] then -- exists globally?
_G[component]:removeSelf()
end
_G[component] = nil
end
Note that this style of programming (using the global table for this kind of things) is generally not a good idea. In the best case it can surprise you, in the worst case you end up zapping things like standard functions as print.
Therefore I'd recommend puttign things in their own table, and passing it on to the function.
Lua doesn't support passing by reference, but since it does support return values you can always achieve what you want using this idiomatic approach:
function removeComponent(component)
if component then component:removeSelf() end
return nil
end
And then call it like this:
a = removeComponent(a)
Edit: It's worth pointing out too that since Lua supports multiple return values and multiple assignments, you never actually need pass-by-reference. If you need several items updated, pass them in and return them and then do the call as
a,b = myFunction(a,b)
Its not different really than any other language. Passing by a value by reference (in C++ for example) would not stop any program from holding another copy of this same value elsewhere.
I know nothing of Corona, but this isn't really a Corona question so much as a Lua style question. However, if I had wrote it, I would ensure that the 'component' userdata or underlying value would clear itself up. If the userdata was accessed again, it should throw an error complaining about re-using a dead userdata.
I have written this code:
local component = display.newCircle(100, 100, 100);
local function removeComponent(c)
if component then component:removeSelf() end
component = nil
end
removeComponent(component)
if component == nil then print("Component is nil") else print("Component is not nil") end
And it prints "Component is nil". Perhaps you have a copy of your component somewere else or may be you forget to call function removeComponent or something else. Need to see more of your code

Create local variable in Lua

If I have a variable name("x" for example) stored in another variable (varname in this example) I can create a global variable via
_G[varname]=42
This is a complicated way to say
x=42
Now I want to do the same thing for local variables. Is it possible?
No, because local variables are not stored in a table, or in any structure which associates their name to them. When the lua code is compiled into bytecode, local variable names turn into numeric offsets on the lua stack.
If you really need to use a string to modify local variables, your best option is using a local table.
local localVars = {}
function setValue(varname, value)
localVars[varname] = value
end
function getValue(varname)
return localVars[varname]
end
You are not really creating and destroying local variables this way, but you get pretty close.

Resources