Any question about lua

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
vadim2
Prole
Posts: 7
Joined: Sun Jan 03, 2010 9:20 am

Any question about lua

Post by vadim2 »

Hi all. Yesterday discovered love2d and now to fool around with its features (versed in them.) The question arose in Lua.
There are a few constants y1, y2, ... and variable y0, which is equal to any of the constants. When you press up the variable "i" is incremented by 1 and then
y0 = y.i (for example)
How to do that if the "i" = 1, then y0 = y1?
I had thought to make the array "y" and in it the values and then
y0 = y
How to do it right?
Thanks(or no) for the translation of google :)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Any question about lua

Post by Robin »

Well, Lua has no arrays, it has tables.

So, you'll have to create a table first, and give it the name y:

Code: Select all

y = {}
You can then fill it with values.
Help us help you: attach a .love.
vadim2
Prole
Posts: 7
Joined: Sun Jan 03, 2010 9:20 am

Re: Any question about lua

Post by vadim2 »

I do so:

Code: Select all

function love.load()
    love.graphics.setFont(16)
    y = {300, 280, 260}
    menu = {"New Game", "Score", "Exit"}
    text = "-->"
    i = 1
    x = 300
    x0 = 280
    y0 = y.i
end

function love.update(dt)
    if love.keyboard.isDown("up") then
        if i>=0 then
        i = i - 1
        end
    elseif love.keyboard.isDown("down") then
        if i<=3 then
        i = i + 1
       end
    end
end

function love.draw()
    love.graphics.print(text,x0,y0)
    love.graphics.print(menu.1,x,y[1])
    love.graphics.print(menu.2,x,y[2])
    love.graphics.print(menu.3,x,y[3])
end
Tell me where the mistakes?
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Any question about lua

Post by nevon »

vadim2 wrote:Tell me where the mistakes?

Code: Select all

y0 = y.i
Should be

Code: Select all

y0 = y[i]
vadim2
Prole
Posts: 7
Joined: Sun Jan 03, 2010 9:20 am

Re: Any question about lua

Post by vadim2 »

Tnx, almost work
User avatar
Adamantos
Prole
Posts: 27
Joined: Sun May 16, 2010 10:47 pm

Re: Any question about lua

Post by Adamantos »

Hello vadim2,

I really can encourage you, to read the section in the lua manuel about tables (http://www.lua.org/pil/2.5.html)
Gernerally you address an element of a table by t["index"]. If the index is a string, there is a shorter notation:

Code: Select all

 t = { 1 = 123, 2 = 234, 3 = 333, a = 555, b = 666, c = 777}
 t["1"] is equal to t[1]
 t["a"] is equal to t.a
So your draw function should be

Code: Select all

love.graphics.print(menu[1],x,y[1])
love.graphics.print(menu[2],x,y[2])
love.graphics.print(menu[3],x,y[3])
And you made some minor mistakes:
  • y0 = y.i should be y0 = y
  • you check the keyboard in the love.update() function, so on every frame - this would be way too fast !
  • love.keyboard.isDown("up") then if i>=0 then
    is wrong, because indexes in lua begin with 1..n
  • love.graphics.print(text,x0,y0)
    - you never update the value of y0 !!



I made some modifications - check it out (you also can add more menu items without the need to change your code !)

Code: Select all

function love.load()
  love.graphics.setFont(16)
  y = {260,280,300}
  menu = {"New Game", "Score", "Exit"}
  text = "-->"
  select = 1
  x = 300
end


function love.keypressed(key)
  if key == "down"  then select = math.min(#menu, select+1) end
  if key == "up"    then select = math.max(1,select-1) end
end


function love.draw()
  love.graphics.print(text,x-40,y[select])
  for i,name in ipairs(menu) do
    love.graphics.print(name,x,y[i])
  end
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Any question about lua

Post by Robin »

Adamantos wrote:

Code: Select all

 t = { 1 = 123, 2 = 234, 3 = 333, a = 555, b = 666, c = 777}
 t["1"] is equal to t[1]
 t["a"] is equal to t.a
Most of your post is correct, however that piece contains horrible misinformation.

Code: Select all

 t = { [1] = 123, [2] = 234, [3] = 333, a = 555, b = 666, c = 777} -- if the key is not a string and a valid identifier, you have to include [] here
 t["1"] is NOT equal to t[1]
 t["a"] is equal to t.a
Help us help you: attach a .love.
vadim2
Prole
Posts: 7
Joined: Sun Jan 03, 2010 9:20 am

Re: Any question about lua

Post by vadim2 »

Adamantos, tnx, I almost do not know lua, and studied it from the other examples.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 10 guests