ExtraTerrestrial
ExtraTerrestrial

Reputation: 11

Updating global variables from a function in Lua

Ok this may be a dumb question but Im new to programing so here it gose. I've written a function that I hope to use in a rpg style game to make the player level up based on exp

--sets the level based on exp
function levelCheck(exp,level) 
   repeat 
         c=math.sqrt(exp)-(level*4) 
            if ( c>=1 ) then 
               level=level+1 
               print("Congradulations level "..level) 
            end 
   until ( c<1) 
   return level
end

Thing is i want this to update the level varible globaly I'm not sure I'm saying that right but please help.

Upvotes: 1

Views: 1390

Answers (2)

ExtraTerrestrial
ExtraTerrestrial

Reputation: 11

It's true level isn't just a number but the plan is to use level as a variable in other equations such as deff=level*tough+equipmentfor example idk that's the plan

Upvotes: 0

Dimitry
Dimitry

Reputation: 2348

Global variables in lua are updated from the function whenever you address them by their global name:

variable=1
f=function(x) variable=x end
f()

Function can alter its argument (actually contents of its argument) if it is a table:

f=function(t)
    t.variable=4
end

...which answers the title, but not the problem you have. As Nicol Boras commented, you should rethink your ways if your level is more than just a number.

Upvotes: 1

Related Questions