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

**URL:** <https://forums.unrealengine.com/t/child-class-method-not-executing-despite-using-virtual-and-override-keywords/1621909>\
**Category:** C++\
**Tags:** inheritance, unreal-engine, question, CPP\
**Created:** [January 17, 2024, 5:27pm UTC](https://forums.unrealengine.com/t/child-class-method-not-executing-despite-using-virtual-and-override-keywords/1621909 "2024-01-17T17:27:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bennur\_1206](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bennur\_1206](https://forums.unrealengine.com/u/bennur_1206)\
**Post date:** [January 17, 2024, 5:27pm UTC](https://forums.unrealengine.com/t/child-class-method-not-executing-despite-using-virtual-and-override-keywords/1621909/1 "2024-01-17T17:27:06Z")

</div>

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!!!

```auto
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.

```auto
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.

```auto
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.

```auto
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?

---

<div class="post-metadata">

**Author:** ![bennur\_1206](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bennur\_1206](https://forums.unrealengine.com/u/bennur_1206)\
**Post date:** [January 18, 2024, 5:57am UTC](https://forums.unrealengine.com/t/child-class-method-not-executing-despite-using-virtual-and-override-keywords/1621909/2 "2024-01-18T05:57:55Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Chatouille](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chatouille/32/1194622_2.png) [@Chatouille](https://forums.unrealengine.com/u/Chatouille)\
**Post date:** [January 18, 2024, 1:53pm UTC](https://forums.unrealengine.com/t/child-class-method-not-executing-despite-using-virtual-and-override-keywords/1621909/3 "2024-01-18T13:53:49Z")

</div>

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 :

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

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

```

---

<div class="post-metadata">

**Author:** ![Emaer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/emaer/32/570877_2.png) [@Emaer](https://forums.unrealengine.com/u/Emaer)\
**Post date:** [January 21, 2024, 11:44pm UTC](https://forums.unrealengine.com/t/child-class-method-not-executing-despite-using-virtual-and-override-keywords/1621909/4 "2024-01-21T23:44:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bennur\_1206](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bennur\_1206](https://forums.unrealengine.com/u/bennur_1206)\
**Post date:** [January 26, 2024, 7:26pm UTC](https://forums.unrealengine.com/t/child-class-method-not-executing-despite-using-virtual-and-override-keywords/1621909/5 "2024-01-26T19:26:51Z")

</div>

> [@Chatouille](#):
>
> Seems to me that your object is simply not a ASword, but a AWEAPON instead.

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”?

---

<div class="post-metadata">

**Author:** ![bennur\_1206](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bennur\_1206](https://forums.unrealengine.com/u/bennur_1206)\
**Post date:** [January 26, 2024, 7:27pm UTC](https://forums.unrealengine.com/t/child-class-method-not-executing-despite-using-virtual-and-override-keywords/1621909/6 "2024-01-26T19:27:26Z")

</div>

Ok, I will keep it in mind. Thanks!

---

<div class="post-metadata">

**Author:** ![GarnerP57](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GarnerP57](https://forums.unrealengine.com/u/GarnerP57)\
**Post date:** [January 27, 2024, 12:40pm UTC](https://forums.unrealengine.com/t/child-class-method-not-executing-despite-using-virtual-and-override-keywords/1621909/7 "2024-01-27T12:40:57Z")

</div>

> [@bennur\_1206](#):
>
> Any tips, advice or suggestions in how to be a better “video game programmer”?

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.

---

<div class="post-metadata">

**Author:** ![bennur\_1206](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bennur\_1206](https://forums.unrealengine.com/u/bennur_1206)\
**Post date:** [January 28, 2024, 11:40am UTC](https://forums.unrealengine.com/t/child-class-method-not-executing-despite-using-virtual-and-override-keywords/1621909/9 "2024-01-28T11:40:12Z")

</div>

I understand. Thanks for the reply.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [February 27, 2024, 11:40am UTC](https://forums.unrealengine.com/t/child-class-method-not-executing-despite-using-virtual-and-override-keywords/1621909/10 "2024-02-27T11:40:24Z")

</div>

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