Page 1 of 2

Do i need to learn lua before learning löve? Or can I learn it both at the same time?

Posted: Wed Apr 22, 2020 12:54 pm
by Bodeman
Do I need to learn lua before learning löve? Or can I learn it both at the same time? I am new with programming (started yesterday) only watched a couple of tutorials. But Got to think that i needed to learn lua first. Because there where some tutorials where they said that you only can make games in löve if you know lua.
(Sorry if this is not the correct forum for asking this.)

Re: Do i need to learn lua before learning löve? Or can I learn it both at the same time?

Posted: Wed Apr 22, 2020 1:41 pm
by Varkas
You can learn both at the same time. Love2d "just" adds extra functionality to Lua. So you can start small, learn Lua basics and use a bit of that functionality, and then continue to learn more. Worked for me, but I had experiences with other programming languages, so Lua was not totally foreign to me, just a bit "unusual"

Re: Do i need to learn lua before learning löve? Or can I learn it both at the same time?

Posted: Wed Apr 22, 2020 2:43 pm
by Bodeman
Ok thanks! Can I ask you another question?

Re: Do i need to learn lua before learning löve? Or can I learn it both at the same time?

Posted: Wed Apr 22, 2020 3:04 pm
by pgimeno
"Don't ask if you can ask, just ask"

Re: Do i need to learn lua before learning löve? Or can I learn it both at the same time?

Posted: Thu Apr 23, 2020 10:05 am
by Bodeman
oh ok sorry, I am following a tutorial but Im getting syntax errors can this be because this tutorial is from a older version of love2d?
If you want to see it this is my code:
The syntax error said that it had something to do with the line player.fire_sound = love.audio.newSource("shoot.wav")
love.graphics.setDefaultFilter("nearest", "nearest")
enemy = {}
enemies_controller = {}
enemies_controller.enemies = {}
enemies_controller.image = love.graphics.newImage("enemy.png")
function love.load()
player = {}
player.x = 0
player.y = 110
player.bullets = {}
player.cooldown = 20
player.speed = 2
player.image = love.graphics.newImage("player.png")
player.fire_sound = love.audio.newSource("shoot.wav")
player.fire = function()
if player.cooldown <= 0 then
love.audio.play(player.fire_sound)
player.cooldown = 20
bullet = {}
bullet.x = player.x + 4
bullet.y = player.y - 1
table.insert(player.bullets, bullet)
end
end
enemies_controller:spawnEnemy(0, 0)
enemies_controller:spawnEnemy(20, 0)

end

function enemies_controller:spawnEnemy(x, y)
enemy = {}
enemy.x = x
enemy.y = y
enemy.bullets = {}
enemy.cooldown = 20
enemy.speed = 2
table.insert(self.enemies, enemy)
end

function enemy:fire()
if self.cooldown <= 0 then
self.cooldown = 20
bullet = {}
bullet.x = self.x + 35
bullet.y = self.y
table.insert(self.bullets, bullet)
end
end

function love.update(dt)
player.cooldown = player.cooldown -1
if love.keyboard.isDown("right") then
player.x = player.x + player.speed
elseif love.keyboard.isDown("left") then
player.x = player.x - player.speed
end

if love.keyboard.isDown("space") then
player.fire()
end

for _,e in pairs(enemies_controller.enemies) do
e.y = e.y + 1
end

for i,b in ipairs(player.bullets) do
if b.y < -5 then
table.remove(player.bullets, i)
end
b.y = b.y - 2
end
end


function love.draw()
love.graphics.scale(5)
--draw player
love.graphics.setColor(255, 255, 255)
love.graphics.draw(player.image, player.x, player.y)

--draw enemy
for _,e in pairs(enemies_controller.enemies) do
love.graphics.draw(enemies_controller.image, e.x, e.y, 0, 1)
end

--draw bullets
love.graphics.setColor(225, 225, 225)
for _,b in pairs(player.bullets) do
love.graphics.rectangle("fill", b.x, b.y, 2, 2)
end
end


Thanks for helping! :)

Re: Do i need to learn lua before learning löve? Or can I learn it both at the same time?

Posted: Thu Apr 23, 2020 11:57 am
by steVeRoll
The code you posted doesn't seem to have any syntax errors. The error was that love.audio.newSource expects a second argument, which is the source type ("static" or "stream"). In previous versions of love it defaulted to some value, but in the newer versions you have to specify it yourself (usually "static" is used for sound effects, and "stream" is used for music).

Also, another incompatibility that won't cause errors but will look weird: in older versions of love, color values were in the range 0 - 255, but in the newer versions this has been changed to 0 - 1. So in the setColor calls, replace 255 with 1, and 225 with 225/255.

Re: Do i need to learn lua before learning löve? Or can I learn it both at the same time?

Posted: Thu Apr 23, 2020 1:09 pm
by pgimeno
Also, please use [code] ... [/code] tags when you post code in the forums; it helps with readability. Here's your code using these tags:

Code: Select all

love.graphics.setDefaultFilter("nearest", "nearest")
enemy = {}
enemies_controller = {}
  enemies_controller.enemies = {}
  enemies_controller.image =  love.graphics.newImage("enemy.png")
function love.load()
player = {}
player.x = 0
player.y = 110
player.bullets = {}
player.cooldown = 20
player.speed = 2
player.image = love.graphics.newImage("player.png")
player.fire_sound = love.audio.newSource("shoot.wav")
player.fire = function()
 if player.cooldown <= 0 then
    love.audio.play(player.fire_sound)
  player.cooldown = 20
  bullet = {}
  bullet.x = player.x + 4
  bullet.y = player.y - 1
  table.insert(player.bullets, bullet)
end
end
  enemies_controller:spawnEnemy(0, 0)
  enemies_controller:spawnEnemy(20, 0)

end

function enemies_controller:spawnEnemy(x, y)
  enemy = {}
  enemy.x = x
  enemy.y = y
  enemy.bullets = {}
  enemy.cooldown = 20
  enemy.speed = 2
  table.insert(self.enemies, enemy)
end

function enemy:fire()
if self.cooldown <= 0 then
  self.cooldown = 20
  bullet = {}
  bullet.x = self.x + 35
  bullet.y = self.y
  table.insert(self.bullets, bullet)
end
end

function love.update(dt)
  player.cooldown = player.cooldown -1
  if love.keyboard.isDown("right") then
    player.x = player.x + player.speed
  elseif love.keyboard.isDown("left") then
    player.x = player.x - player.speed
  end

  if love.keyboard.isDown("space") then
    player.fire()
  end

for _,e in pairs(enemies_controller.enemies) do
  e.y = e.y + 1
end

  for i,b in ipairs(player.bullets) do
    if b.y < -5 then
      table.remove(player.bullets, i)
    end
    b.y = b.y - 2
  end
end


function love.draw()
  love.graphics.scale(5)
  --draw player
  love.graphics.setColor(255, 255, 255)
  love.graphics.draw(player.image, player.x, player.y)

--draw enemy
for _,e in pairs(enemies_controller.enemies) do
  love.graphics.draw(enemies_controller.image, e.x, e.y, 0, 1)
end

  --draw bullets
  love.graphics.setColor(225, 225, 225)
  for _,b in pairs(player.bullets) do
    love.graphics.rectangle("fill", b.x, b.y, 2, 2)
  end
end

Re: Do i need to learn lua before learning löve? Or can I learn it both at the same time?

Posted: Fri Apr 24, 2020 10:52 am
by Bodeman
oh ok thank you, Im sorry if this is annoying all these questions but can you help me with one last problem?
when i shoot the enemies with a bullet both the enemies go away. Do you know how I fix this?
(the enemy right is working)

Thanks.

Code: Select all

 love.graphics.setDefaultFilter("nearest", "nearest")
enemy = {}
enemies_controller = {}
  enemies_controller.enemies = {}
  enemies_controller.image =  love.graphics.newImage("enemy.png")

function checkCollisions(enemies,  bullets)
for _, e in pairs(enemies) do
  for _, b in pairs(bullets) do
    if b.y <= e.y + e.height and b.x > e.x and b.x < e.x + e.width then
      table.remove(enemies, i)
      end
    end
  end
end
function love.load()
player = {}
player.x = 0
player.y = 110
player.bullets = {}
player.cooldown = 20
player.speed = 2
player.image = love.graphics.newImage("player.png")
player.fire_sound = love.audio.newSource("shoot.wav", "static")
player.fire = function()
  if player.cooldown <= 0 then
    love.audio.play(player.fire_sound)
  player.cooldown = 20
  bullet = {}
  bullet.x = player.x + 4
  bullet.y = player.y - 1
  table.insert(player.bullets, bullet)
  end
end
  enemies_controller:spawnEnemy(0, 0)
  enemies_controller:spawnEnemy(40, 0)

end

function enemies_controller:spawnEnemy(x, y)
  enemy = {}
  enemy.x = x
  enemy.y = y
  enemy.width = 10
  enemy.height = 10
  enemy.bullets = {}
  enemy.cooldown = 20
  enemy.speed = 2
  table.insert(self.enemies, enemy)
end

function enemy:fire()
if self.cooldown <= 0 then
  self.cooldown = 20
  bullet = {}
  bullet.x = self.x + 35
  bullet.y = self.y
  table.insert(self.bullets, bullet)
  end
end

function love.update(dt)
  player.cooldown = player.cooldown -1

  if love.keyboard.isDown("right") then
    player.x = player.x + player.speed
  elseif love.keyboard.isDown("left") then
    player.x = player.x - player.speed
  end

  if love.keyboard.isDown("space") then
    player.fire()
  end

for _,e in pairs(enemies_controller.enemies) do
  e.y = e.y + 1
end

  for i,b in ipairs(player.bullets) do
    if b.y < -5 then
      table.remove(player.bullets, i)
    end
    b.y = b.y - 2
  end
  checkCollisions(enemies_controller.enemies, player.bullets)
end


function love.draw()
  love.graphics.scale(5)
  --draw player
  love.graphics.setColor(1, 1, 1)
  love.graphics.draw(player.image, player.x, player.y)

--draw enemy
for _,e in pairs(enemies_controller.enemies) do
  love.graphics.draw(enemies_controller.image, e.x, e.y, 0, 1)
end

  --draw bullets
  love.graphics.setColor(1, 1, 1)
  for _,b in pairs(player.bullets) do
    love.graphics.rectangle("fill", b.x, b.y, 2, 2)
  end
end

Re: Do i need to learn lua before learning löve? Or can I learn it both at the same time?

Posted: Fri Apr 24, 2020 12:05 pm
by sphyrth

Code: Select all

function checkCollisions(enemies,  bullets)
for _, e in pairs(enemies) do
  for _, b in pairs(bullets) do
    if b.y <= e.y + e.height and b.x > e.x and b.x < e.x + e.width then
      table.remove(enemies, i)
      end
    end
  end
end
This is what I got from a glance. This line...
table.remove(enemies, i)

You have no "i" or index to delete. It's not referencing anything. That "i" should go here:

Code: Select all

for i, e in pairs(enemies) do
It's also a good practice to:
1. Mark your items for deletion first.
2. And then deleting them from the last item to the first.

Re: Do i need to learn lua before learning löve? Or can I learn it both at the same time?

Posted: Fri Apr 24, 2020 6:26 pm
by ErtYwek
Bodeman wrote: Wed Apr 22, 2020 12:54 pm Do I need to learn lua before learning löve? Or can I learn it both at the same time? I am new with programming (started yesterday) only watched a couple of tutorials. But Got to think that i needed to learn lua first. Because there where some tutorials where they said that you only can make games in löve if you know lua.
(Sorry if this is not the correct forum for asking this.)
I recommend you learn the same way I did. Install Roblox Studio then get some free models and edit scripts inside of them and check what your changes did. Learning Lua in roblox is much much simpler than there in LOVE2D engine.