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)
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 partially hidden from the programmer, so that she does not have to declare this parameter (although she still can use the word
selforthisinside a method). Lua can also hide this parameter, using the colon operator. We can rewrite the previous method definition asand the method call as
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:
Possible Conclusion:
With this in mind I assume that when calling a ‘:’ function using ‘pcall’ you must supply the ‘self’ argument.