How to use function love.keyreleased(key) in another lua 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.
Post Reply
Jachin
Prole
Posts: 3
Joined: Sat May 20, 2017 5:32 am

How to use function love.keyreleased(key) in another lua file

Post by Jachin »

I wanna use the love.keyreleased(key) in another lua file and require it in main.lua .
But when i do this, the love.keyreleased(key) of main.lua become unuseable.
Have any idea to solve this problem? I need some help.
User avatar
amightydish
Prole
Posts: 11
Joined: Fri May 12, 2017 5:18 pm

Re: How to use function love.keyreleased(key) in another lua file

Post by amightydish »

If you implement the function in two places, they will override each other. The last one defined will be the one used.

Why don't you just have the keyreleased function in main and then call your own function, e.g. a submodule.mykeyreleased(key) from it?

Here an example

mystuff.lua

Code: Select all

function my_keyhandler(key)
  print("key released:", key)
end

main.lua

Code: Select all

require("mystuff")

function love.keyreleased(key)
  my_keyhandler(key)
end

I'm typing this on the phone so can't test it, but I hope you get the idea.
Jachin
Prole
Posts: 3
Joined: Sat May 20, 2017 5:32 am

Re: How to use function love.keyreleased(key) in another lua file

Post by Jachin »

thank u!You are really friendly. I know how to do this from your codes.
If i want to use it in three lua file,should i have three different functions in different lua file to use it?
eg:

function my_keyhandlerone(key)
print("key released:", key)
end
function my_keyhandlertwo(key)
print("key released:", key)
end
function my_keyhandlerthree(key)
print("key released:", key)
end
I am not good at lua.Please tell me about how to use it easily.
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: How to use function love.keyreleased(key) in another lua file

Post by Beelz »

Do you mean use the same function in multiple files? You could:
  • Require the file once, and have that file create global variables.
  • Require the file once, and return as a global variable.
  • Require the file in every other file that needs it, and return a local variable.

Also when including code on a forum post, please wrap it in a code tag. Makes it easier for anyone to help you.

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
Jachin
Prole
Posts: 3
Joined: Sat May 20, 2017 5:32 am

Re: How to use function love.keyreleased(key) in another lua file

Post by Jachin »

Thanks your help.I have understood it.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 207 guests