Page 1 of 1

Lua questions

Posted: 07 Jun 2012, 04:56
by smoth
1) What does the "gadget:" gadget:MyFunction() do?

2) I have a function in my gadget I want other gadgets to have acess, I am trying to reduce code redundancy. I see that I can use sendtounsynced calls across gadgets, what about functions within synced?

Re: Lua questions

Posted: 07 Jun 2012, 07:29
by gajop
smoth wrote:1) What does the "gadget:" gadget:MyFunction() do?
Bit confused but are you talking about the declaration or invocation?
Calling gadget:MyFunction() without any arguments is probably wrong, don't you need the gadget "object" to invoke it on? f.e: myGadget:MyFunction() or gadget.MyFunction(myGadget)? It may also have problems with scope if called from other gadgets - I don't think gadget:MyFunction() is globally defined, and it's probably ill advised to "VFS:Include" one gadget into another.
2) I have a function in my gadget I want other gadgets to have acess, I am trying to reduce code redundancy. I see that I can use sendtounsynced calls across gadgets, what about functions within synced?
Again, I'm a bit confused if what you're trying to do is anything more than having a simple function invocation, but just having that function defined in other files. How is it different than
https://github.com/gajop/Toolbox/blob/m ... r.lua#L120 , is the function defined as a "method" in a gadget - does it really need to be a method?
Thing to pay attention to is that the errors produced in the called function would crash the invoking gadget if left uncaught, and not the "gadget" where the function was defined.

Re: Lua questions

Posted: 07 Jun 2012, 11:43
by Pako
smoth wrote:1) What does the "gadget:" gadget:MyFunction() do?
i removed this part. -kaiser

These all are basically the same things:
function gadget:GameFrame(n) end
function gadget.GameFrame(self, n) end
gadget.GameFrame = function(self, n) end
gadget['GameFrame'] = function(self, n) end
smoth wrote: 2) I have a function in my gadget I want other gadgets to have acess, I am trying to reduce code redundancy. I see that I can use sendtounsynced calls across gadgets, what about functions within synced?
There is many ways to do it. One of the easiest is to use a global table like GG.

Re: Lua questions

Posted: 07 Jun 2012, 14:38
by FLOZi
http://www.lua.org/pil/16.html will help you understand the colon operator.

But as Pako rudely suggests, it calls a function with the object itself as the first argument. The 'gadget' object is basically the object created by the gadgethandler upon loading this particular.. gadget.

https://github.com/spring/spring/blob/d ... s.lua#L392


WRT #2, I agree with Pako, I'd use GG.

Example # 1: Lus Helper Gadget from BTL

Example #2: Custom Command ID Generator from S44

Re: Lua questions

Posted: 07 Jun 2012, 21:55
by smoth
ah ok, thanks floz. Isn't gg bad? IIRC JK/zever have advocated other methods.

I am not too bothered by pako, it is how he talks to people at least he is trying to give me info. Forb, if I was upset I can report pako but I am not.

Re: Lua questions

Posted: 11 Jun 2012, 20:12
by FLOZi
Link related: http://answers.springlobby.info/questio ... created-it

I don't know of any issues with GG but I'm not an expert.

Re: Lua questions

Posted: 11 Jun 2012, 20:20
by smoth
oh, I did the code and it worked but I was worried I was building bad code :|

Re: Lua questions

Posted: 11 Jun 2012, 21:28
by gajop
what's so special with "GG" other than being used often?
wouldn't it make more sense to create your own table if you don't want to pollute the global namespace?

Re: Lua questions

Posted: 12 Jun 2012, 18:37
by zwzsg
It's shared amongst all gadgets.

Otherwise gadgets get their own environnment.