What techniques that everyone should know?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: What techniques that everyone should know?

Post by Davidobot »

micha wrote:
Davidobot wrote:How to have clean code. Clean code is of outmost importance.
Here's a great book about clean code: http://www.amazon.com/Clean-Code-Handbo ... 0132350882
And of course there are two great videos about it. Part 1 and Part 2.
Yup! I found out about it from there!
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: What techniques that everyone should know?

Post by Azhukar »

Robin wrote:Personally I prefer to use whatever is idiomatic for that language
What language other than python does this apply to?
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: What techniques that everyone should know?

Post by ejmr »

Azhukar wrote:What language other than python does this apply to?
Haskell, occam, and FORTRAN from twenty-three years ago.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: What techniques that everyone should know?

Post by Azhukar »

ejmr wrote:Haskell, occam, and FORTRAN from twenty-three years ago.
I'm sure he meant those. :awesome:
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: What techniques that everyone should know?

Post by Inny »

I think the best thing to learn in any language, is to learn that the unit of reuse is the function. Different languages have different ways of bundling and shipping those functions around, but as a principle, you have to use a function to facilitate code reuse. As it applies to Lua, you have a lot of freedoms when it comes to bundling functions, but the best (IMO) is the simple mixin. A table whose only purpose is to serve as a collection of functions to allow other tables or objects to share and reuse the code.

Code: Select all

function mixin(to, from)
  for k, v in pairs(from) do to[k] = v end
end
Or perhaps a better style that's a bit more usable:

Code: Select all

function mixin(self, ...)
  for i = 1, select('#', ...) do
    for k, v in pairs(select(i, ...)) do self[k] = v end
  end
  return self
end
What this ultimately facilitates is that if you design your mixins as simple tables, you can give functionality to any object just through this sharing mechanism. Here's something i wrote recently that demonstrates this, it's a mixin that lets a object house and draw static image.

Code: Select all

local image_component = {

  load_image = function(self, name)
    self._image = love.graphics.newImage(name)
    self._image:setFilter("nearest", "nearest")
  end,

  draw_image = function(self, x, y)
    love.graphics.draw(self._image, x, y)
  end,

  image_width = function(self) return self._image:getWidth() end,
  image_height = function(self) return self._image:getHeight() end,
}
And no point here did I have to talk about metatables when talking about code reuse.
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: What techniques that everyone should know?

Post by ejmr »

Azhukar wrote:I'm sure he meant those.
Yeah it was not easy to think of languages that apply different semantics based on whitespace, lol.
Inny wrote:I think the best thing to learn in any language, is to learn that the unit of reuse is the function. Different languages have different ways of bundling and shipping those functions around…. As it applies to Lua, you have a lot of freedoms when it comes to bundling functions, but the best (IMO) is the simple mixin. A table whose only purpose is to serve as a collection of functions to allow other tables or objects to share and reuse the code.
I think that’s a great point Inny. Even when writing assembly language I still use labels and such to organize my code in the same way I would with functions in other languages.

Going back to the OP’s original question, I think Inny’s post highlights something important about Lua: *you really, really, really need to be comfortable with tables.* They are ubiquitous. Tables in Lua act as arrays, hash tables, containers, linked lists, graphs—pretty much any non-trivial data structure that you will see in other languages. The book “Programming in Lua” is a useful resource about tables. This page may also help. And make sure to read about the standard library for tables because you will both see and use those functions a lot.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: What techniques that everyone should know?

Post by bartbes »

ejmr wrote:
Azhukar wrote:I'm sure he meant those.
Yeah it was not easy to think of languages that apply different semantics based on whitespace, lol.
He did say idiomatic, not significant.
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: What techniques that everyone should know?

Post by ejmr »

bartbes wrote:
ejmr wrote:Yeah it was not easy to think of languages that apply different semantics based on whitespace, lol.
He did say idiomatic, not significant.
You’re right, but I would say the significant whitespace in all of those languages is the genesis for many idioms found in each.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: What techniques that everyone should know?

Post by Robin »

Yeah, but other languages still have idiomatic use of whitespace. That's what I was going for. Nothing to do with the off-side rule.
Help us help you: attach a .love.
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: What techniques that everyone should know?

Post by ejmr »

Can we all agree that Whitespace (the language) has the most idiomatic use of whitespace? Heh.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest