How to break out of an if-statement?

I have a character that I want to be able to “grab” an object so that it moves with him. I want to go into a “grab” mode when the object is touched and then exit the “grab” mode when the object is touched a second time. I’m having trouble figuring out how to do this. I know why my current code isn’t working right (it’s enabling the grab and then disabling before exiting the function), but I feel like I need some sort of a way to jump out of the if loops when one condition is satisfied… so only one thing (turning grab mode on or off) is being done per click.

[lua]function grabEnabled(event)
if event.phase == “began” then
if grabIsEnabled == false then
if crate.x - character.x <= 5 then
print(“grab enabled”)
grabIsEnabled = true
end
end
if grabIsEnabled == true then
grabIsEnabled = false
print(“grab disabled”)
end
end
return true
end[/lua]

The second question: Is there a better way to “link” the character and object together besides forcing the x-values? Is there some way to temporarily join the two objects through the physics engine? [import]uid: 146966 topic_id: 34909 reply_id: 334909[/import]

There is a “break” statement used to terminate loops early and are typically inside of “if” statements, but you can’t break out of an if, it only terminates loops like for, while and repeat.

The return statement can be used to terminate a function early. To get out of the middle of an if, you just have to structure the if correctly.

As for the 2nd question, I’m not a physics expert, but I think this is the use for a joint. [import]uid: 199310 topic_id: 34909 reply_id: 138737[/import]

your code can be changed to:

function grabEnabled(event) if event.phase == "began" then if grabIsEnabled == false and (crate.x - character.x) \<= 5 then print("grab enabled") grabIsEnabled = true else grabIsEnabled = false print("grab disabled") end end return true end [import]uid: 31508 topic_id: 34909 reply_id: 138755[/import]

Thanks bpran that worked.

BTW, I’ve been playing around with the distance joint and I still have some bugs to work out, but it is working much better than setting crate.x to character.x + some value. [import]uid: 146966 topic_id: 34909 reply_id: 138933[/import]

There is a “break” statement used to terminate loops early and are typically inside of “if” statements, but you can’t break out of an if, it only terminates loops like for, while and repeat.

The return statement can be used to terminate a function early. To get out of the middle of an if, you just have to structure the if correctly.

As for the 2nd question, I’m not a physics expert, but I think this is the use for a joint. [import]uid: 199310 topic_id: 34909 reply_id: 138737[/import]

your code can be changed to:

function grabEnabled(event) if event.phase == "began" then if grabIsEnabled == false and (crate.x - character.x) \<= 5 then print("grab enabled") grabIsEnabled = true else grabIsEnabled = false print("grab disabled") end end return true end [import]uid: 31508 topic_id: 34909 reply_id: 138755[/import]

Thanks bpran that worked.

BTW, I’ve been playing around with the distance joint and I still have some bugs to work out, but it is working much better than setting crate.x to character.x + some value. [import]uid: 146966 topic_id: 34909 reply_id: 138933[/import]