How can I utilize closures in a function within an event sheet?

0 favourites
  • 5 posts
From the Asset Store
- This pack is suitable for shooting games, running games, platform games, and side-scrolling...
  • I created a local variable called `rslt` in the function.

    I thought this would work the same as a function called `num` that the function takes as a parameter.

    But it wasn't.

    I've seen in the documentation that the value is initialized to zero after some action works.

    I tried to refer back, but couldn't find it.

    Can someone give me a link to something related to this?

    I need to use closures, but now this situation is very confusing to me.

    The following is an example code of the event sheet.

    To solve the above problem, there is a way to declare all variables that I want to use as closures as parameters, not as local variables.

    Is it only this way?

  • Not sure what you mean by "closure" here. Local variable are always reset to their default value after a wait. If you don't want to reset, set the variable as static.

  • Closure by me means that the variable is referenced out of scope.

    You can find out more by searching for JavaScript closures.

    I'm a web developer and I think Construct 3 works based on the web, so I think the problem was that I tried to write the event sheet in JavaScript code style too.

    Thanks for the answer.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Parameters will remain unchanged even after "Wait"s within a function

    Local variables will not

    So if you were to put rslt as a parameter your code would work as expected

    This is safer than using a static variable since only your function will have access to it's parameters

  • If you used a function return you wouldn't have that issue either, because the log would have both parts of the equation at the same time.

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