Showing posts with label unreal engine 4. Show all posts
Showing posts with label unreal engine 4. Show all posts

Sunday, October 9, 2016

Unreal 4 - Playing Sounds from C++ with Sound Cue using Sound Parameters (Sound Component)

Hopefully you're here if you want it clearly stated how to manipulate a sound-cue from C++ without relying on any blueprint.  I'm going to assume just about zero knowledge, so for the more experienced you may want to skip down to the code sections.

I'm powering my computer with a bicycle..., just show me the code!: (copyable code-blocks below)


Using: USoundCue UAudioComponent from C++, C (no blueprints needed)

The final result of our pitch modulated Sound Cue


Sound cue refresher (skip me if you're familiar with a sound cue)

A sound cue represents an encapsulated group of logic around a sound or sounds. A sound cue can switch between playing multiple sounds, apply effects to the sound based on input, etc.  To the engine, a sound cue is a singular object/sound, but may contain quite a lot of complexity, and mix together many different sounds.  Sound cue's contain parameters just like materials contain parameters, and these parameters can be set during runtime to manipulate how the sound-cue behaves.  In our case we are going to modify a modulation parameter to control the pitch of a looping sound cue. 

Setup our sound cue in the editor (skip me if you have a sound cue w/parameters)

Step 1.) Create a new sound cue (content browser - Add New - sounds - sound cue)


Step 2.) Save the sound-cue with a name (in my case 'airplane-engine') and open it.









Step 3.) Add a Wave Player to the Sound Cue.


Step 4.) Add a Continuous Modulator to the Sound Cue




Step 5.) Connect Wave Player to Modulator to Output.








Step 6.) Click on the Wave Player to configure it, and set what sound it plays (.wav file)
In our case, I want the sound to loop continiously, so I check 'Looping'.







Step 7.) Configure the modulator

It's important the parameter value is set, in my case I only care about manipulating pitch at runtime, and not volume, however you can set parameters for both if you need to manipulate both during runtime.  We will use this parameter name 'pitch' later in our code.



 Step 8.) My real configuration for my propeller







Step 9.) Save the cue and close it...







Code Configuration

First, I'm going to place all my logic in a Pawn class that inherits from AActor... you can theoretically place your sound logic anywhere, but this example will include a pawn.

Step 1.) We need to setup some fields in our header to hold references to our objects.  Ensure you use the UPROPERTY() macros, as this prevents unreal from garbage collecting the object before it should.

Create a pawn class and in your header file ensure these properties and methods are defined.



Step 2.) We need to configure our constructor to get references to and configure our sound objects.

Step 3.) We need to add our Sound Cue to our Audio Component, which you should avoid doing in the constructor, so we'll perform the same action in PostInitializeComponents()


Step 4.) When our pawn is spawned (-1 for rhyming) we need to have it start playing the Cue audio. You may want to do this based on some other event, but for my needs I want the sound to start playing on BeginPlay


Step 5.) Each frame we need to update the sound parameter pitch based on our engines speed.



Thursday, May 26, 2016

Unreal 4 - this declaration has no storage class or type specifier (Fixing Failed Compiles)

The Unreal Header Tool (UHT)


The first thing to note about Unreal Engine 4 (UE4) is that the engine generates some headers for you. This is true for any classes, enums, and structs that has a UCLASS, UENUM, USTRUCT macro defined above them.  These 'decorators' automatically wrap your class, struct, enum with special reflection meta information that gives the Editor and game engine information about the object or interface that allows a designer to use or subclass a C++ code implementation in blueprint.

If your project won't compile or you're getting strange errors, the first thing to check is the Visual Studio output log.  The UnrealHeaderTool can fail, which will result in either no errors or warnings, or will result in missing *.generated.h  headers.  This isn't immediately obvious.  To check for this you need to open the output pane, by going to view - output or by pressing ctrl + alt + O.

One of the most common signs the Unreal Header Tool has failed is this message next to a GENERATED_BODY() macro statement indicating:

this declaration has no storage class or type specifier 



If the output pane contains any error you must correct these to get your compile working again!

In the image below you'll see I've highlighted the first error, this error resulted in erroneous editor errors so it's important to check the output pane first!



Generated Headers

When the engine encounters GENERATED_BODY() macro statements it responds by generating a header which is meant to be included in the source file.  For each source file with unreal engines reflection macro statements in them, there should be a corresponding generated file following the name:
FileName.generated.h.  To work this include must be the last include in the source file.

If you have a missing generated header, or the solutions above did not correct your problem you should try re-generating all project files.  This can be done by navigating to the main directory of your project.  (In Documents/Unreal Projects/NameOfProject).
Click right on your .uproject file then click "Generate Visual Studio project files"



Incomplete Compiles and Intellisense

Intellisense uses information created during the code analysis and build stages to know if code is formed correctly.  An incomplete compilation or even a breakpoint being set that prevents a program from fully running will generate erroneous Intellisense errors.

Before you correct any errors in your project make sure you follow the steps above, and disable breakpoints while ensuring you have allowed a full compilation to take place.