How do I break a function?

0 favourites
  • 6 posts
  • Hi!

    I have a function in my game and I was wondering if there is an action to break it if a certain condition is true. So that the rest of the function will ned be executed? Here is a little example in C++.

    void function()
    {
    	if (i == 5)
    	{
    		cout << "I was equal to 5";
    		return;
    	}
    
    	cout << "I was unequal to 5";
    
    }[/code:57b7tl5w]
    
    Thank you!
  • There is no use for breaking a function. It always returns to its call.

    A loop you can break with System > (General) Stop Loop. Keep in mind that all actions and events after the 'stop loop' will continue to run, the 'stop loop' just prevents the loop from running again from its top-event.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • There is no use for breaking a function. It always returns to its call.

    A loop you can break with System > (General) Stop Loop. Keep in mind that all actions and events after the 'stop loop' will continue to run, the 'stop loop' just prevents the loop from running again from its top-event.

    I know that a fucntion always returns to its call position, but I want to return earlier if a certain point has been reached in the function.

    It's hard to explain, but I have a function creating a path randomly. Sometime it reaches the edge before the function has been completed, so I want to return then to the caller, so that the rest of the function will not be executed.

  • Or break the loop. Or skip conditional actions by making the conditions not true. It will return.

    A 'while" would be a better choice for the loop, btw.

    Local var check = 0

    Local variable i (if a counter is needed)

    While

    check = 0

    evaluate and do stuf

    break condition > set check = 1

    add 1 to i (if needed)

  • Or break the loop. Or skip conditional actions by making the conditions not true. It will return.

    A 'while" would be a better choice for the loop, btw.

    Local var check = 0

    Local variable i (if a counter is needed)

    While

    check = 0

    evaluate and do stuf

    break condition > set check = 1

    add 1 to i (if needed)

    Yes that's what I've done to avoid it. But I thought there is a better solution for that

    Thank you!

  • if your void function() is running every tick you could just add a compare 2 values condition with an else condition, include this in a group and disable the group when you want to break/return

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)