setLabelClickCallback("mtar1", matt() )

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

setLabelClickCallback("mtar1", matt() )

Post by Caled »

Code: Select all

function matt()
    return("mattack")
end

setLabelClickCallback("mtar1", matt() )

Code: Select all

function matt()
    echo("mattack")
end

setLabelClickCallback("mtar1", "matt()" )
Both of the above, return an error when I click on the associated label, also defined in the same script object. It seems like a pointless script, but when I have it working, I will change the matt() function to do a lot more. Different commands to be sent depending on different criteria.

I seem to have a syntax problem though - how can I make a label call a function when clicked?

Thylacine
Posts: 28
Joined: Sun May 10, 2009 5:04 am

Re: setLabelClickCallback("mtar1", matt() )

Post by Thylacine »

Have you tried removing the brackets after 'matt' in either case?

Your function definitions are fine, however I don't think you want "return" - that'll just force the function to return that particular value and as far as I know, it wont be used.

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

Re: setLabelClickCallback("mtar1", matt() )

Post by Caled »

The brackets need to be there for two reasons - one being that they are part of the correct syntax for calling a function, and the second being that once it is working, I will be passing arguments to the function inside those brackets.

I have tried all sorts of combinations of quotes around the "matt()", no quotes, return, echo, send - and all give an error. It compiles, but an error is reported in the debug window when I click on the label.

Thylacine
Posts: 28
Joined: Sun May 10, 2009 5:04 am

Re: setLabelClickCallback("mtar1", matt() )

Post by Thylacine »

Oops I meant within setLabelClickCallback itself, sorry.

This is from the manual; "e.g. setLabelClickCallback( "compassNorthImage", "onClickGoNorth" )", where onClickGoNorth is a function. Most languages don't require brackets with function pointers, as the pointers themselves simply point to a predefined entry point (memory address), for a function that has already had it's arguments defined. If they didn't do that, then anyone could parse bogus parameters not required by the object. I assumed this was the case for Lua as well...

Hopefully, that'll do it anyway.

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

Re: setLabelClickCallback("mtar1", matt() )

Post by Caled »

So how do I send an argument to matt?

Code: Select all

function matt(n)
    n=tonumber(n)
    setna("kai banish " .. multiTarget[n])
end
There are 10 label positions, each one corresponding to a different preset target, that target's name being stored as an entry in the numbered list "multiTarget".

matt(7) then, should kai banish the 7th name in the list. I can do a separate function for each label, but I want this layer of indirection because I have plans to add more and more functionality to this system of labels, eventually ending up with a group fighting setup for Aetolia that resembles a raiding addon for WoW - something like Grid healing or clickcasting.

Indirection is needed, or making changes in the future is going to take hours.

Thylacine
Posts: 28
Joined: Sun May 10, 2009 5:04 am

Re: setLabelClickCallback("mtar1", matt() )

Post by Thylacine »

As far as I know, it's not (directly) possible providing the click callback now works.

A workaround would be to have functions for each label, with each function calling 'matt' with specific values as required.
I.e, have a callback point to foo for one label and bar for another:

Code: Select all

function foo()
  matt(1)
end

function bar()
  matt(7)
end
I know it's messy, but it's the only way I can think of for the moment. Should save you a couple of hours, anyway ;)

I think with buttons (instead of labels) you could assign a value to a global in the script portion. Sadly buttons need to be floating in a toolbar or docked, it seems.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: setLabelClickCallback("mtar1", matt() )

Post by Heiko »

Just scanning the posts, so I might have overlooked something:
Tylancine is correct. The function parameter needs to be a string. In your case this means:

Code: Select all

callback = matt(n); -- if matt(n) returns the full name of the callback function otherwise: callback = "funcName" .. matt(n);
setLabelClickCallback("mtar1", callback )
We could add optional user parameters for the callback function like in the event system if this is really needed.

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

Re: setLabelClickCallback("mtar1", matt() )

Post by Caled »

That solution works, and in fact is probably easier than if it were possible the way I tried it initially.

Eventually I will have them passing two arguments to the function, one being a number referring to the target, and one being a mode indicating the state of the target. In this way I can have the 10 label positions (each with 4 or more labels, only one being shown at a time, into the one function. Given that is 40 or more labels in total, I'm pretty happy I don't need a separate function for each of them :D

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: setLabelClickCallback("mtar1", matt() )

Post by Heiko »

Changed setLabelClickCallback() to allow an unlimited amount of parameters for the callback function. Parameter type is restricted to string and int at the moment.

example:

Code: Select all

createLabel("compass",64,WindowHeight-162,75,75,1)
setBackgroundColor("compass",0,0,0,0);
setBackgroundImage("compass",gfx_path .. "/compass.png");
setLabelClickCallback("compass", "callbackTest", 1, "score", 94, 104, 99, "lastParameter" );

function callbackTest( p1, p2, p3, p4, p5, p6  )
	echo("p1="..p1.." p2<"..p2.."> p3<"..p3.."> p4<"..p4..">p5="..p5.." p6<"..p6..">\n");
	if p1 == 1 then
		send( p2 );
	else
		send( p2 );
	end
end

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

Re: setLabelClickCallback("mtar1", matt() )

Post by Caled »

Oh cool, nice one :)

Post Reply