Functions with return value containing a loop do not work in while and nested loops?

0 favourites
  • 3 posts
From the Asset Store
For Adventure, Action, Heroes, Sci-fi and Action-RPG games.
  • Guys i have a strange problem and i don't know why this is (was) happening but i need some feedback from you C3 pros. I'm doing a "Bust a Move" like game and like to do some Functions with return value.

    Every orb (Objectname: Ball_Object) in my game belongs to the family "Ball".

    There is this funtion with a string return value: "TestNeighbour(Ball_Object.UID)".

    "TestNeighbour" will pick the ball with the UID, then start a loop to test an Instance of family "Ball" overlapping point around 6 coordinates, at a fixed distance every loopindex * 60 degrees around it. This tests if the neighbour ball is actually there. I do this calculation with a simple, also returning trigonomic function i wrote which returns X and Y. Every found UID is saved in an string variable, which i will return at the end.

    I tested this function when mouse clicking on instances of Ball_Object and it works fine!

    Now i want to do a loop, testing all balls as long as new are found. I use several loops to perform different actions. Main loop is a for loop. The code is not so big and i use nested loops for repeated actions. Everything works fine till i get to the nested while loop. NO it's not hanging up but i recognised a strange behaviour which drives me mad. I want to set a local variable called: "current_neighbours" to the return value of "TestNeighbour" with simply doing:

    Set "current_neighbours" to Functions.TestNeighbour(Ball_Object)

    Thats all i'm doing and directly testing "current_neighbour" afterwards it contains nothing. I mean really nothing.

    At first i thought my function was wrong and returned nothing. But debugging and testing it showed that it works.

    Then setting a breakpoint into my function revealed, that the function was not even called. Step, by step debugging showed, the programm got to the point where i set the variable to this function return value and it got completely ignored. Like i haven't typed anything at all. "current_neighbors" =

    After having always problems with nested loops and functions i tried our something dirty. Instead of a loop in the Function "TestNeighbour", i deleted the loop and copied the same code 6 times in a row. And what should i say, it works. Now my variable is set to the return value.

    In code the main program looks like this:

    -----

    for loop

    {

    while

    {

    ... (Some code before)

    current_neighbours = Functions.TestNeighbour(Ball_Object.UID)

    ... (Some code afterwards)

    }

    }

    -----

    So what is going on there and why does this not work:

    -----

    string TestNeighbour(Ball_Object.UID)

    {

    Pick Ball_Object with Ball_Object.UID

    string variable_to_return = ""

    loop type of your coice (6 times)

    {

    Do this and add UID of found, surrounding Balls to variable_to_return

    }

    return variable_to_return

    }

    ------

    But this works fine:

    ------

    string TestNeighbour(Ball_Object.UID)

    {

    Pick Ball_Object with Ball_Object.UID

    string variable_to_return = ""

    Do this and add UID of #1 found, surrounding Balls to variable_to_return

    Do this and add UID of #2 found, surrounding Balls to variable_to_return

    Do this and add UID of #3 found, surrounding Balls to variable_to_return

    Do this and add UID of #4 found, surrounding Balls to variable_to_return

    Do this and add UID of #5 found, surrounding Balls to variable_to_return

    Do this and add UID of #6 found, surrounding Balls to variable_to_return

    return variable_to_return

    }

    ------

    I really don't know why this is not working with a loop. Are there some engine limitations or is this intendet? Maybe a bug? Please can someone explain me whats going on?

  • It's really difficult to understand the issue without an example. Can you share a small demo project?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I agree with dop, seeing a project or at least a picture of your events would be helpful. Best I can guess it's likely some small logic or picking aspect you're overlooking with your events.

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