Page 45 of 91

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

Posted: Thu Feb 04, 2016 12:50 pm
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.

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

Posted: Thu Feb 04, 2016 1:07 pm
by adge
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

Posted: Thu Feb 04, 2016 1:17 pm
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

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

Posted: Thu Feb 04, 2016 2:48 pm
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

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

Posted: Thu Feb 04, 2016 4:46 pm
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.

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

Posted: Fri Feb 05, 2016 9:48 am
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?

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

Posted: Fri Feb 05, 2016 1:05 pm
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

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

Posted: Fri Feb 05, 2016 8:20 pm
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.

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

Posted: Sat Feb 06, 2016 2:57 pm
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?

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

Posted: Sat Feb 06, 2016 5:17 pm
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 ?