How do I call a function recursively?

  • Can't find it in the documentation. Basically I just want a function to call itself, but when adding the call action inside it only other functions appear.

  • Why not use a repeat/while/for loop?

    You can try adding an inline js code that calls the function, but I don't know if it's a good idea.

  • Calling a function from within itself is definitely not possible if I am correct as there would be no way of stopping it.

    First there must be somewhere in your code that triggers the initial function call. There you can recall the function but I think it comes down to what exactly your function is doing.

  • I have used recursive functions before with no problem...

    I just made a new test file, added a function, and it showed up fine for me when I went to add a function call within it.

  • obviously, within the function you have to check whether some condition is met before you call itself again. When that is met, it will pop back up through however many times it called itself.

    if the function is creating instances of an object, those objects wont finish being created until the next top level event, and they wont be pickable until then. So there are a number of potential ways you can get yourself into trouble.

  • here is a recursive Fibonacci example

  • doing something like this is also a bad idea unless you cache the value for recursive function (dynamic programming) this gets really expensive on the bigger the number, you can run out of stack memory very quick

  • Hmm so it appears this should be possible. Weird. Maybe I'm missing something obvious?

    As for the purpose, this is for a simple text parser that analyzes text line number X then calls itself on next/another line. Rinse and repeat until end of file. Can't just straight up repeat/loop because the parser is non-linear (jumps to a specific line upon encountering a trigger). Well technically I could do that with some logistics but why do that when there's recursion...

    doing something like this is also a bad idea unless you cache the value for recursive function (dynamic programming) this gets really expensive on the bigger the number, you can run out of stack memory very quick

    I think my use case is actually a pretty textbook example of where recursion is legit. But I might be wrong.

  • Memoization example for Fibonacci to avoid recursive stack: :

    *note; Array size were predefined

  • Recursive functions are definitely supported. It should work. It's hard to say more without seeing a sample project.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Ok identified the problem. It appears functions which return values can only be called inline while ones without returns can only be called via actions. That makes sense. So the function in question couldn't be called not only from within itself but also from anywhere else in the project.

    However, it's still a bit confusing. The function had an in-line reference to it somewhere in the project but I deleted the set return value action from it. Therefore there was no immediate indication that this was still a returning function; and on top of that even upon editing it I couldn't set return to none (choice grayed out). Once the inline reference was deleted I could change to none and the function became available for calling in actions.

    Ashley I suggest adding some kind of an indication to distinguish between returning and non returning functions or a persistent warning if a function is called inline but there's no set return action in it.

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