Game with separate files tryout

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.
SynfigMaster91
Prole
Posts: 17
Joined: Thu Nov 23, 2017 4:46 pm

Re: Game with separate files tryout

Post by SynfigMaster91 »

xNick1 wrote: Fri Dec 15, 2017 6:11 pm I said "or even".
You either call square.speed or call speed and declare the variable.
But I did so!

local square = {}
speed = 40

And changed this!

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

But nothing!
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: Game with separate files tryout

Post by xNick1 »

square.x = square.x + 40 * dt

Step by step.
Then you're gonna investigate why your code didn't work.
It won't work anyway cause you're using an empty table now.
square.x doesn't exist anymore.
You needed it.
If you don't wanna use a table you have at least to declare a variable called x and use that one
Last edited by xNick1 on Fri Dec 15, 2017 6:35 pm, edited 1 time in total.
SynfigMaster91
Prole
Posts: 17
Joined: Thu Nov 23, 2017 4:46 pm

Re: Game with separate files tryout

Post by SynfigMaster91 »

xNick1 wrote: Fri Dec 15, 2017 6:11 pm 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
I did this:

Code: Select all

local square = {x = 10, y = 10, speed = 50}

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', x, y, 50, 50 )
end

-- return the square local so we can access it outside of this file
return square
But now...

Error

square.lua:19: bad argument #2 to 'rectangle' (number expected, got nil)

Traceback

[C]: in function 'rectangle'
square.lua:19: in function 'draw'
main.lua:12: in function 'draw'
[C]: in function 'xpcall'

:x :x :x
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: Game with separate files tryout

Post by xNick1 »

Now you're using the table again, so it's square.x in your draw callback etc

Please consider following a tutorial about variables and tables
SynfigMaster91
Prole
Posts: 17
Joined: Thu Nov 23, 2017 4:46 pm

Re: Game with separate files tryout

Post by SynfigMaster91 »

xNick1 wrote: Fri Dec 15, 2017 6:36 pm Now you're using the table again, so it's square.x in your draw callback etc

Please consider following a tutorial about variables and tables
I know about variables, but I'm doing this.
local square

function square.update( dt )
if love.keyboard.isDown("right") then
square.x = 10 + 40 * dt
end
if love.keyboard.isDown("left") then
square.x = 10 - 40 * dt
end
if love.keyboard.isDown("up") then
square.y = 10 - 40 * dt
end
if love.keyboard.isDown("down") then
square.y = 10 + 40 * dt
end
end

function square.draw()
love.draw.rectangle('fill', 10, 10, 50, 50)
end

-- return the square local so we can access it outside of this file
return square
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Game with separate files tryout

Post by MadByte »

SynfigMaster91 wrote: Fri Dec 15, 2017 6:14 pm ...

local square = {}
speed = 40

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

...
This one seems to be alright. Maybe the error now is somewhere else?
I thought it might help you to see a similar example to identify the problem you have in your code so I attached the moving square example.
Hope it helps.

To see the lua files, unpack it with any file archiver (7-zip, winrar, etc).
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: Game with separate files tryout

Post by xNick1 »

SynfigMaster91 wrote: Fri Dec 15, 2017 6:44 pm
xNick1 wrote: Fri Dec 15, 2017 6:36 pm Now you're using the table again, so it's square.x in your draw callback etc

Please consider following a tutorial about variables and tables
I know about variables, but I'm doing this.
local square

function square.update( dt )
if love.keyboard.isDown("right") then
square.x = 10 + 40 * dt
end
if love.keyboard.isDown("left") then
square.x = 10 - 40 * dt
end
if love.keyboard.isDown("up") then
square.y = 10 - 40 * dt
end
if love.keyboard.isDown("down") then
square.y = 10 + 40 * dt
end
end

function square.draw()
love.draw.rectangle('fill', 10, 10, 50, 50)
end

-- return the square local so we can access it outside of this file
return square
This way, square.x doesn't exist
And square wouldn't have any value, so you don't need it.
Please read the error messages, they're saying what's wrong with your code!
SynfigMaster91
Prole
Posts: 17
Joined: Thu Nov 23, 2017 4:46 pm

Re: Game with separate files tryout

Post by SynfigMaster91 »

MadByte wrote: Fri Dec 15, 2017 6:47 pm
SynfigMaster91 wrote: Fri Dec 15, 2017 6:14 pm ...

local square = {}
speed = 40

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

...
This one seems to be alright. Maybe the error now is somewhere else?
I thought it might help you to see a similar example to identify the problem you have in your code so I attached the moving square example.
Hope it helps.

To see the lua files, unpack it with any file archiver (7-zip, winrar, etc).
I did as you said, but...

Error: Syntax error: square.lua:11: unexpected symbol near '10'

stack traceback:
[C]: at 0x68d0f9a0
[C]: in function 'require'
main.lua:2: in main chunk
[C]: in function 'require'
[string "boot.lua"]:429: in function <[string "boot.lua"]:275>
[C]: in function 'xpcall'

I'm more desperate than ever! :x
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: Game with separate files tryout

Post by xNick1 »

MadByte wrote: Fri Dec 15, 2017 6:47 pm
SynfigMaster91 wrote: Fri Dec 15, 2017 6:14 pm ...

local square = {}
speed = 40

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

...
This one seems to be alright. Maybe the error now is somewhere else?
I thought it might help you to see a similar example to identify the problem you have in your code so I attached the moving square example.
Hope it helps.

To see the lua files, unpack it with any file archiver (7-zip, winrar, etc).
He deleted the variables in the table. It was crashing cause there were no square.x etc
SynfigMaster91
Prole
Posts: 17
Joined: Thu Nov 23, 2017 4:46 pm

Re: Game with separate files tryout

Post by SynfigMaster91 »

xNick1 wrote: Fri Dec 15, 2017 6:54 pm
MadByte wrote: Fri Dec 15, 2017 6:47 pm
SynfigMaster91 wrote: Fri Dec 15, 2017 6:14 pm ...

local square = {}
speed = 40

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

...
This one seems to be alright. Maybe the error now is somewhere else?
I thought it might help you to see a similar example to identify the problem you have in your code so I attached the moving square example.
Hope it helps.

To see the lua files, unpack it with any file archiver (7-zip, winrar, etc).
He deleted the variables in the table. It was crashing cause there were no square.x etc
Then this doesn't help...

One more strike and I leave LÖVE and Lua forever!
Post Reply

Who is online

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