Share You Use Cases

This is the place to share your use cases for Virtual Functions

  • Where have you seen virtual used before?
  • What cases can you imagine?

The example from the lecture was a Gun class with a virtual Shoot function.

So far I have also noticed the virtual used with the player input component other than begin play and tick.

But I can imagine using virtual for using it in cases of repetitive functionality. Like shoot it could be with basic attacks like from game that use a hero base system that uses the same inputs but what the abilities do and are quite different in how they might work.

2 Likes

Abilities is a really good example! You could have a virtual Use function that changes in the different abilities.

5 Likes

Could virtual be useful in an RTS setting, with a building function? Bit like how we had Shoot where hopefully every gun should do this!

An RTS building is a great example for a base class. What sort of virtual functions do you think it would have?

piggy backing off of the RTS building function example - perhaps in a game, buildings might spawn enemies. maybe different building objects would need to be designed to have different create enemy functionality?

You will use a lot of virtual functions when creating skill trees.

I’m totally brand new to coding in general. I decided this would be my first programming language.

So to put an example, maybe in a game like Fortnite we use virtual to call the same emotes from different characters?

I don’t think it would be used in that scenario. More likely, if the different characters implemented the same emote but did each of them in a different way.

2 Likes

Learning C++ requires a lot of revisiting all of the functions and methods and logics. I’m past halfway in the course and I sometimes find myself lost during a challenge. But when I see the instructor’s implementation everything starts to make sense.

By the way, does this make sense? Did you find yourself lost when you first began learning C++?

3 Likes

I think that virtual override are used used in :
BeginPlay and Tick
I think we could use it in making different guns shoot and in health .

So, like zkc08 I see them in the BeginPlay and Tick Declaration.

I couldn’t actually think of any examples at first but from the lecture, and the other examples above I’m drawing the conclusion that using virtual/override ultimately is what allows you to force the code to double-check against its self to make sure that main function doesn’t have any unique instances in code that supersede or alter it, and if they do call them instead. I imagine any system that uses leveling in any way would use this extensively.

I think Virtual overide functions have to get the biggest respect for all programmers for performance. It is able to get used by so many things like abilities, all different equipments, similar and not so similar characters and so on.

For UE4, I have seen the virtual being used for BeginPlay, Tick and InputComponent. For possible use cases, a virtual function can be used for different type of guns, where you would want guns to fire the same way but have different properties depending on what type of gun the player has. Another example can be with melee weapons, where you would want to call the melee attack but again having different properties depending on what that weapon is. And for a third example, it can also be used with enemies as well, where they all inherit an attack function and their current level. The enemies can have different properties on how strong they may attack and also what their current level depending on what kind of enemy they are. Those were the three use cases that I can imagine them being used.

Obviously we’ve used it with Tick and BeginPlay.

I can imagine it being used with a character class to perhaps change animation sets based on weapon types (although with what we’ve done this far it seems like there would be a better way to do this in blueprints, but maybe we can set animation blueprints based for weapon types in engine, then assign them in C++? Or damage type additions/changes if a character has class abilities that add specific damage onto weapons.

We have used the virtual keyword already in the ToonTanks project. In the PawnBase class we defined a virtual HandleDestruction method that we overrode in the PawnTank and PawnTurret classes. This could be a common use case: we have a player controlled character and several AI controlled characters, and they all inherit base behavior from a common base class. An example could be a scroll down shooter like Raiden, where you have several types of enemies: they all shoot and move, but each of them shoots differently or moves differently across the game area.

In a chess game, maybe there would be an ActorBase or PawnBase class as a base class for all of the chess pieces. But then a class for each type of piece, e.g., bishops, rooks, queens, etc. All pieces move so maybe in the base class there is a basic type of movement function, but then this movement function is overridden in the child classes because of the different specific movement that each piece has.
Then again, maybe this would be an overcomplicated approach to making a game like chess?

Perhaps Virtual Override can be used in a game like Dark Souls where enemies are able to use light, strong and parry attacks just like the player inherited from a base class. However, the player is able to do more advanced things like switch weapons/ armour and take a range of stat boosters etc.

Maybe for elemental damage types or in a card-type game, cards of a similar class type.

Hey I just wanted to share that this was a VERY helpful and informative video about Virtual Methods. Loved it, loved how you went out of the box to show us how it worked, loved how informative it was; just overall a great video. You’re a fantastic instructor Sam, thank you.

I would think visual functions would be useful for remembering our last item in our inventory. If we have a health slot with multiple healing items, a weapon slot with multiple weapons, and a grenade slot with multiple grenade types, we could retain what the last selected item of each slot was and be able to swap between each much easier than if we have to assign each inventory item it’s own slot.