Yes and it's common practice to do so. You can also define functions in functions and return them as closures.adge wrote:I'm just wondering if you could pass a function to a function in lua?
"Questions that don't deserve their own thread" thread
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: "Questions that don't deserve their own thread" thread
Re: "Questions that don't deserve their own thread" thread
Could you show me an example were a function passed to another function is needed or makes sense?
Re: "Questions that don't deserve their own thread" thread
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
Re: "Questions that don't deserve their own thread" thread
[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
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.
- 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
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 True 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.
Re: "Questions that don't deserve their own thread" thread
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?
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?
Re: "Questions that don't deserve their own thread" thread
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 likeadge 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?
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
Re: "Questions that don't deserve their own thread" thread
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.
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
Re: "Questions that don't deserve their own thread" thread
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?
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?
- 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
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 ?
I belive I need to compile LÖVE with this but how would I go about doing that ?
Who is online
Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 0 guests