enigma-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Enigma-devel] Call a function with current x/y coordinates


From: Nat Pryce
Subject: Re: [Enigma-devel] Call a function with current x/y coordinates
Date: Wed, 8 Oct 2003 21:47:44 +0100

You can get this effect using Lua nested functions (functions defined within
functions) and "upvars".  For LISP or Scheme programmers, these are a form
of higher order function closed over the variables in the defining scope,
while for Java programmers they are very like inner classes and final
variables.

I have used nested functions in several levels to have callbacks from
switches that know which switch got toggled.  Look at the nat??.lua or
natmaze??.lua files to find them, those with higher numbers are more likely
to use the technique because I only used it after learning enough about the
Lua language to do cunning things with it.

Cheers,
            Nat.
_______________________
Dr. Nathaniel Pryce
B13media Ltd.
http://www.b13media.com
+44 (0)7712 526 661

----- Original Message -----
From: "Petr Machata" <address@hidden>
Cc: "Enigma-devel Group" <address@hidden>
Sent: Sunday, October 05, 2003 6:14 PM
Subject: Re: [Enigma-devel] Call a function with current x/y coordinates


> Michi Hostettler wrote:
> > hi,
> >
> > is it possible to get the current x and y coordinates for a cell? for
> > example:
> >
> > Require("levels/ant.lua")
> > function file_oxyd(x,y,f)
> >     oxyd_default_flavor=f
> >     oxyd(x,y)
> > end
> >
> > function GTarget(x,y) <--x and y should be the cordinates of the cell
> > print("gt:",tostring(x))
> > print("gt:",tostring(y))
> > if (x==1) and (y==0) then retval="laser1" <-- Only if current cell is
the
> > cell with the red marked !
> > return (retval)
> > end
> >
> > function GAction(x,y) <--x and y should be the cordinates of the cell
> > print("ga:",tostring(x))
> > print("ga:",tostring(y))
> > if (x==1) and (y==0) then retval="laser1" <-- Only if current cell is
the
> > cell with the red marked !
> > return (retval)
> > end
> >
> > cells["!"]=cell{stone={"st-switch", {action=GAction,target=GTarget}}}
<-- if
> > a cell is ! then it should call GAction and GTarget with this cell's
> > coordinates.
> > cells["."]=cell{stone={"st-laser-w", {on=1,name="laser1"}}}
> >
>
> As to my best knowledge, action and target have to be strings. Enigma
> then takes an object with the name <target> and sends a message <action>
> to it. No coordinates are transferred.
>
> If you want to call a 'GTarget' each time you press a button, use this:
>   action="callback"
>   target="GTarget"
> However, GTarget will get only one argument -- a state of button (1 or
> 0, afaik).
>
> You could somehow utilize CELL's parent handling, for example this:
>   cells["!"]=cell{parent={GTarget}}
> will call function GTarget for each cell, that you marked with '!', and
> it will pass a coordinates also. But it will be done once only, at the
> moment when you call 'create_world_by_map'.
>
> Actually, there is at least one way how to make what you want. But it
> involves rather hardcore LUA hacking and definitely it won't be nice code.
>
> >
> > michi
> >
>
> Petr Machata
>
>
>
>
>
> _______________________________________________
> Enigma-devel mailing list
> address@hidden
> http://mail.nongnu.org/mailman/listinfo/enigma-devel





reply via email to

[Prev in Thread] Current Thread [Next in Thread]