Page 1 of 1

Total beginner lost in its first "separated" program [Solved]

Posted: Tue Nov 15, 2016 7:00 pm
by RetroExcavadora
Hi, I'm trying to make my first simple game separating the elements (player, enemies... ), but I see I'm lost in the first step. I'm trying to only draw a rectangle as a player on a black window, and these are the three files I'm working with.

conf.lua

Code: Select all

function love.conf(t)
  t.window.title = "Juego de prueba"
  t.window.width = 800
  t.window.height = 600

  --t.console = true
  t.modules.touch = false
end
main.lua

Code: Select all

require("jugador/Jugador")

function love.load()
  Jugador:new()
end

function love.update(dt)
  Jugador:update(dt)
end

function love.draw()
  Jugador:draw()
end

--function love.quit()
  -- body...
--end
jugador/Jugador.lua

Code: Select all

local Jugador = {}

function Jugador:new()
  Jugador.x = 0 
  Jugador.y = 550 
  Jugador.speed = 10 
end

function Jugador:update(dt)

  if love.keyboard.isDown("right") then 
     Jugador.x = Jugador.x + Jugador.speed
  end
  if love.keyboard.isDown("left") then 
    Jugador.x = Jugador.x - Jugador.speed
  end
  if love.keyboard.isDown("down") then 
    Jugador.y = Jugador.y + Jugador.speed
  end
  if love.keyboard.isDown("up") then 
    Jugador.y = Jugador.y - Jugador.speed
  end
end

function Jugador:draw()
  love.graphics.rectangle("fill", Jugador.x, Jugador.y, 80, 20)
end
And this is the result I get when I start the program:

Code: Select all

connect(2) call to /dev/shm/jack-1000/default/jack_0 failed (err=No such file or directory)
attempt to connect to server failed
Error: main.lua:6: attempt to index global 'Jugador' (a nil value)
stack traceback:
        main.lua:6: in function 'load'
        [string "boot.lua"]:440: in function <[string "boot.lua"]:436>
        [C]: in function 'xpcall'
Sorry to be so clumsy.

Re: Total beginner lost in its first "separated" program

Posted: Tue Nov 15, 2016 7:12 pm
by zorg
Hi there!

You have two issues with the code; the first is that you're missing a return Jugador from Jugador.lua, and you need the first line in your main.lua to be local Jugador = require("jugador/Jugador") since you made it a local inside that file (which is a good thing), but that also means you need to return it and assign it to a variable in your main.lua

The other issue is that your Jugador "class" will work fine with one player instance... but no more, since you're not using self anywhere, but rather the Jugador table itself.

Re: Total beginner lost in its first "separated" program

Posted: Tue Nov 15, 2016 9:44 pm
by pgimeno
zorg wrote:local Jugador = require("jugador/Jugador")
Does that syntax work in all systems? I thought it was require("jugador.Jugador"), with a dot instead of a slash.

Re: Total beginner lost in its first "separated" program

Posted: Tue Nov 15, 2016 9:53 pm
by zorg
pgimeno wrote:
zorg wrote:local Jugador = require("jugador/Jugador")
Does that syntax work in all systems? I thought it was require("jugador.Jugador"), with a dot instead of a slash.
You're right, it should be with a dot, though it should work with the forward slash too, on unices (like linux, and probably even Mac OS X) and i believe it may also work on windows like this as well... but yeah, lua says to use a dot, since require takes patterns, not paths.

Re: Total beginner lost in its first "separated" program

Posted: Thu Nov 17, 2016 7:04 am
by skyHights
zorg wrote:(like linux, and probably even Mac OS X)
I don't know about linux, but forwards slashes work on mac.

Re: Total beginner lost in its first "separated" program

Posted: Thu Nov 17, 2016 3:23 pm
by RetroExcavadora
zorg wrote:Hi there!

You have two issues with the code; the first is that you're missing a return Jugador from Jugador.lua, and you need the first line in your main.lua to be local Jugador = require("jugador/Jugador") since you made it a local inside that file (which is a good thing), but that also means you need to return it and assign it to a variable in your main.lua

The other issue is that your Jugador "class" will work fine with one player instance... but no more, since you're not using self anywhere, but rather the Jugador table itself.
Thank you very much for you comment, it helped me to solve my problem and now I have a little more about the basics to continue my learning.

I know that it can be improvable, but this workaround work for me. I will paste the modified files here:

main.lua

Code: Select all

Jugador = require("jugador.Jugador") 

jugador = Jugador:crear()

function love.load()
  jugador:basicos()
end

function love.update(dt)
  -- body...

  jugador:update(dt)
end

function love.draw()
  -- body...
  jugador:draw()
end

--function love.quit()
  -- body...
--end
jugador/Jugador.lua

Code: Select all

local Jugador = {}

function Jugador:crear()
  local jugador = {}

  function jugador:basicos()
    jugador.x = 0 
    jugador.y = 550 
    jugador.speed = 10 
  end

  --Controles del jugador
  function jugador:update(dt)
    if love.keyboard.isDown("right") then 
    jugador.x = jugador.x + jugador.speed
    end
    if love.keyboard.isDown("left") then 
      jugador.x = jugador.x - jugador.speed
    end
    if love.keyboard.isDown("down") then 
      jugador.y = jugador.y + jugador.speed
    end
    if love.keyboard.isDown("up") then 
      jugador.y = jugador.y - jugador.speed
    end
  end

  -- Función de dibujar
  function jugador:draw()
    love.graphics.rectangle("fill", jugador.x, jugador.y, 80, 20)
  end

  return jugador
end

return Jugador
About the slash, I use Manjaro Linux and that worked for me in previous examples I followed. I see that my error (with the class) was extremely silly from me.

Sorry if I left some comment in Spanish in my workaround. Since now I will strongly respect the Lua syntax.