FAQ  •  Register  •  Login

questions about Lua

<<

tazinator

Posts: 352

Joined: Fri Jul 05, 2013 7:35 pm

Post Wed Mar 26, 2014 8:34 pm

questions about Lua

Q: Can all functions call on local?

Q: How many ticks will local persist? e.g. local rar = ship:getVel()

Q: How many ticks will data last if I DON'T use local? rar = ship:getVel()

Q: when i run code, does it happen on the next tick or the current tick?

Q: Are there timers for ticks?
Play my new level! Two different teams fight over a nexus: One mainly defends while the other attacks! is fun
viewtopic.php?f=33&p=21002#p21002
<<

Fordcars

User avatar

Posts: 1016

Joined: Fri Apr 20, 2012 3:51 pm

Location: Some city, somewhere

Post Wed Mar 26, 2014 8:50 pm

Re: questions

A local is a variable that is only accessible in the current scope. A scope is everything that has an 'end'. So:

  Code:
function foo()
    local name = "My name"
    logprint(name) -- Works

    if(1<2) then
        logprint(name) -- Works

        local age = 20
    end -- All variables declared in this scope (if) are destroyed. So, in this case, only age is destroyed

    logprint(age) -- Error, variable doesn't exist!
end -- name is destroyed


All variables that you want to use throughout your code should be 'global'. To have a 'global' variable, all you must do is to declare it in the main() function:

  Code:
function main()
    variable = "I am a global variable, yo!"
end

function onTick()
    logprint(variable) -- Works!

    local anotherVariable = "Hai"
end

function hello()
    logprint(variabel) -- Works!
    logprint(anotherVariable) -- Doesn't work!
end


When your script starts, it will run the function main() first. In Bitfighter, there is the onTick() function that triggers at each frame (around 60 times per second). It's that simple :D

You can have timers too, check out http://bitfighter.org/luadocs/class_timer.html :)
skybax: Why is this health pack following me?
bobdaduck: uh, it likes you.

Return to Technical Support

Who is online

Users browsing this forum: No registered users and 5 guests

cron