Page 2 of 2

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

Posted: Mon Feb 27, 2017 8:02 pm
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)

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

Posted: Mon Feb 27, 2017 8:04 pm
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

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

Posted: Mon Feb 27, 2017 8:06 pm
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

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

Posted: Mon Feb 27, 2017 9:49 pm
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'.

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

Posted: Mon Feb 27, 2017 11:21 pm
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

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

Posted: Tue Feb 28, 2017 12:43 am
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).

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

Posted: Tue Feb 28, 2017 1:05 am
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.)

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

Posted: Tue Feb 28, 2017 5:35 pm
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.

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

Posted: Wed Mar 22, 2017 7:44 pm
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 5327 times