Announcement

Collapse
No announcement yet.

C++ only gameplay tutorial not working on 4.21

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    C++ only gameplay tutorial not working on 4.21

    Trying to learn c++ programming for Unreal. Thought I'd give the documentations a try. Tried to follow the the c++ only light gameplay tutorial and I keep getting errors:

    Errors:
    D:\UnrealProjects\CPPTriggerLevel\Source\CPPTriggerLevel\LightSwitchOverlapCodeOnly.cpp(16) : error C2027: use of undefined type 'UPointLightComponent'
    d:\unrealprojects\cpptriggerlevel\source\cpptriggerlevel\LightSwitchOverlapCodeOnly.h(17): note: see declaration of 'UPointLightComponent'
    D:\UnrealProjects\CPPTriggerLevel\Source\CPPTriggerLevel\LightSwitchOverlapCodeOnly.cpp(17) : error C2027: use of undefined type 'UPointLightComponent'
    d:\unrealprojects\cpptriggerlevel\source\cpptriggerlevel\LightSwitchOverlapCodeOnly.h(17): note: see declaration of 'UPointLightComponent'
    D:\UnrealProjects\CPPTriggerLevel\Source\CPPTriggerLevel\LightSwitchOverlapCodeOnly.cpp(18) : error C2440: '=': cannot convert from 'UPointLightComponent *' to 'USceneComponent *'
    D:\UnrealProjects\CPPTriggerLevel\Source\CPPTriggerLevel\LightSwitchOverlapCodeOnly.cpp(18): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    D:\UnrealProjects\CPPTriggerLevel\Source\CPPTriggerLevel\LightSwitchOverlapCodeOnly.cpp(21) : error C2027: use of undefined type 'UBoxComponent'
    d:\unrealprojects\cpptriggerlevel\source\cpptriggerlevel\LightSwitchOverlapCodeOnly.h(21): note: see declaration of 'UBoxComponent'
    D:\UnrealProjects\CPPTriggerLevel\Source\CPPTriggerLevel\LightSwitchOverlapCodeOnly.cpp(22) : error C2027: use of undefined type 'UBoxComponent'
    d:\unrealprojects\cpptriggerlevel\source\cpptriggerlevel\LightSwitchOverlapCodeOnly.h(21): note: see declaration of 'UBoxComponent'
    D:\UnrealProjects\CPPTriggerLevel\Source\CPPTriggerLevel\LightSwitchOverlapCodeOnly.cpp(23) : error C2027: use of undefined type 'UBoxComponent'
    d:\unrealprojects\cpptriggerlevel\source\cpptriggerlevel\LightSwitchOverlapCodeOnly.h(21): note: see declaration of 'UBoxComponent'
    D:\UnrealProjects\CPPTriggerLevel\Source\CPPTriggerLevel\LightSwitchOverlapCodeOnly.cpp(24) : error C2027: use of undefined type 'UBoxComponent'
    d:\unrealprojects\cpptriggerlevel\source\cpptriggerlevel\LightSwitchOverlapCodeOnly.h(21): note: see declaration of 'UBoxComponent'
    This is my code

    .h code
    Code:
    #pragma once
    
    #include "CoreMinimal.h"
    #include "GameFramework/Actor.h"
    #include "LightSwitchOverlapCodeOnly.generated.h"
    
    UCLASS()
    class CPPTRIGGERLEVEL_API ALightSwitchOverlapCodeOnly : public AActor
    {
        GENERATED_BODY()
    
    public:    
        //Point Light Component
        UPROPERTY(VisibleAnywhere, Category = "Switch Components")
            class UPointLightComponent* PointLight1;
    
        //Box Component
        UPROPERTY(VisibleAnywhere, Category = "Switch Components")
            class UBoxComponent* Box1;
    
        // Sets default values for this actor's properties
        ALightSwitchOverlapCodeOnly();
    
        //Called when something enters the box component
        UFUNCTION()
            void OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
    
        //Called when something leaves the box component
        UFUNCTION()
            void OnOverlapEnd(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
    
        //Toggles the light component's visibility
        UFUNCTION()
            void ToggleLight();
    
        //The desired intensity for the light
        UPROPERTY(VisibleAnywhere, Category = "Switch Variables")
            float DesiredIntensity;
    
    };

    .cpp code
    ​​​​​​​
    Code:
    #include "LightSwitchOverlapCodeOnly.h"
    
    
    // Sets default values
    ALightSwitchOverlapCodeOnly::ALightSwitchOverlapCodeOnly()
    {
         // 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;
    
        DesiredIntensity = 3000.0f;
    
        PointLight1 = CreateDefaultSubobject<UPointLightComponent>(TEXT("PointLight1"));
        PointLight1->Intensity = DesiredIntensity;
        PointLight1->bVisible = true;
        RootComponent = PointLight1;
    
        Box1 = CreateDefaultSubobject<UBoxComponent>(TEXT("Box1"));
        Box1->InitBoxExtent(FVector(10.0f, 20.f, 5.f));
        Box1->SetupAttachment(RootComponent);
        Box1->OnComponentBeginOverlap.AddDynamic(this, &ALightSwitchOverlapCodeOnly::OnOverlapBegin);
        Box1->OnComponentEndOverlap.AddDynamic(this, &ALightSwitchOverlapCodeOnly::OnOverlapEnd);
    
    }
    
    void ALightSwitchOverlapCodeOnly::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
    {
        //GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Green, FString::Printf(TEXT("%s has Overlapped"),OtherActor->GetName()));
    
    }
    
    void OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
    {
    //    GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("I have exited"));
    }
    Can't understand where to look. Please help me figure this out.
    Thanks,
    Regards,
    Soumitra Goswami

    #2
    Before you can use a function from another class you need to include it.


    Eg. In the .h (header) file you do what is known as "Forward declaration" with the "class" keyword before "UPointLightComponent* PointLight1;"

    Forward declaration only give the compiler information about how much memory to allocate but it still has no knowledge of all the functions within the UPointLightComponent class.

    At the top of the .cpp file you should add
    Code:
    #include "Components/PointLightComponent.h"
    to include the PointLightComponent.

    You can use the Unreal API documentation to find the header you need to include for a particular class.

    You are also missing
    Code:
    #include "Components/BoxComponent.h"
    Last edited by GarnerP57; 12-03-2018, 08:15 AM.

    Comment


      #3
      Thanks for the help. My issue has been resolved.. Appreciate the time.

      Comment

      Working...
      X