Hello, Im following the Battery Collector tutorial (right now im on episode 20) and im getting some undeclaired identifiers errors.
I think its because they are going out of scope since Im declairing them inside the ::BeginPlay part of the code and then trying to use them inside my ::HandleNewState function, but im a newbie so i could be completly wrong.
void ABatteryCollectorGameMode::BeginPlay()
{
Super::BeginPlay();
//Find all spawn volume actors.
TArray<AActor*> FoundActors;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), ASpawnVolume::StaticClass(), FoundActors);
for (auto Actor : FoundActors)
{
ASpawnVolume* SpawnVolumeActor = Cast<ASpawnVolume>(Actor);
if (SpawnVolumeActor)
{
SpawnVolumeActors.AddUnique(SpawnVolumeActor);
}
}
.....
void ABatteryCollectorGameMode::HandleNewState(EBatteryPlayState NewState)
{
switch (NewState)
{
//If the game is playing.
case EBatteryPlayState::EPlaying:
{
//Set spawn volumes to active.
for (ASpawnVolume* Volume : ASpawnVolumeActors)
{
Volume->SetSpawnActive(true);
}
}
break;
//If we´ve won the game.
case EBatteryPlayState::EWon:
{
//Set spawn volumes to inactive.
for (ASpawnVolume* Volume : ASpawnVolumeActors)
{
Volume->SetSpawnActive(false);
}
}
break;
//If we´ve lost the game.
case EBatteryPlayState::EGameOver:
{
//Set spawn volumes to inactive.
for (ASpawnVolume* Volume : ASpawnVolumeActors)
{
Volume->SetSpawnActive(false);
}
//Block player input.
//Ragdoll the character.
}
break;
//Unknown / default state.
default:
case EBatteryPlayState::EUnknown:
{
}
break;
}
}
Im getting undeclared identifiers errors on both “ASpawnVolumeActors” and “Volume” witch are created inside the ::BeginPlay,is there a way to fix this, maybe extend the scoope of the TArray that is beeing set up? Or maybe a way to tell the computer where is should look to find them?
I would really love if someone could help with this.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "Blueprint/UserWidget.h"
#include "BatteryCollectorGameMode.generated.h"
//Enum to store the current state of gameplay.
UENUM(BlueprintType)
enum class EBatteryPlayState : uint8
{
EPlaying,
EGameOver,
EWon,
EUnknown
};
UCLASS(minimalapi)
class ABatteryCollectorGameMode : public AGameModeBase
{
GENERATED_BODY()
public:
ABatteryCollectorGameMode();
virtual void Tick(float DeltaTime) override;
// Returns power needed to win, for the HUD.
UFUNCTION(BlueprintPure, Category = "Power")
float GetPowerToWin() const;
virtual void BeginPlay() override;
//Returns the current playing state.
UFUNCTION(BlueprintPure, Category = "Power")
EBatteryPlayState GetCurrentState() const;
//Sets a new playing state.
void SetCurrentState(EBatteryPlayState NewState);
protected:
// The rate at witch the character loses power.
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Power", Meta = (BlueprintProtected = "true"))
float DecayRate;
// The power needed to win the game.
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Power", Meta = (BlueprintProtected = "true"))
float PowerToWin;
// The widget class to use for our HUD screen.
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Power", Meta = (BlueprintProtected = "true"))
TSubclassOf<class UUserWidget> HUDWidgetClass;
// The instance of the HUD.
UPROPERTY()
class UUserWidget* CurrentWidget;
private:
//Keeps track on the current playing state.
EBatteryPlayState CurrentState;
TArray<class ASpawnVolume*> SpawnVolumeActors;
//Handle any function calls that rely upon changing the playstate of our game.
void HandleNewState(EBatteryPlayState NewState);
};
I do have my SpawnVolumeActors in there but no mention of the Volume.
By the way thanks for the explenation.