Nothing happens when I call function in other script/file?

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.
ziodice
Prole
Posts: 15
Joined: Thu Feb 23, 2012 8:35 pm

Nothing happens when I call function in other script/file?

Post by ziodice »

Uh...I hope I'm doing this right...so, I'm trying to make this ball/physics game where you control a ball, and you need to get to a certain spot on the level, and you can reverse gravity. So, I made a basic "level" where you can just move the ball around, and then I tried to make it so when you press "enter" it goes to the next level, which is located in a different lua script/file whatever. So, I wrote this code in main.lua, in function love.load.

Code: Select all

require "Levels/lev1.lua"
And in lev1.lua, I have code that just clears everything to background color, and then draws the ball and the ground back. So back in main.lua, I told it in function love.update

Code: Select all

lev1()
because that's the name of the first function in lev1.lua...so, when I go into my terminal and run it, it runs fine, but I press enter and nothing happens. Everything else is working. Tell me if you need the full code, I can like, pastebin it or something I guess. Sorry if this is incomprehensible. Also note that the things I wrote are not all of the code in the functions.
User avatar
Ellohir
Party member
Posts: 235
Joined: Sat Oct 22, 2011 11:12 pm

Re: Nothing happens when I call function in other script/fil

Post by Ellohir »

Are you checking well that when you push "enter" something happens? Try debugging your code adding some prints on the caller function and on the load function. Without the code it's impossible to help you more.
ziodice
Prole
Posts: 15
Joined: Thu Feb 23, 2012 8:35 pm

Re: Nothing happens when I call function in other script/fil

Post by ziodice »

Debugging with console? I'm sadly using ubuntu, and I read that console is windows only.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Nothing happens when I call function in other script/fil

Post by Nixola »

Nope, you only have to run your file via terminal, by giving the command 'love path/of/your/floder/or/love/file'
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ziodice
Prole
Posts: 15
Joined: Thu Feb 23, 2012 8:35 pm

Re: Nothing happens when I call function in other script/fil

Post by ziodice »

Alright, so, I got the console to work (Thanks!) BUT, when I press enter, it does NOT produce the text I need it to. So, should I copy paste my entire "main.lua" and "lev1.lua"?
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Nothing happens when I call function in other script/fil

Post by Nixola »

You can pack them in a .love file (select both them, right click one of them, click on compress, create a .zip file, rename it into filename.love instead of filename.zip) and then upload it here... But I think copy/paste is better, unless you have a lot of files/code or images, music or other files
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ziodice
Prole
Posts: 15
Joined: Thu Feb 23, 2012 8:35 pm

Re: Nothing happens when I call function in other script/fil

Post by ziodice »

lev1.lua

Code: Select all

local function lev1()
  print ("Working")
  levdraw()
end

function levdraw()
  love.graphics.clear()
  
  love.graphics.setColor(255, 0, 0)
  love.graphics.circle("fill", objects.ball.body:getX(), objects.ball.body:getY())
  
  love.graphics.polygon("fill", objects.ground.shape:getPoints())
end
main.lua

Code: Select all

function love.load()
  require 'lev1.lua'
    
  world = love.physics.newWorld(0, 0, 650, 650)
  world:setGravity(0, 700)
  world:setMeter(64)

  objects = {}
  
  objects.ground = {}
  objects.ground.body = love.physics.newBody(world, 650/2, 625, 0, 0)
  objects.ground.shape = love.physics.newRectangleShape(objects.ground.body, 0, 0, 650, 50, 0)
  
  objects.ball = {}  
  objects.ball.body = love.physics.newBody(world, 650/2, 650/2, 15, 0)
  objects.ball.shape = love.physics.newCircleShape(objects.ball.body, 0, 0, 20)

  objects.ceiling = {}
  objects.ceiling.body = love.physics.newBody(world, 650/2, 0, 0, 0)
  objects.ceiling.shape = love.physics.newRectangleShape(objects.ceiling.body, 0, 0, 650, 50, 0)
  
  love.graphics.setBackgroundColor(104, 136, 248)
  love.graphics.setMode(650, 650, false, true, 0)
end

function love.update(dt)
  world:update(dt)
    
  xBall = objects.ball.body:getX()
  yBall = objects.ball.body:getY()
  
  if xBall >= 650 then
    objects.ball.body:setX(0)
  end
  if xBall < 0 then
    objects.ball.body:setX(650)
  end
  if yBall < 0 then
    objects.ball.body:setY(650/2)
    world:setGravity(0, 700)
    objects.ball.body:applyForce(0, 0)
  end
   
  if love.keyboard.isDown("up") then
    world:setGravity(0, -700)
    objects.ball.body:applyForce(0, -100)
  elseif love.keyboard.isDown("down") then
    world:setGravity(0, 700)
  elseif love.keyboard.isDown("right") then
    objects.ball.body:applyForce(400, 0)
  elseif love.keyboard.isDown("left") then
    objects.ball.body:applyForce(-400, 0)
  elseif love.keyboard.isDown("r") then
    objects.ball.body:setY(650/2)
    objects.ball.body:setX(650/2)
    world:setGravity(0, 700)
  elseif love.keyboard.isDown("enter") then
    print ("Working")
    lev1()
  end 
end

function love.draw()
  love.graphics.setColor(0, 0, 0)
  love.graphics.print("Press 'up' to reverse gravity", 0, 30)
  love.graphics.print("Press 'down' to return gravity to it's original state", 0, 50)
  love.graphics.print("Press 'left' or 'right' to move", 0, 70)
  love.graphics.print("Please note that gravity changes do not apply until you move", 0, 90)  

  love.graphics.setColor(72, 160, 14)
  love.graphics.polygon("fill", objects.ground.shape:getPoints())

  love.graphics.setColor(0, 0, 0)
  love.graphics.polygon("fill", objects.ceiling.shape:getPoints())

  love.graphics.setColor(193, 47, 14)
  love.graphics.circle("fill", objects.ball.body:getX(), objects.ball.body:getY(), objects.ball.shape:getRadius(), 20)
end
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Nothing happens when I call function in other script/fil

Post by Nixola »

1) Key has to be 'return', according to the wiki
2) If you use love.keyboard.isDown(), everything in the 'if' gets run every frame, if the 'return' key is down, if you want it to be run only once, when the key is pressed, you have to define another callback, outside of love.update/draw/load, love.keypressed, like so:

Code: Select all

function love.keypressed(key, unicode --[[not needed now]])
  if key == 'return' then
    --do whatever you want to do
  end
end
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ziodice
Prole
Posts: 15
Joined: Thu Feb 23, 2012 8:35 pm

Re: Nothing happens when I call function in other script/fil

Post by ziodice »

Now I get an error upon hitting "enter" (Or "return", I guess...). "Attempt to call global "lev1" (a nil value)
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Nothing happens when I call function in other script/fil

Post by Nixola »

Ehm... I forgot 3) ^^'
3) the lev1 function is local, that means that only other functions inside 'lev1.lua' can run it, you have to delete that 'local' before its name to make it work
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Post Reply

Who is online

Users browsing this forum: varchomesghj and 2 guests