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

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
RetroExcavadora
Prole
Posts: 2
Joined: Tue Nov 15, 2016 6:38 pm

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

Post 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.
Last edited by RetroExcavadora on Thu Nov 17, 2016 3:26 pm, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
skyHights
Citizen
Posts: 78
Joined: Mon Aug 25, 2014 12:14 pm

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

Post by skyHights »

zorg wrote:(like linux, and probably even Mac OS X)
I don't know about linux, but forwards slashes work on mac.
Learning to Löve.
RetroExcavadora
Prole
Posts: 2
Joined: Tue Nov 15, 2016 6:38 pm

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

Post 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.
Post Reply

Who is online

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