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
zorg
Party member
Posts: 3441
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 »

airstruck wrote: Mon Feb 27, 2017 7:55 pm
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. ;)
Or, instead of commenting out stuff, use versioning and revert if something doesn't work as you expected it to :3
(Might be overkill though, but it doesn't hurt learning git or something)
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 »

airstruck wrote: Mon Feb 27, 2017 7:55 pm
nice wrote: Mon Feb 27, 2017 7:51 pm If you want pretty, delete all those comments and call it a day. I doubt you'll miss them much. ;)
You're so right :nyu: but since my memory is sometimes like an goldfish, I think it's for the better it's there so it reminds me
:awesome: Have a good day! :ultraglee:
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 8:02 pm Or, instead of commenting out stuff, use versioning and revert if something doesn't work as you expected it to :3
(Might be overkill though, but it doesn't hurt learning git or something)
I have been using Sourcetree and currently using TortoiseHG but all in due time
:awesome: Have a good day! :ultraglee:
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

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

Post by s-ol »

Im surprised noone mentioned blocks. I'm surprised functions are the main response here.
Just like you can use { and } in C randomly group statements, you can use "do" and "end" outside of a loop to create a scope and indent:

Code: Select all

local x = 2
do
  local x = 6
  print(x, "inside 1")
end

do
  local x
  print(x, "inside 2")
end

do
  print(x, "inside 3")
end
Is going to print

Code: Select all

6	inside 1
nil	inside 2
2	inside 3
Now if you add a simple comment above the block you got your region syntax back, with built in editor indentation support and a free local scope to clean up your code beyond the purely aesthetic:

Code: Select all

function complicatedComputation(a)
  local fleeb
  do -- compute fleeb
    local foo = math.sqrt(a * math.pi ....
    ...
    fleeb = ...
  end
  
  local turbulence
  do -- figure out turbulence
    ...
  end
  return a/2 + fleeb * (math.random() - .5) * turbulence
end
Some arguments against functions can probably be taken from 'John Carmack on Inlined Code'.

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
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

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

Post by Inny »

do end blocks.

Code: Select all

function something(parameter)
  local result

  do -- region one
    local inner_scope_variable = "whatever"
    result = inner_scope_variable
  end

  return result
end
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 »

Sure, you can (probably) fold do..end blocks. But if the problem is that the code is hard to read because you have to scroll past a bunch of implementation details to figure out what's going on, breaking things down into smaller functions can actually help (the author or anyone else reading the code) regardless of folding.

To borrow s-ol's example:

Code: Select all

local function computeFleeb (a)
    return math.sqrt(a * math.pi ....
end

local function figureOutTurbulence (...)
    return ...
end

local function complicatedComputation(a)
  local fleeb = computeFleeb(a)
  local turbulence = figureOutTurbulence()
  return a/2 + fleeb * (math.random() - .5) * turbulence
end
If the code in the "region" has enough of a singular purpose that you can name the region, it probably ought to be its own function (if reasonable, of course).
User avatar
zorg
Party member
Posts: 3441
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 »

To be completely honest, the C# regions are not nearly the same as do...end blocks or even functions; hell, they are literally just comments from a code perspective; as i said before, they only hint code folding info to one specific IDE.

Also, nice's question wasn't that specific either, since:
nice wrote: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?
This can go both ways, really.

Visually grouping with #regions does nothing; i can (temporarily) group my own code by indentation levels if i want to fold them up; but i already said this before.

To functionally group code, there are dozens of ways for that depending on many things. do...end blocks, functions (closures), separation into files and requiring them in, hell, even coroutines can be viewed as an alternate way to "break up" logic into different parts; the danmakufu framework, for example, uses that kind of separation for various reasons. (The downside with lua coroutines in löve is that they are not compiled at all under LuaJIT, meaning they will be much slower than not using them.)
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 »

Well I guess that I perhaps I should've provided a clearer answer for next time, like a link that better explains my intentions.
:awesome: Have a good day! :ultraglee:
User avatar
sandygk
Prole
Posts: 20
Joined: Mon Nov 28, 2016 9:22 pm

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

Post by sandygk »

If your code editor supports indentation-based folding (sublime text does) then you could use comments and indentation to simulate regions. That's what I do.
I use VS with Babelua and VS does not support indentation-based folding by default so I implemented an extension for that. let me know if you are interested in it :)

asdf.gif
asdf.gif (258.79 KiB) Viewed 5128 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 177 guests