Child class method not executing, despite using virtual and override keywords

I have a class(AMATTER), its child class(AWEAPON), and its grandchild class(Asword). The method in the child class isn’t being executed, no matter what!!!

class MY_MOBA_API Asword : public AWEAPON
{
	GENERATED_BODY()

protected:
	virtual void BeginPlay() override;

	virtual void OnOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) override;

	virtual void EndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) override;

	virtual void __operate_on_hit_actor_from_array(AActor* hit_actor) override;
	
};

class MY_MOBA_API AWEAPON : public AMATTER
{
	GENERATED_BODY()

protected:
	virtual void BeginPlay() override;

	virtual void __operate_on_hit_actor_from_array(AActor* hit_actor) override;
	
	virtual void OnOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) override;

	virtual void EndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) override;
};


class MY_MOBA_API AMATTER : public AActor
{
	GENERATED_BODY()

// .........

// METHODS
protected:
	virtual void __operate_on_hit_actor_from_array(AActor* hit_actor);

	void __clear_hit_actor_array() { __hit_actor_array.Empty(); }

	virtual void OnOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

	virtual void EndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

public:
	UFUNCTION(BlueprintCallable)
		virtual void initalize_on_possess(AActor* owning_actor);

	UFUNCTION(BlueprintCallable)
		AActor* get_owner_actor() { return __owner_actor; }

	UFUNCTION(BlueprintCallable)
		UAbilitySystemComponent * get_owner_ability_system_component()
		{ return __owner_ASC; }

};

The above are header files of AMATTER, its child AWEAPON, and its grandchild Asword.

void Asword::BeginPlay()
{
	Super::BeginPlay();

}

void Asword::OnOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	Super::OnOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);

}

void Asword::EndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	Super::EndOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex);

}

void Asword::__operate_on_hit_actor_from_array(AActor* hit_actor)
{

	UE_LOG(
		LogTemp,
		Warning,
		TEXT("Asword::__operate_on_hit_actor_from_array => ")
	);

	Super::__operate_on_hit_actor_from_array(hit_actor);

	UAbilitySystemComponent* hit_actor_ASC_ = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(hit_actor);

	if ( !(
		hit_actor_ASC_ &&
		__owner_ASC
	) ) return;

	if (!hit_actor_effect_class) return;

	FGameplayEffectContextHandle effect_context_handle = __owner_ASC->MakeEffectContext();
	effect_context_handle.AddSourceObject(__owner_actor);

	const FGameplayEffectSpecHandle hit_effect_spec_handle_ = __owner_ASC->MakeOutgoingSpec(
		hit_actor_effect_class,
		1.f,
		effect_context_handle
	);

	__owner_ASC->ApplyGameplayEffectSpecToTarget(
		*hit_effect_spec_handle_.Data.Get(),
		hit_actor_ASC_
	);

}

The above is the implementation of the sword methods.

void AWEAPON::BeginPlay()
{
	Super::BeginPlay();

}

void AWEAPON::__operate_on_hit_actor_from_array(AActor* hit_actor)
{
	Super::__operate_on_hit_actor_from_array(hit_actor);
}

void AWEAPON::OnOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	Super::OnOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);

	if (__hit_actor_array.Contains(OtherActor)) return;

	__hit_actor_array.Add(OtherActor);

	__operate_on_hit_actor_from_array(OtherActor);

}

void AWEAPON::EndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	Super::EndOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex);

}

The above is the implementation of AWEAPON methods.

void AMATTER::__operate_on_hit_actor_from_array(AActor* hit_actor)
{
}

void AMATTER::OnOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
}

void AMATTER::EndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
}

The above is the implementation of AMATTER methods.

Basically, OnOverlap method calls __operate_on_hit_actor_from_array. Asword is the object in the world.

OnOverlap method works. When code reaches __operate_on_hit_actor_from_array, it executes parent and grandparent class, not Asword.

I am unable to execute the
void Asword::__operate_on_hit_actor_from_array(AActor* hit_actor)
method, even if sword is the only object in the world.

If I use a debugger, it executes AWEAPON and AMATTER methods - __operate_on_hit_actor_from_array, not Asword.

What is the error? How to resolve?

I have been learning game dev using unreal engine for over 3 years. And the more I learn game dev, the more I realize how big of a fraud it has been in my life. While I am keen on learning new as long as I do game development, I am just reminded how little I know. I cannot get basic features to work.

Unreal Engine’s visual scripting is what got me into game development. I have built several projects using it. This time I have an idea of a great game and wish to implement those features in C++. I am following the best practices to build it while learning C++.

But to no avail. I am struggling in implementing something so simple. I seem to be needing to use internet, stack overflow and AI code for almost the basics of C++.

I have 2+ years experience as a Software Developer and have quit my full time job because of a toxic work environment. Job market is bleak and I hoped to take a career break for a few months. Thought of engaging in game development.

I am quite strong in Data Structures and Algorithms. I have implemented animations, scripted many features and got them working with blueprints in many projects. I am trying to implement them in C++.

But I cannot get basic things right, speed of development has been too slow, and very hard to debug and test features. So many features that I have planned on are not being implemented.

This can sound immature, but please tell me if I should not do game development with such little experience. Is game development right for me? Or am I just not ready? What does it take?

I may as well quit game development entirely!

If there was an issue with your declarations, compiler would complain about the “override” keyword.

Seems to me that your object is simply not a ASword, but a AWEAPON instead.
Can you do this to check rapidly :

// in class AMATTER
virtual void BeginPlay() override
{
    Super::BeginPlay();

    UE_LOG(LogTemp, Warning, TEXT("Object = %s, class = %s"), *GetFullName(), *GetClass()->GetFullName());
}

Show how you create an Asword instance.

BTW
If you work with a framework (in this case, UE), try to use same convention.
If Unreal uses PascalCase, do the same.
If Unreal uses an underscore for functions related to Blueprints (like SomeEvent_Implementation), do the same
Don’t use double underscores at the beginning of function names. It is assumed that this is reserved for compilers.
It is generally accepted that names written in all capital letters refer to macros.

This is easier not only for the person writing the code, but also for everyone reading it.

This is the solution. Thanks!

I didn’t know how I could make this silly error!

Basically, I had a blueprint sword actor - BP_sword that should inherit from sword.h class, but was inheriting from WEAPON.h instead.

How can I avoid such silly mistakes that takes away so much development time and effort?

Any tips, advice or suggestions in how to be a better “video game programmer”?

Ok, I will keep it in mind. Thanks!

Don’t work on issues alone. We all end up with silly mistakes from time to time that probably would be picked up if someone else reviewed your code.
If you work on a solo project it not so easy to get someone to review your code but if you follow the framework of Unreal like mentioned before it is a lot easier to reach out for help perhaps from someone else working on a solo project and you can help each other out instead of being stuck for days.

I understand. Thanks for the reply.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.