Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Bindie wrote:Awesome. Lol, *Bow wow*

Let's say I have:

Code: Select all

function love.keypressed(key)
if key == " " then
return true
end
end

And inside love.update() I want to

Code: Select all

if key == " " and CheckCollision(Players coordinates, Machines coordinates) then
Machine.on = not Machine.on
if Machine.on == true then
play(On-Sound)
else
play(Off-Sound)
end

end
How do I?
wait what?

For one, why put love.keypressed into love.load? Actually that might work, but it also might not (I never tried). It serves no purpose though and only leaves room for error. And why does it return anything? love.keypressed isn't supposed to return something, so anything you do return just gets thrown away. So basically your keypressed function doesn't do anything at all.

Second: where is key in love.update coming from?

And lastly: what exactly are you asking? You seem to have a general outline of what you want to do, but what you just wrote doesn't really make much sense.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
baby:hurt(me)
end
Muris
Party member
Posts: 131
Joined: Fri May 23, 2014 9:18 am

Bindie wrote:How do I?
If I understand correctly what you want to do, you could do something like this:

Code: Select all

local movSpeed = 100 -- move 100 pixels per second
local angle = 0

local player = {
x = 20,
y = 40,
width = 20,
height = 20,
}

local machine = {
on = true,
x = 200,
y = 200,
width = 50,
height = 60
}

local keys = {}

local function checkCollision( obj1, obj2 )
if obj1.x + obj1.width < obj2.x or
obj2.x + obj2.width < obj1.x or
obj1.y + obj1.height < obj2.y or
obj2.y + obj2.height < obj1.y then

return false
end
return true
end

end

function love.draw()
love.graphics.setColor(255, 255, 0 )
love.graphics.rectangle("fill", machine.x, machine.y, machine.width, machine.height )
love.graphics.setColor(255, 0, 0 )
love.graphics.rectangle("fill", player.x, player.y, player.width, player.height )
if machine.on then
love.graphics.setColor( 255,255,255,128 )
love.graphics.arc("fill", machine.x + machine.width / 2, machine.y + machine.height / 2, 100, angle, angle + 0.5)
end

end

function love.update(dt)
angle = angle + dt * 5
if keys.w then
player.y = player.y - dt * movSpeed
elseif keys.s then
player.y = player.y + dt * movSpeed
end
if keys.a then
player.x = player.x - dt * movSpeed
elseif keys.d then
player.x = player.x + dt * movSpeed
end
end

function love.keypressed( key )
if key == "q" or key == "escape" then
love.event.quit()

elseif key == " " and checkCollision( player, machine ) then
machine.on = not machine.on
if machine.on then
print( "play sound on")
else
print( "play sound off ")
end
end
keys[ key ] = true
end

function love.keyreleased( key )
keys[ key ] = false
end

Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Thanks ok. So anytime I want code executed from pressing a key a single time, I have to write it inside the love.keypressed function. That works.
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Bindie wrote:Thanks ok. So anytime I want code executed from pressing a key a single time, I have to write it inside the love.keypressed function. That works.
assuming you don't have love.keyboard.setKeyRepeat set to true, than yes.
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Does that mean I could call the function from anywhere? Instead of having to write all inside keypressed
Muris
Party member
Posts: 131
Joined: Fri May 23, 2014 9:18 am

You do not need to call the function by yourself, every time you press a key love.keypress is being called automatically. When you release the key love.keyreleased will be called.

If you define a function, without word local, it will always be globally visible, meaning if you define a function on any of the lua-files you make, and then require it, it will be globally available from any other files.

If you define a local function, it will be only visible inside that file and only after you have declared it. For example if you need to use function that calls other function that calls back to yours, you need to define them before using them. Basically this means that you're splitting something like recursive function into several functions.

Not very good example, but example regardless:

Code: Select all

--f1() -- cannot be called here because f1 isn't declared yet, will give an error
local f2 -- defining f2, so that the variable is visible into function f1
local function f1(a)
print("f1 called with:", a)
if a then return end
f2(1)
end

f2 = function(a)
print("f2 called with:", a)
f1(a)
end
f1()

On the other hand if you create global functions, they can have cross calls without any problem, and they can be called from any other file, which you might want to prevent for most cases.

Code: Select all

function f3(a)
print("f3 called with:", a)
if a then return end
f4(1)
end
f4 = function(a)
print("f4 called with:", a)
f3(a)
end
f3()

Basically this is kind of the reason why up to my understanding C and C++ use separate header files. Also how I understand Lua, which probably isn't the case, lua only executes the code in file only once when first time required. This is something I bumped into couple of days ago, when I had kind of like data in lua files, then I changed the data, and wanted to reset the data by new require. This didn't really work, so I had to do copy of the table from the require and use the copy, then copy the table again when resetting.

For example if otherfile.lua looks like this:

Code: Select all

print("required file otherfile")

function f5()
print("f5 called")
end

return {}

And main file:

Code: Select all

--f5() -- cannot call f5 from here, because the lua doesn't know its existence
local r1 = require 'otherfile'
local r2 = require 'otherfile'
f5() -- can be called from here, because it was declared in otherfile.lua
r1.x = 3
print( r2.x )

You can notice that r1 and r2 are actually same tables. So chanigng r1 also changes r2. Also you can notice that you can call functions defined in other file, as long as you have first required the file, as long as the functions are globally defined, not local functions.

Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

I think I am very unclear, basically I was just wondering why there isn't a love.keypressed(key) that can return true and then you could use it like love.keyboard.isDown(key).

For example:

Code: Select all

if love.keyboard.isDown("1") then
laser = not laser
end

-- And I wonder why there is no:

if love.keypressed("1") then
laser = not laser
end

In requiring libraries and files I now know that if for example functions are declared local in libariries and files they can't be reached, thanks.
pajh
Prole
Posts: 8
Joined: Fri Jan 30, 2015 4:52 pm

I'm having problems seeing console output using Love & and the latest version Zerobrane.

I have a print statement in love.conf which I can see in ZB's output window, however no print statement from main.lua (for example in love.load) show up at all. This isn't a matter of delayed output, they don't show up when the program stops either. Running from ZB either using a plain run or a debug seems to make no difference.

any ideas?
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

love.keypressed is supposed to be used independet of love.update
it is also supposed to be used for buttons that shouldnt repeat
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm