lua-users home
lua-l archive

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


Rici, I don't quite get what you mean.  Here is what I tried and it
didn't work (of course).  I know how to create an entry into the
registry, but then how would I go about calling the method once I've
stored it?  A small sample would be great.

:EXISTING CODE:
var
  pL : Plua_State;
  callfunc : lua_CFunction;

function lua_astarCallback(X, Y, Fx, Fy: integer): integer;
begin
      WriteLn('Calling cfunction: '+IntToStr(Integer(callfunc)));
  lua_pushcfunction(pL, callfunc);
  lua_pushinteger(pL, X+1);
  lua_pushinteger(pL, Y+1);
  lua_pushinteger(pL, Fx+1);
  lua_pushinteger(pL, Fy+1);
  lua_pcall(pL, 4, LUA_MULTRET, 0);
  if lua_gettop(pL) > 0 then
    result := lua_tointeger(pL, -1)
  else
    result := -1;
end;

function lua_FindPath(L: Plua_State): integer; cdecl;
var
  i, rtbl, etbl, srcX, srcY, dstX, dstY, gridSizeX, gridSizeY : Integer;
  diagonals, fallback : Boolean;
begin
//FindPath(const src, dest, Gridsize: Tpoint; const diagonals,
pleasefallback: boolean; const grabcallback: PInspectBlock);
  result := 0;
  if lua_gettop(L) <> 9 then
    luaL_error(L, 'AStarPath expects 9 parameters (srcX, srcY, dstX,
dstY, gridSizeX, gridSizeY, diagonals, fallback, CallBackProc)')
  else
    begin
      pL := L;
      srcX := lua_tointeger(L, 1) -1;
      srcY := lua_tointeger(L, 2) -1;
      dstX := lua_tointeger(L, 3) -1;
      dstY := lua_tointeger(L, 4) -1;
      gridSizeX := lua_tointeger(L, 5);
      gridSizeY := lua_tointeger(L, 6);
      diagonals := luatoboolean(L, 7);
      fallback := luatoboolean(L, 8);
      WriteLn('Retrieving cfunction');
      WriteLn(lua_typename(L, 9));
      callfunc := lua_tocfunction(L, 9);
      WriteLn('Got cfunction');
      FindPath(Point(srcX, srcY), Point(dstX, dstY), Point(gridSizeX,
gridSizeY), diagonals, fallback, @lua_astarCallback);
      if Length(Path) = 0 then
        begin
          lua_pushboolean(L, false);
          result := 1;
        end
      else
        begin
          result := 1;
          lua_newtable(L);
          rtbl := lua_gettop(L);
          for i := 0 to Length(Path) -1 do
            begin
              lua_pushinteger(L, i+1);
              lua_newtable(L);
              etbl := lua_gettop(L);
              lua_pushliteral(L, 'X');
              lua_pushinteger(L, Path[i].X);
              lua_settable(L, etbl);
              lua_pushliteral(L, 'Y');
              lua_pushinteger(L, Path[i].Y);
              lua_settable(L, etbl);
              lua_settable(L, rtbl);
            end;
        end;
    end;
end;

:USAGE SCRIPT:
playingfield = {
  [1] = {[1]=1, [2]=1, [3]=1, [4]=1, [5]=1},
  [2] = {[1]=1, [2]=1, [3]=1, [4]=1, [5]=1},
  [3] = {[1]=1, [2]=1, [3]=1, [4]=1, [5]=1},
  [4] = {[1]=1, [2]=1, [3]=1, [4]=1, [5]=1},
  [5] = {[1]=1, [2]=1, [3]=1, [4]=1, [5]=1}
}

function CallBack(AX, AY, FX, FY)
  return playingfield[AX][AY]
end
--srcX, srcY, dstX, dstY, gridSizeX, gridSizeY, diagonals, fallback,
CallBackProc
path = AStarPath(1, 1, 5, 5, 5, 5, false, true, CallBack)

if path~=nil and path~=false then
  for i=1,table.getn(path) do
    table.foreach(path[i], print)
  end
else
  print('no path found')
end

Thanks,
 - Jeremy

"Help I suffer from the oxymoron Corporate Security."


> -------- Original Message --------
> Subject: Re: CallBacks and Proto
> From: Rici Lake <lua@ricilake.net>
> Date: Thu, October 26, 2006 11:21 am
> To: Lua list <lua@bazar2.conectiva.com.br>
> 
> On 26-Oct-06, at 11:10 AM, jdarling@eonclash.com wrote:
> 
> > I know I've seen this answer here some place, but I can't find it in my
> > searchs (proto, lua, callback, function, cfunction).  I have a script
> > that defines a callback to be called by source method being called by
> > the script.  Ok that makes no sense, so lets try some sample source:
> >
> > :SCRIPT:
> > v = {[1]='Hello', [2]='World', [3], '!'}
> > function foo(i)
> >   print(v[i]..' ')
> > end
> >
> > TestCallback(table.getn(v), foo)
> > :ENDSCRIPT:
> >
> > Now TestCallback is defined in my source code and recieves the getn(v)
> > fine (3 in this case) it then attempts to store the method pointer for
> > foo (I thought it would be cfunction type, but its proto) of course
> > this doesn't work for calling later on.  So how can I store the proto
> > and use it later?  I'm guessing that proto means prototype or method
> > prototype, but can't find any info on using it or what the structure
> > looks like.
> 
> You have to store it in some Lua structure, probably a table.
> 
> Often people do this with luaL_reg, but you could just store it 
> directly in the registry using some known key.