Introduction to C++ Programming tutorial problem

I’m on lesson 14 and I’m having problems with this line:

CPP:


// spawn the pickup
APickup* const SpawnedPickup = World->SpawnActor<APickup>(WhatToSpawn, SpawnLocation, SpawnRotation, SpawnParams);

Header file:



	/** The pickup to spawn */
	UPROPERTY(EditAnywhere, Category = Spawning)
	class APickUp* WhatToSpawn;

Error says:

1>C:\Users\Andrew\Documents\Unreal Projects\TutorialCode\Source\TutorialCode\SpawnVolume.cpp(83): error C2065: ‘APickup’ : undeclared identifier
1>C:\Users\Andrew\Documents\Unreal Projects\TutorialCode\Source\TutorialCode\SpawnVolume.cpp(83): error C2059: syntax error : ‘const’

any ideas please?

Thanks

Your CPP code is probably missing:

#include “Pickup.h”

Go back through the video and see where and when they added that line in.

Thanks Slayemin, but I’m still having problems. In my header I have:


	UPROPERTY(VisibleInstanceOnly, Category = Spawning)
	class UBoxComponent* WhereToSpawn;

That doesn’t match what they have in the tutorial. I changed it because I read TSubclassOf WhatToSpawn; is the old fashioned way to do it.

The build output says this:


1>C:\Users\Andrew\Documents\Unreal Projects\TutorialCode\Source\TutorialCode\SpawnVolume.cpp(83): error C2664: 'T *UWorld::SpawnActor<APickup>(UClass *,const FVector &,const FRotator &,const FActorSpawnParameters &)' : cannot convert argument 1 from 'APickup *' to 'UClass *'

any ideas please?

Your identifier is wrong.
In the .cpp you have it declared as APickup, but the .h has it as APickUp. With a capital U.

Thanks but still getting this error:


error C2664: 'T *UWorld::SpawnActor<APickUp>(UClass *,const FVector &,const FRotator &,const FActorSpawnParameters &)' : cannot convert argument 1 from 'APickUp *' to 'UClass *'

my Header:


#pragma once

#include "GameFramework/Actor.h"
#include "SpawnVolume.generated.h"

UCLASS()
class TUTORIALCODE_API ASpawnVolume : public AActor
{
	//defaults to private
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ASpawnVolume(const FObjectInitializer& ObjectInitializer);

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	void EnableSpawning();

	void DisableSpawning();

	UPROPERTY(VisibleInstanceOnly, Category = Spawning)
	class UBoxComponent* WhereToSpawn;
	
	/** The pickup to spawn. */
	UPROPERTY(EditAnywhere, Category = Spawning)
	class APickUp * WhatToSpawn;

	//Min Spawn Delay
	UPROPERTY(EditAnywhere, BluePrintReadWrite, Category = Spawning)
	float SpawnDelayRangeLow;

	//Max Spawn Delay
	UPROPERTY(EditAnywhere, BluePrintReadWrite, Category = Spawning)
	float SpawnDelayRangeHigh;

	/** Finds a random point within the BoxComponent */
	UFUNCTION(BlueprintPure, Category = Spawning)
	FVector GetRandomPointInVolume();


private:

	/** Whether or not spawning is enabled. */
	bool bSpawningEnabled;

	/** Calculates a random spawn delay */
	float GetRandomSpawnDelay();

	/** The current spawn delay */
	float SpawnDelay;

	//Handles the spawning of a new pickup
	void SpawnPickup();
	
	/** The timer for when to spawn the pickup */
	float SpawnTime;

};

My CPP:


#include "TutorialCode.h"
#include "SpawnVolume.h"
#include "PickUp.h"


// Sets default values
ASpawnVolume::ASpawnVolume(const class FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	//create the simple staticmeshcomponent to represent the pickup in the level
	WhereToSpawn =  ObjectInitializer.CreateDefaultSubobject<UBoxComponent>(this, TEXT("WhereToSpawn"));


	//Set the staticMeshComponent as the root component
	RootComponent = WhereToSpawn;

	//set the spawn delayt range and get the first spawnDelay

	SpawnDelayRangeLow = 1.0f;
	SpawnDelayRangeLow = 4.5f;
	SpawnDelay = GetRandomSpawnDelay();

}

// Called when the game starts or when spawned
void ASpawnVolume::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void ASpawnVolume::Tick( float DeltaTime )
{

	if (!bSpawningEnabled)
	return;

	Super::Tick( DeltaTime );

	//Always add deltatime to out sawn time

	SpawnTime += DeltaTime;

	bool bShouldSpawn = (SpawnTime > SpawnDelay);

	if (bShouldSpawn)

		SpawnPickup();

	//Deduct spawn delay from accumulated time value

	SpawnTime -= SpawnDelay;

	SpawnDelay = GetRandomSpawnDelay();

}

void ASpawnVolume::SpawnPickup()
{
	// If we have set something to spawn:
	if (WhatToSpawn != NULL)
	{
		// Check for a valid World: 
		UWorld* const World = GetWorld();
		if (World)
		{
			// Set the spawn parameters
			FActorSpawnParameters SpawnParams;
			SpawnParams.Owner = this;
			SpawnParams.Instigator = Instigator;

			// Get a random location to spawn at
			FVector SpawnLocation = GetRandomPointInVolume();

			// Get a random rotation for the spawned item
			FRotator SpawnRotation;
			SpawnRotation.Yaw = FMath::FRand() * 360.f;
			SpawnRotation.Pitch = FMath::FRand() * 360.f;
			SpawnRotation.Roll = FMath::FRand() * 360.f;

			// spawn the pickup
			APickUp* const SpawnedPickup = World->SpawnActor<APickUp>(WhatToSpawn, SpawnLocation, SpawnRotation, SpawnParams);
		}
	}
}

FVector ASpawnVolume::GetRandomPointInVolume()
{
	FVector RandomLocation;
	float MinX, MinY, MinZ;
	float MaxX, MaxY, MaxZ;

	FVector Origin;
	FVector BoxExtent;

	// Get the SpawnVolume's origin and box extent
	Origin = WhereToSpawn->Bounds.Origin;
	BoxExtent = WhereToSpawn->Bounds.BoxExtent;

	// Calculate the minimum X, Y, and Z
	MinX = Origin.X - BoxExtent.X / 2.f;
	MinY = Origin.Y - BoxExtent.Y / 2.f;
	MinZ = Origin.Z - BoxExtent.Z / 2.f;

	// Calculate the maximum X, Y, and Z
	MaxX = Origin.X + BoxExtent.X / 2.f;
	MaxY = Origin.Y + BoxExtent.Y / 2.f;
	MaxZ = Origin.Z + BoxExtent.Z / 2.f;

	// The random spawn location will fall between the min and max X, Y, and Z
	RandomLocation.X = FMath::FRandRange(MinX, MaxX);
	RandomLocation.Y = FMath::FRandRange(MinY, MaxY);
	RandomLocation.Z = FMath::FRandRange(MinZ, MaxZ);

	// Return the random spawn location
	return RandomLocation;
}

float ASpawnVolume::GetRandomSpawnDelay()
{
	// Get a random float that falls within the spawn delay range
	return FMath::FRandRange(SpawnDelayRangeLow, SpawnDelayRangeHigh);
}

void ASpawnVolume::EnableSpawning()
{
	bSpawningEnabled = true;
}

void ASpawnVolume::DisableSpawning()
{
	bSpawningEnabled = false;
}

is it the way I’ve set up the class declaration like this??


UPROPERTY(EditAnywhere, Category = Spawning)
	class APickUp * WhatToSpawn;

thanks

FrankyFurter,

I suggest trying



UPROPERTY(EditAnywhere, Category = Spawning)
	class APickUp  WhatToSpawn;


Instead of storing a pointer to an APickUp, store the APickUp type itself. Just remove the asterisk.

If you’re going to have multiple blueprints that inherit from APickUp, and you want to be able to pick and choose which one to spawn inside the editor, try:



UPROPERTY(EditAnywhere, Category = Spawning)
	TSubclassOf<class APickUp>  WhatToSpawn;


Let me know if this works.

  • Stone Razor

Thanks Stone Razor, when I removed the * I was getting an error saying it can’t find the * but when I used:

it started to work. I think the idea is that you can specify which type of pick up to spawn in the editor.

Thanks

ok so now, after I’ve made the spawn volume in the editor, no battery pickups spawn.

In UDK I used to use DebugMessagePlayer() a lot. Is there something similar in c++? - so I can start looking at why nothing spawns.

Thanks

so basically, what is the unreal C++ function for printing text on screen?

Sorry for the late response. This might be what you’re looking for.



FString YourDebugMessage = FString(TEXT("Hello, world!"));
if (GEngine)
{
//Print debug message
	GEngine->AddOnScreenDebugMessage(-1, 0.5f, FColor::Yellow, YourDebugMessage);
}