Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Value to Variable in Lua?

Tags:

lua

In most languages, I can easily do some mathematical function (add, subtract, etc...) to a variable's current value in a short form like foo +=1. Is there something similar in Lua?

like image 531
Eli Avatar asked Sep 17 '13 22:09

Eli


People also ask

Can you do ++ in Lua?

++ is not a C function, it is an operator. So Lua being able to use C functions is not applicable. Possible duplicate of stackoverflow.com/questions/7855525/….

How do you write variables in Lua?

The syntax for declaring a global variable in Lua is pretty straightforward, just declare whatever name you want to use for your variable and assign a value to it. It should be noted that we cannot declare a global variable without assigning any value to it, as it is not something that Lua allows us to do.

Do you need to declare variables in Lua?

Global variables in Lua do not need declarations. Although this is handy for small programs, in larger programs a simple typo can cause bugs that are difficult to find. However, we can change that behavior if we like.

How do I use local variables in Lua?

You can access the local variables of any active function by calling getlocal , from the debug library. It has two parameters: the stack level of the function you are querying and a variable index. It returns two values: the name and the current value of that variable.


1 Answers

No, there is no shortcut to foo = foo + 1.

like image 155
lhf Avatar answered Oct 18 '22 11:10

lhf