Lua questions

Lua questions

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Lua questions

Post 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?
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Lua questions

Post 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.
Pako
Posts: 174
Joined: 12 Jul 2009, 18:57

Re: Lua questions

Post 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.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: Lua questions

Post 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
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: Lua questions

Post 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.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: Lua questions

Post 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.
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: Lua questions

Post by smoth »

oh, I did the code and it worked but I was worried I was building bad code :|
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Lua questions

Post 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?
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Lua questions

Post by zwzsg »

It's shared amongst all gadgets.

Otherwise gadgets get their own environnment.
Post Reply

Return to “Lua Scripts”