0
\$\begingroup\$

I am working in Unity using Visual Scripting - but am looking here for general advice for looping algorithms!

I made a While loop that continues as long as a boolean glowactive is true. It's under a "On Enter State" that has coroutines enabled.

enter image description here

Then the code enters 2 separate For loops, that go from 0 to 20 and back again. (Actually it goes from 0 to 1 in 0.05 increments, but it's the same idea.)

enter image description here

The loop runs as long as glowactive is true... but glowactive isn't changed in the loop. However, it can be changed in other parts of the code. I read though that you should never have a While loop with a condition that doesn't change inside the loop... or you will get an infinite loop. That may well be true, because I am indeed getting an infinite loop!

Any advice on how to properly setup loops to achieve my goal? I basically want to continuously vary the _GlowPower from 0 to 1.0, by increments of .05. This makes the text glow on and off.

It works for about 2 cycles or so, and then stops... then it locks up in infinite loop.

I am not sure how to structure the code so that it loops _GlowPower from 0 to 1.0 and then back to 0, over and over... until something happens outside of this code which causes glowactive to become false. I've read up on the different kinds of loops, but something isn't clicking because nothing I've tried has fully worked. (I can easily get the glowing effect to cycle twice, but then it stops.) Please advise!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Should the glow stop in the middle of the sequence when it gets interrupted or first finish the cycle? \$\endgroup\$
    – Zibelas
    Commented Jun 2, 2022 at 9:15
  • \$\begingroup\$ @Zibelas I don't think so. the glow stopping in the middle of the sequence sounds not good-looking :D \$\endgroup\$
    – Mangata
    Commented Jun 2, 2022 at 10:40
  • \$\begingroup\$ @zibelas - You are correct, it shouldn't stop the glow in the middle of the sequence! But the core issue I'm running into is the infinite loop I'm triggering... I am totally missing something here, and I think it's triggering the infinite loop! \$\endgroup\$
    – kanamekun
    Commented Jun 2, 2022 at 10:53

2 Answers 2

0
\$\begingroup\$

enter image description here

Make a check before the for loop with Branch node, if glowactive is false, wait for one frame. Equivalent to checking before the start of each round of the full glow animation.

\$\endgroup\$
5
  • \$\begingroup\$ Ok added it! It did slow down the animation a bit, but an infinite loop still triggered... \$\endgroup\$
    – kanamekun
    Commented Jun 2, 2022 at 10:09
  • \$\begingroup\$ Yes, I suspect that I'm doing it wrong! Basically, this GameObject is in a Finite State Machine. While it's in this particular State, I'd like the text in the GameObject to glow and pulsate. When someone clicks on the GameObject, it will change states and then we recheck the GlowActive variable to see if it should be changed or not. The idea of the coroutine was to allow the "Wait for New Frame" node to slow down the pulsating... and in the meantime, to just loop through the Glow/Pulsating. Not sure how to loop from 0 to 1 (and back again) without triggering an infinite loop! \$\endgroup\$
    – kanamekun
    Commented Jun 2, 2022 at 10:25
  • \$\begingroup\$ You can start the coroutine when the object is created instead of when the state is switched. And when the state is switched, change GlowActive immediate. (And close the coroutine when object is destroyed) \$\endgroup\$
    – Mangata
    Commented Jun 2, 2022 at 10:38
  • \$\begingroup\$ << And when the state is switched, change GlowActive immediate. >> Yes, that is exactly the plan! The problem though is that an infinite loop immediately crashes Unity when GlowActive becomes true. So I think my loop structure is wrong... maybe I am not using the Once node correctly? \$\endgroup\$
    – kanamekun
    Commented Jun 2, 2022 at 10:52
  • \$\begingroup\$ I agree that. the Once node here seems to be useless. \$\endgroup\$
    – Mangata
    Commented Jun 2, 2022 at 15:29
0
\$\begingroup\$

Turned out, the issue was inside of the loop!

I was changing the glow every single frame, and that was overwhelming Unity.

I changed it instead so that the glow varies every 1/5th of a second. It now also changes by much larger increments: by +/- -0.333 instead of +/- 0.05.

That combination of changes solved the problem. Thanks to all for the thoughtful comments and feedback along the way!

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .