tags:

views:

318

answers:

3

This is a WoW (World of Warcraft) lua script question. Not many of these get asked here but I have no where to turn and Stackoverflow is the programmer oasis for answers.

Question:

Wowwiki states that the 2nd, 3rd, 4th arguments are your calling functions 1st, 2nd, 3rd arguments. I don't find this to be true. I find that the 3rd, 4th, 5th arguments end up being the 1st, 2nd, 3rd arguments.

Link: http://www.wowwiki.com/API_pcall

Function:

function myTest(arg1) return arg1 .. 10; end

Problem:

local retOK, ret1 = pcall(myTest,"string value");

when I try the sample I get an error of "trying to perform concatenate on local 'arg1' (a nil value)". If I change the code to

local retOK, ret1 = pcall(myTest,"string value", "bob");

then I get the output of "bob10". Where does the 2nd argument go and what is it for?

More Testing:

function BobsToolbox:RunTest() local test1, value1 = pcall(BobsToolbox.Test1, "string value"); SharpDeck:Print("Test1: " .. tostring(test1) .. " Value: " .. tostring(value1)); end

function BobsToolbox:Test1(arg1) return arg1 .. "10"; end

Results: attempt to concatenate local 'arg1' (a nil value)

function BobsToolbox:RunTest() local test1, value1 = pcall(Test1, "string value"); SharpDeck:Print("Test1: " .. tostring(test1) .. " Value: " .. tostring(value1)); end

function Test1(arg1) return arg1 .. "10"; end

Results: string value10

I am new to lua and I can't understand why these are different.

New Question:

The following code works but why?

function BobsToolbox:RunTest() local test1, value1 = pcall(BobsToolbox.Test1, "string value"); SharpDeck:Print("Test1: " .. tostring(test1) .. " Value: " .. tostring(value1)); end

function BobsToolbox.Test1(arg1) return arg1 .. "10"; end

What's the difference between the following: ("." vs ":")

  • function BobsToolbox.Test1(arg1)
  • function BobsToolbox:Test1(arg1)
+1  A: 

Well, I don't think WoWWiki is wrong. Here is the code I am using:

function myTest(arg1) return arg1 .. 10; end 

local retOK, ret1 = pcall(myTest,"string value"); 
DEFAULT_CHAT_FRAME:AddMessage(ret1);

local retOK, ret1 = pcall(myTest,"string value", "bob"); 
DEFAULT_CHAT_FRAME:AddMessage(ret1);

Here is the output I get in my General chat box:

string value10
string value10

How are you trying your sample code? I just pasted my code into an existing mod lua file and made sure that mod was enabled in the addons window before selecting my character and logging in. I made a few changes to the source lua file and typed:

/console reloadui

To try the new changes and have the results output to my screen. I don't have much advice to offer you, because I haven't done much work with WoW addons. Have you tried this code in a blank addon to make sure nothing else is interfering? Have you actually tried the code in game? If you can provide any more information or want me to try anything else, let me know!

Update: Decided to try a few more tests. Here are the tests (with the same function):

local retOK, ret1 = pcall(myTest,""); 
DEFAULT_CHAT_FRAME:AddMessage(ret1);

local retOK, ret1 = pcall(myTest, nil, "bob"); 
DEFAULT_CHAT_FRAME:AddMessage(ret1);

And the results:

10
attempt to concatenate local 'arg1' (a nil value)

It's interesting that the error I see when arg1 is nil is slightly different than the error you see. I'd be interested in knowing how you are testing your code. Or maybe you didn't copy the error down verbatim? I guess you could also try clearing out your WTF folder and disabling the rest of your addons to test this function. If it makes a difference, then you can enable them one a time until you find the problem.

Venesectrix
I apologize but I think I left out needed information. At the time I didn't think it was important. If you could I would like to see if you agree with my entry.
Bobby Cannon
A: 

Related: There are nice live code editors for WoW. I used to use LuaSlinger, but turns out that's no longer developed and the developer recommends Hack instead.

However, what you might be encountering here is that the colon method-call syntax is just syntax sugar, ditto for method definitions, IIRC. Basically, if you do foo:bar("quux!"), where foo is an object, you are in reality just doing foo.bar(foo, "quux!").

Hope that helps!

AKX
I apologize but I think I left out needed information. At the time I didn't think it was important. If you could I would like to see if you agree with my entry.
Bobby Cannon
+1  A: 

Lua Documentation:

http://www.lua.org/pil/16.html

This use of a self parameter is a central point in any object-oriented language. Most OO languages have this mechanism partly hidden from the programmer, so that she does not have to declare this parameter (although she still can use the name self or this inside a method). Lua can also hide this parameter, using the colon operator. We can rewrite the previous method definition as

function Account:withdraw (v) self.balance = self.balance - v end and the method call as a:withdraw(100.00)

The effect of the colon is to add an extra hidden parameter in a method definition and to add an extra argument in a method call. The colon is only a syntactic facility, although a convenient one; there is nothing really new here. We can define a function with the dot syntax and call it with the colon syntax, or vice-versa, as long as we handle the extra parameter correctly:

Account = { balance=0, withdraw = function (self, v) self.balance = self.balance - v end }

function Account:deposit (v) self.balance = self.balance + v end

Account.deposit(Account, 200.00) Account:withdraw(100.00)

Possible Conclusion:

With this in mind I assume that when calling a ":" function using "pcall" you must supply the "self" argument.

Bobby Cannon
Yes, it looks like you have found the answer! When you create the function BobsToolbox:Test1(arg1) the first argument after the function name using pcall is expected to be the object to operate on. The argument after that is the argument you actually want to use.
Venesectrix
Yes, this seems about right.
AKX