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 can I allow a method to be overridden in both blueprints and C++?

I have a class defined in C++. It has a method:

 UFUNCTION(BlueprintNativeEvent)
 void DoAThing();

This is great if I want to override it in blueprints. It's not so great if I want to override it in C++. When I try to add virtual:

 UFUNCTION(BlueprintNativeEvent)
 virtual void DoAThing();

I get a compile error:

 BlueprintNativeEvent functions must be non-virtual

I'm trying to do the same thing with "pure virtual" (i.e. using PURE_VIRTUAL) functions using BlueprintImplementableEvent, and I'm getting this error with that:

 BlueprintImplementableEvents should not be virtual. Use BlueprintNativeEvent instead.

...except, as we saw, that doesn't work, either. How can I get Unreal to let me override a function in both C++ and blueprints?

I'd like to do this because the DoAThing() implementations might get quite expensive, both in terms of performance and maintenance, and I'd like to be able to move code from C++ to blueprints. I also have team members who prefer using C++ to blueprints, and would like to accommodate them as much as possible.

Product Version: UE 4.16
Tags:
more ▼

asked Jun 20 '17 at 11:51 PM in C++ Programming

avatar image

nic-hartley
28 1 3 6

avatar image IslamCorrM Jun 22 '17 at 10:31 PM

you can explain why you need to make event is virtual

avatar image nic-hartley Jun 23 '17 at 07:01 PM

You can explain what you meant, since I'm having trouble understanding your comment.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

BlueprintNativeEvent is correct. The method you define is the stub, there is then an implementation method, which is virtual. I don't remember if it gets auto declared, but anyway you can do so yourself: virtual void DoAThing_Implementation(); You can provide the implementation in that class, and/or implement it in derived C++ classes, and/or in blueprints.

more ▼

answered Jun 22 '17 at 10:06 PM

avatar image

kamrann
2.1k 83 33 119

avatar image nic-hartley Jun 23 '17 at 07:02 PM

Oh, cool! So, to clarify, I shouldn't use BlueprintImplementableEvent at all? Even if there's no sensible native implementation? Should I just add a throw SomeException("you called a method that should never be called"); line to the DoAThing_Implementation method in the base class?

avatar image kamrann Jun 23 '17 at 07:45 PM

Use BlueprintImplementableEvent if you just want an optional blueprint-only notify, which you don't care if someone doesn't bother implementing.

Otherwise, yeah BlueprintNativeEvent is more flexible, and if it's mandatory that it gets implemented at some level, then yeah you want some check. UE4 generally doesn't use exceptions, and you don't want to crash the editor whenever someone forgets to implement something. So put something like ensureMsg(false, ...) which will break if you're debugging, and also print an error to the output log.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
more ▼

answered Jun 23 '17 at 11:38 PM

avatar image

Stratego99
41 1 5 5

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
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