A = {}
function A:text()
return 100
end
print(A["text"]()) -- prints "100"
----------------------------------
A = {}
function A:text(value)
return value
end
print(A["text"](100)) -- prints "nil"
Is there a way that I can pass a value as a parameter and return the same value?I need to loop through 5 functions...
You could, if you declared your function correctly.
function A:text(value)
This creates a function that takes two parameters. The : is what's responsible for that. The first parameter is an implicitly declared parameter called self. The second is value. This function is intended to be called as A:text(100) or with A["text"](A, 100).
These are for class-member-like functions.
You should instead create the function like this:
function A.text(value)
This creates a function that takes one parameter.
As "Nicol Bolas" pointed out, I add table/self parameter and it worked fine.
-- from "A["text"](100)" to "A["text"](self, 100)" or "A["text"](A, 100)"
A = {}
B = {"text", "type"}
function A:text(value)
return "text "..value
end
function A:type(value)
return "type "..value
end
for i=1, 3 do
for j=1, #B do
print(A[B[j]](self, i)) -- prints "text 1 type 1 text 2 type 2 text 3 type 3"
end
end
Related
How do I correctly perform a for-loop that returns as a f-string (like in Python.)
For example,
for i=10, 1, -1 do
test = string.gsub('checkbox[val = "a"]','a', i)
return test
end
Returns
checkbox[v10l = "10"]
However, I was expecting:
checkbox[val = "1"]
checkbox[val = "2"]
..
..
checkbox[val = "10"]
I'm honestly not sure what are you trying to do here. Lua has no supports for f-strings and you're trying to return multiple times from function?
This is probably the closest to what you want:
for i=1, 10 do
print(string.format('checkbox[val = "%d"]', i))
end
You're doing the following:
Running for loop from 10 to 1, in opposite direction of what you want.
Replacing all occurrences of string a in checkbox[val = "a"] with loop variable, giving you resulting string and assigning that to (global, which is not good) variable test.
Returning string you just got, thus exiting loop and function that contains loop.
If you're trying to pass multiple values from loop to the outside world, you can do one of the following:
Add them into table and return it:
local tab = {}
for i=1, 10 do
tab[i] = string.format('checkbox[val = "%d"]', i)
end
return tab
Accept callback, so caller passes in a function to call on each value:
function example(cb)
for i=1, 10 do
cb(string.format('checkbox[val = "%d"]', i))
end
end
example(print) -- Here, we pass `print` as a callback, but it's usually your own function
Choose whatever better suits your particular interface.
i was learning on how to create and use a class on a youtube video and i came across this code
cc = {calo = 0, goal = 1500}
function cc:new(t)
t = t or {}
setmetatable(t,self)
self.__index = self
return t
end
a = cc:new{goal = 100}
i dont understand this part a = cc:new{goal = 100} this is the first time where i see a function being called with anything other then (). i have 2 guesses on what this does maybe it replaces the parameter of cc:new function with {goal = 100} or maybe the function is called and t table is assigned to the variable then assigning the table with {goal = 100}? pls correct me if im wrong
first, {goal = 100} is just an argument.
second, cc:new{goal = 100} is equal to cc:new({goal = 100})
this is a syntactic sugar, you can call a function without brackets if theres only one argument and its type is string or table literal
example:
function foo(x)
print(x)
return foo
end
foo "Hello" "World"
this will output "Hello" and "World"
if you want to call a function without brackets and using multiple arguments, the function you gonna call must return another function for the next argument.
the another function is not always the original one
it may be recursive
I'm creating a dynamic table method reference and trying pass a single param to the method. The dynamic method reference does work and the table method is called just as expected, however the completely not nil param I'm passing to the method is nil inside the method. Can you point out my error in these 2 lines?...
Here is a small working example that demonstrates. On first line in the Consider:Move method, mons is nil
local Consider = {}
function Consider:Move( mons )
print( 'Mons ' .. mons.type .. ' considering Move...')
actionChosen.score = 0
return actionChosen
end
local mons = { type = 'Blue' }
local actionPref = 'Move'
local considerAction = Consider[actionPref]
print( 'MonsterAI:chooseAction mons: ', mons.type )
local actionTest = considerAction( mons )
Functions defined using the colon operator hides an additional first self argument. function Consider:Move(mons) is syntactic sugar for function Consider.Move(self, mons).
Calling the function like considerAction(mons) sets the hidden self argument instead of the desired one.
You might want to pass the Consider table as self:
considerAction(Consider, mons)
Or, alternatively, define the function using the dot operator if you don't need self:
function Consider.Move(mons)
print('Mons ' .. mons.type .. ' considering Move...')
end
function ReturnTwoVal()
return "1","2"
end
function ReturnThreeVals()
return "x","y","Z"
end
TblA = {ReturnThreeVals(),ReturnTwoVal() }
print(TblA[2],TblA[1], TblA[2], TblA[3], TblA[4])
Output will be: 1 x 1 2 nil
Expressions that return multiple values are adjusted to a single value, unless they are the last expression in a function call or table constructor.
Therefore,
TblA = {ReturnThreeVals(),ReturnTwoVal() }
is equivalent to
TblA = {"x", "1","2"}
local meshId = message:sub(message, message:find(message, "/hat%s%d"), message:find(message, "/hat %d+"))
The message:find() returns two values; the first character and the last character. How would I make it only return the last character?
Simply, I think solution. just second prameter return
function returnTwo(...)
local a, b = message:find(...)
return b
end
wrap message:find function and return second value
How about?
If a function returns more than one parameter, you can use select(2, functioncall()) to get the second parameter. For example:
function returntwo() return "first", "second" end
print(select(2, returntwo())) -- prints "second"
In the case in question, you'd use it as local meshId = message:sub(message, select(2, message:find(message, "/hat%s%d")), select(2, message:find(message, "/hat %d+")))