Is there a way to group lines of code in Löve?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Is there a way to group lines of code in Löve?

Post by nice »

So I'm currently using Unity and C# at school a lot so in the recent days I've been pretty spoiled by using regions.

For those of you that doesn't know what region is:

Code: Select all

#region Let's hide our super fun code here
	
	// Totally cool and legit code right here
	
#endregion
By using regions in a project you can group up a lot of code, let's say you have this complex movement code for an enemy but you feel that it's a bit of a pain to scroll through to get to another part of the code, then regions is really good to have.

So does that kind of solution exist?
:awesome: Have a good day! :ultraglee:
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Is there a way to group lines of code in Löve?

Post by airstruck »

Functions!

No, that's not sarcasm. If your editor does code folding, it probably does it for functions. Maybe do..end blocks would also work, but functions really seem like the right choice (SRP, SoC and all that) unless you have a situation where it would produce too many closures and it's too much trouble to move it up to the "top level" for some reason.
Last edited by airstruck on Mon Feb 27, 2017 7:26 pm, edited 1 time in total.
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Re: Is there a way to group lines of code in Löve?

Post by nice »

airstruck wrote: Mon Feb 27, 2017 7:22 pm Functions!

No, that's not sarcasm. If your editor does code folding, it probably does it for functions. Maybe do..end blocks would also work, but functions really seem like the right choice (SRP, SoC and all that).
but if there's code inside the function that I want to group?
:awesome: Have a good day! :ultraglee:
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Is there a way to group lines of code in Löve?

Post by airstruck »

More functions! ;)

It's just my opinion, but this sounds like a sign that your functions are doing too much work. I'd break them up into smaller functions, not try to hide the stuff inside.
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Re: Is there a way to group lines of code in Löve?

Post by nice »

airstruck wrote: Mon Feb 27, 2017 7:26 pm More functions! ;)

It's just my opinion, but this sounds like a sign that your functions are doing too much work. I'd break them up into smaller functions, not try to hide the stuff inside.
well you're partly true, right now I'm trying to get back to LÖVE after not using it for approx. 2 years and I've left some code in my current project as a reminder for another time like this:

Code: Select all

	
-- What happens reach a certain threshold
	--[[
	if moveX < 700 then
		moveX = moveX + 100 * dt
	end
	]]

-- Here we're playing around with true/false
-- You can use "if moving == true" but that's overflow
-- For a false statement use "not" after "if".
	--[[
	if not moving then
		moveX = moveX + 100 * dt
	end

	moveX = moveX + 10 * dt
	-- moveY = moveY + 10 * dt
	]]
:awesome: Have a good day! :ultraglee:
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Is there a way to group lines of code in Löve?

Post by zorg »

So,
#region lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor. In longer code files, it is convenient to be able to collapse or hide one or more regions so that you can focus on the part of the file that you are currently working on.
Things like this are usually not part of any languages, rather it's dependant on the code editor one uses; for example, sublime text 2 can hide blocks, but it's horribly simplistic (it can't hide the closing ends unless i indent them as well).

I really think that embedding such functionalities in a language itself is just meaningless; it's equivalent (from a code perspective) to comments; also see this: http://softwareengineering.stackexchang ... code-smell
That's just my opinion though.

(Oh, and regarding your code snippet, you can just use "if moving then", since it's a boolean, merely putting it there will be evaluated to true... in fact, apart from nil and false, everything evaluates to true in lua)
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.
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Re: Is there a way to group lines of code in Löve?

Post by nice »

zorg wrote: Mon Feb 27, 2017 7:38 pm I really think that embedding such functionalities in a language itself is just meaningless; it's equivalent (from a code perspective) to comments; also see this: http://softwareengineering.stackexchang ... code-smell
That's just my opinion though.
Well I like them :C but seriously there were in one project where I used it every where and it looked bad after extensive use, so I set as a rule for myself that I should only use it if I have to.
zorg wrote: Mon Feb 27, 2017 7:38 pm (Oh, and regarding your code snippet, you can just use "if moving then", since it's a boolean, merely putting it there will be evaluated to true... in fact, apart from nil and false, everything evaluates to true in lua)
When you say it, I realize you're right.
I went back and saw my small "mistake", thanks for pointing it out!
:awesome: Have a good day! :ultraglee:
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Is there a way to group lines of code in Löve?

Post by airstruck »

Hmm, so really you want to fold up a bunch of comments? You could wrap the whole thing in a multiline comment, like this:

Code: Select all

--[==[
    ...other comments here...
--]==]
The thing with the equals signs is a special syntax for block comments that happen to contain ]] (or ]=], or ]==], etc.). Just make sure you use the same number of equals signs at the beginning and end.

With some luck your editor can figure out what's going on here and do the folding properly.
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Re: Is there a way to group lines of code in Löve?

Post by nice »

airstruck wrote: Mon Feb 27, 2017 7:48 pm Hmm, so really you want to fold up a bunch of comments? You could wrap the whole thing in a multiline comment, like this:

Code: Select all

--[==[
    ...other comments here...
--]==]
The thing with the equals signs is a special syntax for block comments that happen to contain ]] (or ]=], or ]==], etc.). Just make sure you use the same number of equals signs at the beginning and end.

With some luck your editor can figure out what's going on here and do the folding properly.
Hey thanks for pointing it out but the only downside I see with this is that it ain't pretty..
But nonetheless thanks!
:awesome: Have a good day! :ultraglee:
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Is there a way to group lines of code in Löve?

Post by airstruck »

nice wrote: Mon Feb 27, 2017 7:51 pmHey thanks for pointing it out but the only downside I see with this is that it ain't pretty..
But nonetheless thanks!
If you want pretty, delete all those comments and call it a day. I doubt you'll miss them much. ;)
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests