How do I test if a loop condition is met?

Hia. I am super new to Blueprint scripting (in fact, this is my second Blueprint) and I just spent the last 4 hours trying to make an object rotate 90 degrees. That is all I want to do… and after 4 hours of struggling I am here to ask for help…

What I want to do is say “When the state is Deactivate, rotate the x till it is 100 then set the state to Inactive” and also “When the state is Activate, rotate till the x is 0 then set the state to Active”.

That is it!

I found that if I do a while loop to test the current rotation against the hard coded vector then I end up with 0 the one frame and the 100 the next. I only see the result after the while has completed it’s loop. Okay, so clearly doing a while inside a normal function is not the answer. So I added the enum switch to the tick event and now I can rotate the object but after I rotated it I can’t seem to take the rotation and ask: “Is this now 0,100,0 or not?”. It seems after the rotation I need to CALL something, not test for something so I have absolutely no idea how to tell my rotation that it is now done and can stop rotating…

How would I do that? Could someone please give me a simple Blueprint (using int is fine) that just does this:

If var == 0, rotate from 0,0,0 to 100,0,0 over 0.5 seconds. 
If rotation is 100,0,0, set var to 1. 
If var == 1, rotate from 100,0,0 to 0,0,0 over 0.5 seconds
if rotation is 0,0,0, set var to 0

That simple thing has taken me over 4 hours so far and it is driving me insane. I am trying to see if I can work in Blueprint or not but, honestly speaking, so far I can’'t understand why everybody doesn’t just code in C++. I’m sure this entire Blueprint would have taken me 10 minutes in C++ :frowning:

What am I missing? Why is this so darn hard?

Thanks in advance for any assistance

Achieving certain goals in blueprints requires a completely different mindset. Translating C++ into BPs will not be pretty, here’s the closest you (I) can get to the mentioned pseudo code that still makes sense for visual scripting:

I mean, I could ditch the Timeline and increase rotation every Tick (frame) manually but that would defeat the purpose of using BPs. And that’s the crux, you generally choose the best tool for the job - in BPs if you want something to last precisely .5s, you’d use a timeline or a timer. Using a Timeline requires adopting a very specific workflow - feeding that one node information it needs and, in this case, it’s event driven.

In the example above rather than doing float comparison and worrying about its precision, I just let the timeline do its magic, and once it has finished its job, I play it backwards.

In an ideal world, you do the heavy lifting in C++ and manage higher level code in blueprints.


For a ping-pong like movement, I’d normally do something like this (providing that’s all what’s needed):

Image from Gyazo

Thank you so much for this! I’m gonna save this out and study this in detail to see if I can get my project working this weekend. For now all I wanted was what I explained above because I wanted to see how I check for a loop to end… once I got that basics up and running I could plug in the holes with variables down the line. That would have been the easy part. Creating the enum and causing the various state changes to to trigger the relevant actions, that all was easy as pie… telling a rotation “You’ve gone far enough. Stop” was doing my head in.

The final code should work like this when it’s all said and done… When the game starts all objects get deactivated and animate down. Each object now gets a random timer after which it contacts the parent object to ask “How many of us are active?” and if too many are active then anew timer is assigned to the object. If there is an open slot then the object’s state is set to Activating and it starts to animate up. Now when the parent is asked how many objects are active, the state of this one not being Inactive means it gets counted as one of the actives ones. After a random time between X and Y seconds it would deactivate itself and animate down.

Finally, after a certain amount of time the speed at which they animate up or down also gets increased for a total of 5 different speeds from start to finish. I actually had a Timeline in there before because I like the dampening effect it would provide… but I got rid of it when I realized I had no idea how to change it to run for shorter periods of time…

And that is the sum total of what i was trying to create this weekend. In Unity I could do this in C# in under an hour and in C++ I think I should be able to do close to that but I am trying to give Blueprints a fair chance and see how much I can do with them… so thank you again for your assistance! :smiley: Very much appreciated.

Now to go study what you did…

You make it look so easy… :open_mouth:

Blueprint is “easy”. That is why people who already know how to use C++ also use Blueprint when they just want a quick prototype of something.

I actually had a Timeline in there
before because I like the dampening
effect it would provide… but I got
rid of it when I realized I had no
idea how to change it to run for
shorter periods of time…

The right click Blueprints Actions menu is context sensitive providing you do not right click to invoke it, ehm. It’s a poor man’s IntelliSense of sorts. When you add a timeline to a blueprint graph, it automatically creates a reference which you can then use (drag a wire from it) to see what this component is capable of:

You were looking for Set Play Rate - at 2.0 it will play the Timeline twice as fast; at 0.1 will make it last 10 times longer.

You can’t really update the key values inside the timelines (apart from loading an entirely new curve) - in my 2nd example the Timeline outputs 0-1 alpha while Lerp takes care of the range.

Regarding the description of the desired behaviour, it should not take longer than an hour to put it all together if you’re familiar with the tools at hand.

Do tell if something does not add up.

Well, this took far longer than expected but I ran into unexpected issues along the way and thanks to these struggles I have learned a few extra things and am all the better off for it. :smiley:

My main issue after reading your advice here came in the form of this: Let me give you an example: There are 3 farmers and 50 chickens. Obviously the chickens won’t spawn as child meshes of the farmers and yet I do want the chickens to know who they belong to. To do this I keep an array of Chickens on each Farmer and during start the farmer tells each chicken “I own you” by storing itself in the Chicken’s Owner variable. Great, so far so good… The farmer can speak to the chickens … but whenever the Chicken tries to access anything on the farmer (i.e. the farmer’s level) it just refuses to give me access to anything on the farmer.

After I figured out I have to create a function and give it inputs of the relevant class, well then variable suddenly exposed everything just fine inside the function. Weird that I had no access in the graph directly but inside a function everything works…

Anyway, once I was able to fetch the level from the parent I was finally able to get down to the business of doing the job at hand. 1 function to set the playback speed as (level * 0.1) + 1. 1 function to set the animation to start from the relative rotation so a half open rotation can close down again rather than snapping all the way up first and THEN rotating down… and with those two functions out of the way the rest was as easy ■■■ you made it look. :smiley:

Thanks for your help. Also, I was going to look into changing the animation LENGTH, not the RATE so I think you saved me a lot of grief there without even knowing it.

Thanks again! :smiley: I feel more confident in using BP now. I’m still no expert (after day 2 :P) but at least I feel I can help myself now and eventually start getting good at this… eventually :stuck_out_tongue: