Page 1 of 1
LÖVE Dice
Posted: Sun Mar 24, 2013 5:49 am
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
Re: LÖVE Dice
Posted: Sun Mar 24, 2013 3:20 pm
by Hexenhammer
Interesting, I already have my own dice code though. Just one comment
I prefer to overload the call operator to create new objects. Less typing and just looks slicker if you ask me e.g.
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})
Re: LÖVE Dice
Posted: Sun Mar 24, 2013 11:34 pm
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!
Re: LÖVE Dice
Posted: Mon Mar 25, 2013 10:10 pm
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, ...
Re: LÖVE Dice
Posted: Tue Mar 26, 2013 2:38 am
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.
Re: LÖVE Dice
Posted: Tue Mar 26, 2013 4:15 am
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!