[PD] Lua and PD (update)
Frank Barknecht
fbar at footils.org
Tue May 6 23:08:23 CEST 2008
Hallo,
Mike McGonagle hat gesagt: // Mike McGonagle wrote:
> On 5/6/08, Claude Heiland-Allen <claudiusmaximus at goto10.org> wrote:
> > The object system in pdlua isn't particularly well-thought out or elegant,
> > I'd be interested in what you had developed and how it differs - maybe I
> > could learn something from it.
>
> Well, thanks for that vote of encouragement, but after studying the
> methods used in pdlua, it is actually a bit less verbose. Many of the
> things I had done my way were about a 1/3 longer. It was a method that
> I borrowed from a book I have.
Actually Lua itself doesn't have a preconfigured object system like
C++ or Python, so you'd have to roll your own anyway. Pdlua uses a
protype-based objects system.
Another interesting "object system" would be the Single Method
approach: http://www.lua.org/pil/16.5.html where you use a closure to
store the object's state.
I recently wrote a LOGO-style turtle using this idiom. Attached is a
simplified version of it (removed typechecks etc).
Take special note of how seamlessly the single method turtle in
smtut.lua can be included in the Pd object runturtle.lua using this
primitive method:
function M:in_1(action, atoms)
self.t(action, atoms) -- selt.t is the single method turtle
end
It's even possible to convert smtut.lua to a proper module so that you
can reload it luax-style while Pd is running.
Ciao
--
Frank Barknecht _ ______footils.org__
-------------- next part --------------
A non-text attachment was scrubbed...
Name: runturtle-help.pd
Type: application/puredata
Size: 1213 bytes
Desc: not available
URL: <http://lists.puredata.info/pipermail/pd-list/attachments/20080506/4ef1869c/attachment.bin>
-------------- next part --------------
-- single method turtle
function Turtle(painter)
local x, y, oldx, oldy, dir, draw = 0, 0, 0, 0, 0, true
local painter = painter or print -- drawing function
return function (action, args)
if action == "reset" then
x, y, oldx, oldy, dir, draw = 0, 0, 0, 0, 0, true
elseif action == "setpos" then
x, y = args[1], args[2]
elseif action == "setdir" then
dir = args[1]
dir = dir % 360
elseif action == "turn" then
dir = (dir + args[1]) % 360
elseif action == "pen" then
if args[1] and args[1] ~= 0 then
draw = true
else
draw = false
end
elseif action == "draw?" then
return draw
elseif action == "print" then
print('x', x)
print('y', y)
print('oldx', oldx)
print('oldy', oldy)
print('dir', dir)
print('draw', draw)
elseif action == "forward" then
local dist = args[1]
oldx = x
oldy = y
x = x + dist * math.cos(math.rad(dir))
y = y + dist * math.sin(math.rad(dir))
if draw and type(painter) == "function" then
painter(x, y, oldx, oldy, dir)
end
elseif action == "getpos" then
return x, y
elseif action == "getdir" then
return dir
else
print("error: Unknown action: ", action)
end
end
end
-------------- next part --------------
require 'smtut'
local M = pd.Class:new():register("runturtle")
function M:initialize(name, atoms)
self.inlets = 1
self.outlets = 1
-- painter function for turtles:
local painter = function (x,y,oldx,oldy, dir)
self:paint({x,y,oldx,oldy, dir})
end
self.t = Turtle(painter)
return true
end
function M:in_1(action, atoms)
self.t(action, atoms)
end
function M:paint(coords)
self:outlet(1,"list", coords)
end
More information about the Pd-list
mailing list