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

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.
Bodeman
Prole
Posts: 4
Joined: Wed Apr 22, 2020 12:43 pm

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

Post 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.)
User avatar
Varkas
Citizen
Posts: 83
Joined: Mon Mar 09, 2020 2:26 pm

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

Post 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"
In soviet russia, code debugs you.
Bodeman
Prole
Posts: 4
Joined: Wed Apr 22, 2020 12:43 pm

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

Post by Bodeman »

Ok thanks! Can I ask you another question?
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

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

Post by pgimeno »

"Don't ask if you can ask, just ask"
Bodeman
Prole
Posts: 4
Joined: Wed Apr 22, 2020 12:43 pm

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

Post 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! :)
User avatar
steVeRoll
Party member
Posts: 139
Joined: Sun Feb 14, 2016 1:13 pm

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

Post 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.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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
Bodeman
Prole
Posts: 4
Joined: Wed Apr 22, 2020 12:43 pm

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

Post 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
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

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

Post 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.
User avatar
ErtYwek
Prole
Posts: 6
Joined: Sat Nov 03, 2018 3:59 pm
Contact:

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

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

Who is online

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