Hi, I have another noob question, I've been trying to understand how to handle errors in gideros, in java we use try/catch blocks to encapsulate what we want to test and deal with the error, in lua I found the pcall function, but it doesnt seem to work that well
if I do this, the pcall works well
function func(n)
return 'N'+n;
end
if pcall(func, n) then
print("worked")
else
print("Error")
end
--->Error |
but when I try to use it with gideros functions it doesnt work:
mychild = Bitmap.new(Texture.new("images/image1.png"))
if pcall(self:removeChild(), mychild) then
print("worked")
else
print("Error")
end
--->start.lua:88: bad argument #1 to 'removeChild' (Sprite expected, got no value) |
or if I do like this:
mychild = Bitmap.new(Texture.new("images/image1.png"))
if pcall(self:removeChild(mychild)) then
print("worked")
else
print("Error")
end
--->start.lua:88: The supplied Sprite must be a child of the caller. |
So is there a way to use some kind of try/catch block in gideros, besides pcall, that I can check if a variable is a child of self, or using another self function? or am I using pcall wrong??
tnx in advance
Comments
Likes: vitalitymobile
If you want to use pcall, change to this
I had tried with both self and mychild has arguments, but I always tried with the "self:removeChild" and not with just the .
but I now read the difference between the two calls, when I use . Im calling the function and passing self has first parameter.
I know about removeFromParent(), Im using that, this was just trying to understand how to handle run time errors with gideros, it wasnt about the removeChild function itself Im still learning
tnx again, you guys rock
Likes: vitalitymobile
not jumping into the quick draw contest here, for understanding a few things
1. the difference between : and .
there is a difference between
Hope that helps you understand pcall and the difference between . and :
Likes: ar2rsawseen, vitalitymobile
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
tnx again, you guys rock, Im loving gideros, not just for the tech itself, but cause of this awesome community, always willing to help and answering noob questions like mine
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps