Pointers in Lua

Post Reply
Xikue
Posts: 6
Joined: Sun Jul 05, 2009 4:24 am

Pointers in Lua

Post by Xikue »

Hello. I was trying to go further with the flexible offensive aliases (http://mudlet.sourceforge.net/phpBB3/vi ... p?f=8&t=97) so that entering "kick rat" would also set your target to rat. (And following attacks would not need you to specify the target again.)
So I changed the tar function into this.

Code: Select all

function retarget(given)
   if given ~= nil then
      target = given
   end
      return target
end
But I have separate variables for targets in different activities (dtar, itar, otar) So I passed in the target variable as a parameter
ex. kick rat, pattern ^kick(\s(\w+))$

Code: Select all

send("kick " .. retarget(matches[3], otar))
--
function retarget(given, target)
   if given ~= nil then
      target = given
   end
   return target
end
Of course this failed because I didn't use a pointer. I found something called light user data which is supposed to be a void * pointer, but I don't understand how to use the functions they mentioned (such as lua_pushlightuserdata).

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: Pointers in Lua

Post by Caled »

I don't understand the term 'pointer' but this doesn't seem all that complex a problem. The one thing missing is that (I am told) Lua doesn't seem to support recursive expansion of variables. The way around this being to stick your various target variables into a table.

Code: Select all

tars={
 otar="",
 itar="",
 dtar=""
}

function retarget(target, given)
	if given ~= nil then
		tars[target] = given
	end
	return tars[target]
end
Edit
I am not in a position to test that, but I -hope- that by switching the function parameters like that, this function will still work even when matches[3] in the alias is nil . At least, the function will work, but I am not sure if the alias itself will work.

If the alias refuses to call the function with a nil second parameter, then the problem is more complex. Well, even then the solution is simple enough, but it is not a solution I like - it involves putting more script in the alias itself, and in my opinion part of the point here is to make these aliases simpler to write as well as to use.

Xikue
Posts: 6
Joined: Sun Jul 05, 2009 4:24 am

Re: Pointers in Lua

Post by Xikue »

Ah, I didn't switch the parameters and it still works with the table, so I guess it doesn't mind nil as the first parameter.
Thanks!

Post Reply