x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How to call a function in a TsubclassOf

Header:

  UPROPERTY(EditDefaultsOnly, Category = Weapon)
             TSubclassOf<class ABaseWeapon> PrimaryWeapon;

cpp:

 void UCharacterClassManager::Fire()
 {
     switch (WeaponSlotEnum)
     {
     case EWeaponSlotEnum::WS_Primary:
         
         PrimaryWeapon->Fire();
 
         break;
     case EWeaponSlotEnum::WS_Secondary:
         break;
     default:
         break;
     }
 }


essentially what my current system is, is I have a manager that's supposed to manage primary, secondary and grenades. All this class is supposed to do be able to switch out weapons on the fly and call reload, fire, switch (between primary and secondary) I use the tsubclass so have a base weapon (which is a separate file), that contains actions like the reload and fire, so that the manager says "I want to fire this weapon" and the base weapon class has the firing logic.

Problem is I can't call the firing logic, it instead gives me the error message that "class UClass has no member Fire", but it has it.

 void ABaseWeapon::Fire()
 {
 
 }
 
Product Version: UE 4.20
Tags:
more ▼

asked Oct 24 '18 at 12:50 PM in C++ Programming

avatar image

PasteteDoeniel
31 5 6

avatar image mightyenigma Oct 25 '18 at 05:15 PM

Difference between an object and an Actor is that Actors exist in the world and are spawned not just constructed

(comments are locked)
10|2000 characters needed characters left

3 answers: sort voted first

Hi PasteteDoeniel,

Just as the error says, UClass does not have a member function called Fire. Your class ABaseWeapon does, though.

TSubclassOf<...> is a wrapper around a type to allow it to be passed around with both compile-time and runtime safety. As Ali Akbar and Shadowriver have said, if you want to call a method on your weapon then you are going to need some kind of instance to call the method on. The most common approach to doing this is to, as you stated in a comment, spawn the weapon in the world yourself and attach it to a bone on your character.

If you would like a good example of an existing system that has multiple weapons, you can take a look at the ShooterGame project that comes with the engine.

more ▼

answered Oct 25 '18 at 04:26 PM

DarkwindRichard (suspended)
avatar image the_batch Oct 25 '18 at 05:01 PM

Your class ABaseWeapon does, though.

Exactly! And, PasteteDoeniel, since your individual weapons all probably derive from your ABaseWeapon class (hence its name), then no matter which specific weapon the player is using, it is guaranteed to have a Fire function.

Another way you can ensure that your weapons have the required functions is to use interfaces.

But, in either case, as others have said, you still have to have an instance to call those functions on!

avatar image PasteteDoeniel Oct 25 '18 at 05:29 PM

I'm gonna have a look at these interfaces. sounds interesting and helpful.

avatar image PasteteDoeniel Oct 25 '18 at 05:28 PM

ok I'm gonna have a look at these things. I might have a look at the ShooterGame project (but I'll try to avoid it, because I want to figure it out on my own through learning by doing.

avatar image Shadowriver Oct 25 '18 at 06:25 PM

TSubclassOf type main function is to limit selection of class to specific subclass so you are 100% sure the class you getting is related to that class (but it still can be nullptr), it also limits selection in the property editor

avatar image the_batch Oct 25 '18 at 08:06 PM

Small correction: TSubclassOf variables cannot be nullptr because they are value types, not pointer (reference) types. But they can be null, which was your meaning, I think. So, yeah!

avatar image DarkwindRichard Oct 25 '18 at 08:31 PM

Yes, as I said using TSubclassOf gives you guaranteed type safety at both compile- and runtime. It is a wrapper for UClass, and assigning it a value is limited in the editor and checked at runtime by inspecting the type hierarchy. My point for clarifying what TSubclassOf was, though, was to give an explanation for PasteteDoeniel's confusion regarding the class UClass has no member Fire error message.

(comments are locked)
10|2000 characters needed characters left

Your PrimaryWeapon variable doesn't hold the reference to the weapon actor spawned in the world, but instead holds the class type

Change from this:

 UPROPERTY(EditDefaultsOnly, Category = Weapon)
 TSubclassOf<class ABaseWeapon> PrimaryWeapon;


to this:

 UPROPERTY(EditDefaultsOnly, Category = Weapon)
 class ABaseWeapon* PrimaryWeapon;

and save it like so:

 PrimaryWeapon = GetWorld()->Spawn<ARifle>(...)



more ▼

answered Oct 24 '18 at 01:11 PM

avatar image

Ali Akbar
681 2 5 8

avatar image PasteteDoeniel Oct 24 '18 at 01:43 PM

I don't want to spawn it in the world though. I just want it to hold the necessary information. in this case, the firing pattern, ammunition, etc. So that the manager can access that information or functions. Pretty much like this: charater (calls fire in the manager) manager (calls fire of the weapon) weapon (fires) I want to do it like this so i can add a lot of weapons in a short amount of time and keep it as modular as possible.

In the end i want to have a system like in a multiplayer game, where you can choose what primary, secondary you want to have.

avatar image Shadowriver Oct 25 '18 at 04:04 PM

But you need to spawn it to create data for it, you might also consider making actor component maybe

avatar image PasteteDoeniel Oct 25 '18 at 04:26 PM

yeah. seems like best way is to spawn it in the world and attach it to the character. Do you mean the weapon itself should be a actor component?

avatar image Shadowriver Oct 25 '18 at 04:30 PM

No, your weapon manager, you could also spawn and just hold the pointer no need to attach it since it's not physical at all. But imo you could good as well implement Fire function and weapon managment in actor it self.

avatar image PasteteDoeniel Oct 25 '18 at 04:54 PM

oh. well the weapon manager is a actor component attached to the character.

(comments are locked)
10|2000 characters needed characters left

Did you try to call PrimaryWeapon->GetDefaultObject()->Fire() Would be great to hear if it worked :)

more ▼

answered Oct 25 '18 at 12:25 AM

avatar image

Ninjin
4.1k 88 38 151

avatar image PasteteDoeniel Oct 25 '18 at 07:25 AM

nope, that did not work. I think I found a solution, at least theoretically. I'm just gonna spawn the weapon in the world and attach it to the character.

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question