How Do Functions Work?

0 favourites
  • 12 posts
  • I am curious if someone can help clarify how functions work.

    I have a function here where the opacity is decreased with a lerp. It does not work. It seems like it only runs once when called.

    Of course if I move out the condition string condition "Foo" outside of the function it works fine. But would it not be best practice to encapsulate all the conditions inside of the function it pertains too?

    Thank you in advance.

  • Functions only run when called, so that function is only ever called once on start of layout. The events then don't make sense, because they use lerp and dt in an event that only runs once, which won't work like you probably think it does.

  • If you want to use the function like this for fading out the sprite, you either need to call this function multiple times (every tick), or use a loop inside the function, something like this:

    Repeat 100 times
    	Wait 0.016*loopindex
    	Sprite set opacity to (self.opacity-1)
    
  • Also, for stuff that happens in a cycle, I always prefer using an object with a timer behavior. Not saying this is the right way to do, I just like it that way as it keeps things more organized from my pov.

  • Thanks everyone for the great replies. I think I will add a do while type loop in my fade function so that I can run the fade and turn off inputs until the fade is complete.

    Thanks again Ashleydop2000brunopalermo

  • A do while won't be seen gradually. It will just execute super fast and you won't be able to see the fading...

    You can see how they work in this file.

    Hope this helps...

    Cheers!

  • the event sheet runs 60 times a second, so trying to slow down the execution of a function with a while loop probably will not work.

    dop2000's repeat loop works because it adds a series of ever increasing waits. but the function itself finishes in one tick.

    is there a reason you don't want to use the Fade behavior, and the On fade finished trigger?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • AllanR thanks for your reply. I did another test using the fade behaviour but I recall not have access to on fade complete in the scope of the function. I was initially trying to copy what they are doing in the kiwi story game. I will test fade again and the other methods described with my new knowledge.

    Again, thanks

  • the on fade complete is a trigger and should go out at the top level, not in the function.

  • So I have a functioning fade in and fade out using the fade behavior. I just have one issue now with a very jarring flicker at the beginning of the fades. My goal is to have a function that can fade from black to visible and from visible to black. Here is the code I currently have in place. I am unsure how to get a smooth transition.

    Thanks in advance.

  • I gave it a try... the only way I could do it with no flicker was to have the Fade sprite visible and covering the screen at the start of the layout. So, I put it in its own layer, and made the layer hidden so it doesn't cause trouble editing other things on the layout.

    I added Fade to the sprite twice - once called FadeIn and once called FadeOut. That way I could have everything preset - just not enabled.

    at the start of layout I enable FadeOut, so the screen goes from Black to visible. We get a trigger when that is done, so we know the game (or menu) is ready to go.

    when it is time to leave the layout, I start FadeIn to go from visible to Black. That gets its own trigger, so we know when to call the next layout.

    I didn't use a function or any global variables - just the triggers.

    https://www.rieperts.com/games/forum/FadeInOut.c3p

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