Page 1 of 4
Game with separate files tryout
Posted: Thu Dec 14, 2017 6:10 pm
by SynfigMaster91
Hi, guys.
Until now, following tutorials of Sheepollution and Simple Game Tutorials, I could make a Pong clone and an Eyes game.
Now, I'm trying to learn by myself how to make a game with several lua files. I'm using a white square and I managed to making appear on the screen.
The game has two lua files: main.lua and square.lua (with the square I want to make move)
Unfortunately, I can't make the square move despite writing his actions on its code. I tried to fix it, but now... the square doesn't appear! What am I doing wrong?
Here's the main.lua code:
Code: Select all
require "square"
function love.load(arg)
end
function love.update( dt )
end
function love.draw()
end
And here, the square.lua code:
Code: Select all
local square = {x = 100, y = 100, width = 50, height = 50, speed = 20}
function square.update( dt )
if love.keyboard.isDown("right") then
square.x = x + speed * dt
end
if love.keyboard.isDown("left") then
square.x = x - speed * dt
end
if love.keyboard.isDown("up") then
square.y = y - speed * dt
end
if love.keyboard.isDown("down") then
square.y = y + speed * dt
end
end
function square.draw()
love.graphics.rectangle( 'fill', x, y, width, height )
end
Re: Game with separate files tryout
Posted: Thu Dec 14, 2017 6:15 pm
by ivan
You're almost there:
Code: Select all
function love.update( dt )
square.update(dt)
end
function love.draw()
square.draw()
end
Since your square is "local" it cannot be accessed outside of the square.lua file.
You have 2 options there, either keeping the "square" global OR returning the square to the main file:
Re: Game with separate files tryout
Posted: Thu Dec 14, 2017 6:20 pm
by SynfigMaster91
ivan wrote: ↑Thu Dec 14, 2017 6:15 pm
You're almost there:
Code: Select all
function love.update( dt )
square.update(dt)
end
function love.draw()
square.draw()
end
Since your square is "local" it cannot be accessed outside of the square.lua file.
You have 2 options there, either keeping the "square" global OR returning the square to the main file:
I did it, but I got this error:
Error
main.lua:8: attempt to index global 'square' (a nil value)
Traceback
main.lua:8: in function 'update'
[C]: in function 'xpcall'
Re: Game with separate files tryout
Posted: Thu Dec 14, 2017 6:26 pm
by ivan
Code: Select all
local square = {x = 100, y = 100, width = 50, height = 50, speed = 20}
function square.update( dt )
if love.keyboard.isDown("right") then
square.x = square.x + square.speed * dt
end
if love.keyboard.isDown("left") then
square.x = square.x - square.speed * dt
end
if love.keyboard.isDown("up") then
square.y = square.y - square.speed * dt
end
if love.keyboard.isDown("down") then
square.y = square.y + square.speed * dt
end
end
function square.draw()
love.graphics.rectangle( 'fill', square.x, square.y, square.width, square.height )
end
-- return the square local so we can access it outside of this file
return square
Code: Select all
-- grab the square local from the other file
local square = require("square")
function love.load(arg)
end
function love.update( dt )
square.update(dt)
end
function love.draw()
square.draw()
end
Re: Game with separate files tryout
Posted: Thu Dec 14, 2017 6:28 pm
by SynfigMaster91
ivan wrote: ↑Thu Dec 14, 2017 6:26 pm
Code: Select all
local square = {x = 100, y = 100, width = 50, height = 50, speed = 20}
function square.update( dt )
if love.keyboard.isDown("right") then
square.x = square.x + speed * dt
end
if love.keyboard.isDown("left") then
square.x = square.x - speed * dt
end
if love.keyboard.isDown("up") then
square.y = square.y - speed * dt
end
if love.keyboard.isDown("down") then
square.y = square.y + speed * dt
end
end
function square.draw()
love.graphics.rectangle( 'fill', square.x, square.y, square.width, square.height )
end
-- return the square local so we can access it outside of this file
return square
Code: Select all
-- grab the square local from the other file
local square = require("square")
function love.load(arg)
end
function love.update( dt )
square.update(dt)
end
function love.draw()
square.draw()
end
Now I got this:
Error: square.lua:19: bad argument #2 to 'rectangle' (number expected, got nil)
stack traceback:
[C]: in function 'rectangle'
square.lua:19: in function 'draw'
main.lua:12: in function 'draw'
[string "boot.lua"]:468: in function <[string "boot.lua"]:436>
[C]: in function 'xpcall'
Re: Game with separate files tryout
Posted: Thu Dec 14, 2017 8:16 pm
by xNick1
It says the second parameter you're passing is nil.
So try to put 100 instead of square.x and see if it solves the problem. If it does try to print square.x to see if it's nil. Then try to understand why it's nil xD
Re: Game with separate files tryout
Posted: Fri Dec 15, 2017 5:45 pm
by SynfigMaster91
xNick1 wrote: ↑Thu Dec 14, 2017 8:16 pm
It says the second parameter you're passing is nil.
So try to put 100 instead of square.x and see if it solves the problem. If it does try to print square.x to see if it's nil. Then try to understand why it's nil xD
I managed to make the square appear, but I got now this:
Error: square.lua:5: attempt to perform arithmetic on global 'speed' (a nil value)
stack traceback:
square.lua:5: in function 'update'
main.lua:8: in function 'update'
[string "boot.lua"]:464: in function <[string "boot.lua"]:436>
[C]: in function 'xpcall'
Program completed in 2.16 seconds (pid: 4
Code: Select all
local square = {}
function square.update( dt )
if love.keyboard.isDown("right") then
square.x = square.x + speed * dt
end
if love.keyboard.isDown("left") then
square.x = square.x - speed * dt
end
if love.keyboard.isDown("up") then
square.y = square.y - speed * dt
end
if love.keyboard.isDown("down") then
square.y = square.y + speed * dt
end
end
function square.draw()
love.graphics.rectangle( 'fill', 100, 100, 50, 50 )
end
-- return the square local so we can access it outside of this file
return square
If this keeps up, as much as I hate to say it here, I'm going to be mad and throw the towel and get the Give Up flag, literally-.
Re: Game with separate files tryout
Posted: Fri Dec 15, 2017 5:53 pm
by xNick1
SynfigMaster91 wrote: ↑Fri Dec 15, 2017 5:45 pm
xNick1 wrote: ↑Thu Dec 14, 2017 8:16 pm
It says the second parameter you're passing is nil.
So try to put 100 instead of square.x and see if it solves the problem. If it does try to print square.x to see if it's nil. Then try to understand why it's nil xD
I managed to make the square appear, but I got now this:
Error: square.lua:5: attempt to perform arithmetic on global 'speed' (a nil value)
stack traceback:
square.lua:5: in function 'update'
main.lua:8: in function 'update'
[string "boot.lua"]:464: in function <[string "boot.lua"]:436>
[C]: in function 'xpcall'
Program completed in 2.16 seconds (pid: 4
Code: Select all
local square = {}
function square.update( dt )
if love.keyboard.isDown("right") then
square.x = square.x + speed * dt
end
if love.keyboard.isDown("left") then
square.x = square.x - speed * dt
end
if love.keyboard.isDown("up") then
square.y = square.y - speed * dt
end
if love.keyboard.isDown("down") then
square.y = square.y + speed * dt
end
end
function square.draw()
love.graphics.rectangle( 'fill', 100, 100, 50, 50 )
end
-- return the square local so we can access it outside of this file
return square
If this keeps up, as much as I hate to say it here, I'm going to be mad and throw the towel and get the Give Up flag, literally-.
Speed is under the table called square.
So it's square.speed as you did before with square.x etc
Or even declare a variable called speed since you haven't got one
Re: Game with separate files tryout
Posted: Fri Dec 15, 2017 6:05 pm
by SynfigMaster91
xNick1 wrote: ↑Fri Dec 15, 2017 5:53 pm
SynfigMaster91 wrote: ↑Fri Dec 15, 2017 5:45 pm
xNick1 wrote: ↑Thu Dec 14, 2017 8:16 pm
It says the second parameter you're passing is nil.
So try to put 100 instead of square.x and see if it solves the problem. If it does try to print square.x to see if it's nil. Then try to understand why it's nil xD
I managed to make the square appear, but I got now this:
Error: square.lua:5: attempt to perform arithmetic on global 'speed' (a nil value)
stack traceback:
square.lua:5: in function 'update'
main.lua:8: in function 'update'
[string "boot.lua"]:464: in function <[string "boot.lua"]:436>
[C]: in function 'xpcall'
Program completed in 2.16 seconds (pid: 4
Code: Select all
local square = {}
function square.update( dt )
if love.keyboard.isDown("right") then
square.x = square.x + speed * dt
end
if love.keyboard.isDown("left") then
square.x = square.x - speed * dt
end
if love.keyboard.isDown("up") then
square.y = square.y - speed * dt
end
if love.keyboard.isDown("down") then
square.y = square.y + speed * dt
end
end
function square.draw()
love.graphics.rectangle( 'fill', 100, 100, 50, 50 )
end
-- return the square local so we can access it outside of this file
return square
If this keeps up, as much as I hate to say it here, I'm going to be mad and throw the towel and get the Give Up flag, literally-.
Speed is under the table called square.
So it's square.speed as you did before with square.x etc
Or even declare a variable called speed since you haven't got one
Now I got this:
Error: square.lua:6: attempt to perform arithmetic on field 'speed' (a nil value)
stack traceback:
square.lua:6: in function 'update'
main.lua:8: in function 'update'
[string "boot.lua"]:464: in function <[string "boot.lua"]:436>
[C]: in function 'xpcall'
square.lua
Code: Select all
local square = {}
speed = 40
function square.update( dt )
if love.keyboard.isDown("right") then
square.x = square.x + square.speed * dt
end
if love.keyboard.isDown("left") then
square.x = square.x - square.speed * dt
end
if love.keyboard.isDown("up") then
square.y = square.y - square.speed * dt
end
if love.keyboard.isDown("down") then
square.y = square.y + square.speed * dt
end
end
function square.draw()
love.graphics.rectangle( 'fill', 100, 100, 50, 50 )
end
-- return the square local so we can access it outside of this file
return square
main.lua
Code: Select all
-- grab the square local from the other file
local square = require("square")
function love.load(arg)
end
function love.update( dt )
square.update(dt)
end
function love.draw()
square.draw()
end
Just, why? Why LÖVE doesn't love me?
Re: Game with separate files tryout
Posted: Fri Dec 15, 2017 6:11 pm
by xNick1
I said "or even".
You either use square.speed or use speed and declare the variable.
Please consider using some Lua tutorial:
http://tylerneylon.com/a/learn-lua/
You won't have such issues anymore!
Nah man, love is user friendly.
It's just picky about who his friends are