"Questions that don't deserve their own thread" thread

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.
Locked
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: "Questions that don't deserve their own thread" thread

Post by s-ol »

adge wrote:I'm just wondering if you could pass a function to a function in lua?
Yes and it's common practice to do so. You can also define functions in functions and return them as closures.

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
adge
Citizen
Posts: 54
Joined: Mon Dec 14, 2015 8:50 pm

Re: "Questions that don't deserve their own thread" thread

Post by adge »

Could you show me an example were a function passed to another function is needed or makes sense?
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: "Questions that don't deserve their own thread" thread

Post by s-ol »

adge wrote:Could you show me an example were a function passed to another function is needed or makes sense?

Code: Select all

print("definetely earlier")
timer.inSeconds(3, function () 
  print("3 seconds later")
end)
print("also earlier, actually")

function love.update(dt)
  timer.update(dt)
end

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

Re: "Questions that don't deserve their own thread" thread

Post by pgimeno »

[wiki]Canvas:renderTo[/wiki] expects a function as a parameter, for example.

Edit: As does Lua standard function pcall(). See http://www.lua.org/manual/5.1/manual.html#pdf-pcall
Last edited by pgimeno on Thu Feb 04, 2016 6:23 pm, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: "Questions that don't deserve their own thread" thread

Post by zorg »

Another way to think about this is compartmentalization; let's say you have 50 different types of enemies, and all of them move according to the same code; instead of copypasting that code 50 times, one can create a function that is called in those 50 places. Depending on how you structured your code, you might need to pass this function as a parameter to another function, like a constructor for the enemies, it taking this function as the movement function for them to use.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
adge
Citizen
Posts: 54
Joined: Mon Dec 14, 2015 8:50 pm

Re: "Questions that don't deserve their own thread" thread

Post by adge »

Ok, thank you, that helped me!

Can you also create variable names randomly or in a logical manner? Like if you have a folder where tiles are stored and you need to load all .png's but there are 30 and you don't want to create Table1-Table30 variables by hand?
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: "Questions that don't deserve their own thread" thread

Post by s-ol »

adge wrote:Ok, thank you, that helped me!

Can you also create variable names randomly or in a logical manner? Like if you have a folder where tiles are stored and you need to load all .png's but there are 30 and you don't want to create Table1-Table30 variables by hand?
Yes, there are tricks to do that, but it makes more sense to just create a variable "Table" that is a table and assign the sprites to it like

Code: Select all

Table = {}
Table[1] = ...
Table[2] = ...

-- or with "logic", in a loop
for i=1,30 do
  Table[i] = love.graphics.newImage("sprites/".. i .. ".png")
end

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
adge
Citizen
Posts: 54
Joined: Mon Dec 14, 2015 8:50 pm

Re: "Questions that don't deserve their own thread" thread

Post by adge »

Again, thank you! Helped me!

I'm trying to create my own editor with only code thats from me. Kinda for learning purposes. I'm wondering if I should draw the tiles and export them as they are, or if I should scale them up, or if I should do all the scaling by code in my editor?

I attached my project so you can take a look.
Attachments
Editor.love.zip
(3.91 KiB) Downloaded 54 times
User avatar
unixfreak
Citizen
Posts: 82
Joined: Thu Oct 15, 2015 6:25 am
Location: Bristol, UK
Contact:

Re: "Questions that don't deserve their own thread" thread

Post by unixfreak »

I hope this makes sense as i have a hard time searching for a method on this. Is there a simple approach to getting a 'negative' colour depending on what is drawn behind? For instance, if i have a canvas with colours that clash with another transparent canvas drawn in front, is there a way to make the top canvas have inverted colours?

An example would be white text scrolling across a black background which might contain white squares -- when the white square is behind, that part of the texts colour becomes negative, so it's black instead.

I imagine it could be done with shaders, but that's ahead of my current knowledge. Are there any build in functions or 'hacks' to get this behavior?
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: "Questions that don't deserve their own thread" thread

Post by Ranguna259 »

Is there a way to add luacrypto to LÖVE ?
I belive I need to compile LÖVE with this but how would I go about doing that ?
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
Locked

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 0 guests