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
Any question about lua
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Any question about lua
Well, Lua has no arrays, it has tables.
So, you'll have to create a table first, and give it the name y:
You can then fill it with values.
So, you'll have to create a table first, and give it the name y:
Code: Select all
y = {}
Help us help you: attach a .love.
Re: Any question about lua
I do so:
Tell me where the mistakes?
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
- nevon
- Commander of the Circuloids
- Posts: 938
- Joined: Thu Feb 14, 2008 8:25 pm
- Location: Stockholm, Sweden
- Contact:
Re: Any question about lua
vadim2 wrote:Tell me where the mistakes?
Code: Select all
y0 = y.i
Code: Select all
y0 = y[i]
Re: Any question about lua
Tnx, almost work
Re: Any question about lua
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:
So your draw function should be
And you made some minor mistakes:
I made some modifications - check it out (you also can add more menu items without the need to change your code !)
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
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])
- 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 !
-
is wrong, because indexes in lua begin with 1..nlove.keyboard.isDown("up") then if i>=0 then
-
- you never update the value of y0 !!love.graphics.print(text,x0,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
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Any question about lua
Most of your post is correct, however that piece contains horrible misinformation.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
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.
Re: Any question about lua
Adamantos, tnx, I almost do not know lua, and studied it from the other examples.
Who is online
Users browsing this forum: Ahrefs [Bot] and 12 guests