How do I break a function?

Get help using Construct 2

Post » Thu Jun 16, 2016 8:04 am

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++.

Code: Select all
void function()
{
   if (i == 5)
   {
      cout << "I was equal to 5";
      return;
   }

   cout << "I was unequal to 5";

}


Thank you!
Image Image iTunes: Match3 | Google Play: Match3
B
104
S
37
G
31
Posts: 1,164
Reputation: 28,953

Post » Thu Jun 16, 2016 8:18 am

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.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Thu Jun 16, 2016 9:08 am

99Instances2Go wrote: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.
Image Image iTunes: Match3 | Google Play: Match3
B
104
S
37
G
31
Posts: 1,164
Reputation: 28,953

Post » Thu Jun 16, 2016 9:19 am

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)
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Thu Jun 16, 2016 9:20 am

99Instances2Go wrote: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!
Image Image iTunes: Match3 | Google Play: Match3
B
104
S
37
G
31
Posts: 1,164
Reputation: 28,953

Post » Thu Jun 16, 2016 9:23 am

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
Image Image
B
25
S
14
G
139
Posts: 877
Reputation: 72,512


Return to How do I....?

Who is online

Users browsing this forum: MrSkailo and 13 guests