How to 'start' or 'play' a curve asset in C++

Hello all! I was wondering how it would be possible to start a curve asset in C++. Thanks!

Probably a better way to do this but I created a curve actor type with functions grabbing length, position or tangent at time.

These functions are tagged blueprintimplementableevent and implemented in blueprint.

In my case these actors register themselves in a c++ map in begin play (and remove themselves in endplay) allowing c++ to find an editor authored curve by name. This allows for curves streaming in/out with levels.

Clunky but works, didn’t take too long to implement.

Could you post a screen shot?

Zip attached with code - you’d need to replace the Errorf with a standard UE_LOG and change headers to use in your project.

Here’s a little wrapper:

namespace Curves
{
	bool GetCurvePositionAtTime(const FString &rCurveName, float fTime, FVector &rOut)
	{
		auto *pCurve = AMM_Curve::FindCurve(rCurveName);
		if (pCurve == nullptr)
			return false;

		rOut = pCurve->GetCurveValueAtTime(fTime);

		return true;
	}
}

[Edit] Will attach blueprint image in a second, just upgraded to the latest preview and building new shaders…
[Edit2] Here’s the blueprint - You could set this up in a simpler fashion, in my case I wanted to allow blueprints to be able to choose different curves for the C++ to use based on their author’s logic. I would assume you could place a spline directly in an actor as a UPROPERTY.

[link text][2]

I was wondering more how to make it lerp like a timeline does in bleuprints, not just get a certain time.

And I am not using blueprints.

Then perhaps look into using UCurveVector or UCurveFloat. You’ll still need to write a little class to do the interpolation but that’s really simple - you just need to increment a float by deltaTime in some Tick function and use that to get the value at offset in the curve.

Or even implement your own Hermitic / Bezier / Catmull-Rom (first and last being generally my favorites) spline in C++ :slight_smile: The curve interfaces I’ve seen in Unreal are pretty robust, allow for some cool editing etc but if you’re not using any of that functionality a simple curve interpolation system isn’t very hard to put together.

That is what I was talking about when I said ‘curve asset’ sorry about that, should have been clearer. You mentioned that you can make a float curve have a lerp of sorts, in regards to that, maybe you could answer this question: https://answers.unrealengine.com/questions/390226/how-to-use-a-curve-to-create-a-smooth-transition-c.html

saw the other thread but wasn’t entirely sure how it was different to this one.

Suggest you post some code and try and explain the bit you’re missing.

That is the thing, I am not sure where to start, I have the curve involved in the script, but I am not sure how to get it to lerp like a timeline.

You said to get a Ucurvefloat to lerp, you need to increment a float by deltatime in a tick function and use that to get a value from the curve, could you post a screenshot of that? I think thats what I am looking for, but I dont know what code it requires. (I am new to coding in C++, coming to c++ from C#)
*Like just the syntax of what it needs, not a full script.

One last thought is you’re trying to do this in C++ not Blueprint. Is that for a specific reason - such as learning C++ (in which case I’d think Unreal might be a tough environment to learn)? Blueprints and editor defined objects provide much of the power of unreal so I’m not sure why you’d restrict yourself to avoiding them.

Well, the website lost my more involved response, so my “last thought” didn’t follow the detail it was meant to. Which sucks.

You’re going to need some sort of AActor with a Tick function. Look these up in the documentation.

For instance:

UCLASS()
class MM_04_API AMM_Butterfly : public AActor

In that you’ll want a timer that can be incremented (there are also functions within the actor to ask how long its been alive).

float m_fTimer = 0.0f;

in the Tick function you’re going to want to increment that value

I’m not sure what sort of handle you have on your curve (post screen shots, code snippets for better help!) but you can put something like this in your actor:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Attraction)
	UCurveVector *SomeCurve;

And in your Tick function you can also use your timer to get a value back from this:

virtual void Tick(float DeltaSeconds) override
{
	m_fTimer += DeltaSeconds;

	if (SomeCurve)
	{
		DoSomethingCoolWithThis(SomeCurve->GetVectorValue(m_fTimer));
	}
}

There is a host of documentation of AActor’s, Tick functions, Timers, etc.

Thanks! Going to have a look at the documentation, and I am not sure what: UCLASS()
class MM_04_API AMM_Butterfly : public AActor
means? Could you link me to the doc? And for the DoSomethingCoolWithThis would it be possible to do a GetCurvePositionAtTime, like would that have the value gotten be lerped?

And the reason I am using C++ is because Blueprints change a lot and are unstable, and it feels kind of cheap to use them, since it is not really programming, more like an interface.

I’ve been using blueprints since version 4.4ish. A couple of early engine updates needed some rework but overall they’ve been usable and stable. You can write some fairly complex things in them and overall the power of unreal comes from combining blueprints and the editor for data editing with c++ where needed for either behavior or performance.

The butterfly class is a custom class of mine that I snipped bits of to demonstrate. My prior lost example was more obviously named :frowning:

Given the concepts you’re wondering about my advice is to follow tutorials. There are basic tutorials for placing actors in the world, attaching blueprint behaviors and creating custom c++ classes to further develop behaviors. A few hours spent following these tutorials and exploring the example content provided in the engine will give you a more solid understanding of the engine to build on before you try implementing something of your own.

Topics to look at:

  • placing an actor
  • Adding a blueprint behavior to an actor
  • Adding a custom c++ actor class
  • Exposing c++ properties to the editor
  • Calling c++ functions from blueprint
  • Calling blueprint functions from c++

Its okay, I prefer not to use blueprints for various reasons.

Thanks for all the responses! I have figured out what I am going to do, use a tick function similar to what you did, to lerp the curve value, Thanks.

Hope something helped - you can accept one of my responses as an answer to close the subject and give me brownie points!

Yeah, sorry about that.