Function return and strings

Posted by Dyron on Wed 13 Feb 2013 09:12 PM — 3 posts, 14,974 views.

#0
On Lusternia stances are able to avoid kicks. So I'm making an automated script to adjust my kick by their stance. I am having trouble with the return statement though. It's returning as a table and not a string.


tarstance = {}
tarstance.legs = false
tarstance.head = false



function yanker()
  local kicklimb = {}
  local kicklimb = dokick()
  send ( "kata perform " .. target .. " ninshihe" .. kicklimb )
end



function dokick()

  local toReturn = {}

    if not tarstance.head then 
	local toReturn = "he"
	Note ( "toReturn is " .. toReturn )
    else
	local toReturn = "rl"
	Note ( "toReturn is " .. toReturn )
    end
	
  return toReturn
end


The note shows it correct but when it returns it, it's a table it seems. How do I stop this?


#1
Dyron said:


function dokick()

  local toReturn = {}

    if not tarstance.head then 
	local toReturn = "he"
	Note ( "toReturn is " .. toReturn )
    else
	local toReturn = "rl"
	Note ( "toReturn is " .. toReturn )
    end
	
  return toReturn
end


The note shows it correct but when it returns it, it's a table it seems. How do I stop this?



Remove the local in the if else clause. The local keyword makes the scope of the variable only apply to the if or else part.

It should look like this

function dokick()

  local toReturn = {}

    if not tarstance.head then 
	toReturn = "he"
	Note ( "toReturn is " .. toReturn )
    else
	toReturn = "rl"
	Note ( "toReturn is " .. toReturn )
    end
	
  return toReturn
end


Bast
#2
Works like a charm! Thanks a ton.