LÖVE Dice

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
daviddoran
Prole
Posts: 30
Joined: Sun Mar 24, 2013 5:35 am

LÖVE Dice

Post by daviddoran »

I came across LÖVE a while ago and knew I'd have to find time to play with it. Very pleased so far!

I created a little dice library (for those times when you want to do something with a 1 in 20 chance etc). Easy to do with math.random but it was a nice first library.

Check it out on github:

https://github.com/daviddoran/lovedice

I'm also new to Lua so any comments on how to make it more idiomatic are welcome.

-- dave
User avatar
Hexenhammer
Party member
Posts: 175
Joined: Sun Feb 17, 2013 8:19 am

Re: LÖVE Dice

Post by Hexenhammer »

Interesting, I already have my own dice code though. Just one comment

Code: Select all

local die20 = Dice.newDie(20)
I prefer to overload the call operator to create new objects. Less typing and just looks slicker if you ask me e.g.

Code: Select all

local die20 = Dice(20)
Or from my code

Code: Select all

Area[Position(2, 4)] 

as opposed to

Area[Position.New(2, 4)]
I case you don't know how to do that, you simply set the __call field of the object's metatable e.g.

Code: Select all

setmetatable(Position, {__call = Position.New})
User avatar
daviddoran
Prole
Posts: 30
Joined: Sun Mar 24, 2013 5:35 am

Re: LÖVE Dice

Post by daviddoran »

Thanks for the tip Hexenhammer. Part of the reason I went with .newDie(n) was to mirror the love libraries but also because Dice(6) read to me like it would create 6 dice (as opposed to one 6-sided die). But I love the pattern..it's very clean. I'll use it in my new libaries!
User avatar
pakoskyfrog
Citizen
Posts: 62
Joined: Mon Feb 04, 2013 12:54 pm
Location: France

Re: LÖVE Dice

Post by pakoskyfrog »

Hi ! First, nice first little Lib you have there ^^

And, as coding convention, I prefer to not use the __call for creating new objects for a simple reason : I want to keep the possibility for my objects to use it for something else. e.g. What if i want to create a functional object that can produce something from an argument ?

Code: Select all

f1 = CFunc.create(6)
result1 = f1(3)
result2 = f1(7)
For instance, you could use the __call instead of the roll() function, since it is called more often than the factory.

Code: Select all

D20 = CDice.create(20)
D6 = CDice.create(6)
result20 = D20()
result6 = D6()
-- or even get a set of N values 
resultTable20 = D20(35) -- N=35
I know i could use polymorphism so the __call from the class will not be the same as the __call of the instance. But it seems to me that it's clearer to keep a "new" or "create" function to make objects. I prefer when it's obvious.

All of that just to tell, it's up to you ! Keep what you like the most, and test all the method/possibility you have to make you own opinion ^^

ps : Since I really like obvious things I always name my classes starting with a C, enumeration with an E, ... ;)
User avatar
Hexenhammer
Party member
Posts: 175
Joined: Sun Feb 17, 2013 8:19 am

Re: LÖVE Dice

Post by Hexenhammer »

pakoskyfrog wrote: I know i could use polymorphism so the __call from the class will not be the same as the __call of the instance.
That's actually what I do. class __call is the factory function, instance __call returns the iterator (in case of container types) so I can do stuff like:

Code: Select all

area = Array(Dimensions(64, 64))

for cell in area()
  cell.terrain = Terrain.grass
end
But it seems to me that it's clearer to keep a "new" or "create" function to make objects.
Must be my programming background but I consider an explicit "new" to be clumsy. In most programming languages you write "MyType a", "a := MyType", or something like that. In the same way languages which support foreach usually have a syntax like "for element in container". Using __call for these things in Lua makes the code look most.. eh.. "natural" to me. But it is ultimately a subjective style issue.
User avatar
daviddoran
Prole
Posts: 30
Joined: Sun Mar 24, 2013 5:35 am

Re: LÖVE Dice

Post by daviddoran »

Both viewpoints are perfectly valid (I think we can all admit).

I think it all comes down to preference and taste, except possibility for the future proofing that newX gives you. I like the factory style, e.g. Dice.newDie(), Dice.newWeightedDie(), Dice.newDieSet(2) and keeping the constructor at Dice.Die. It's just a bit more explicit.

But I'll probably change my mind next week so don't hold me to anything!
Post Reply

Who is online

Users browsing this forum: No registered users and 25 guests