Same C++/BP script, multiple objects?

Okay, so this is something I’ve been wondering about and haven’t been able to find anything on how to achieve it. The closest thing I’ve found was the “BatteryCollector” example source/video series.

In Unity, you can make a C# script and attach it to any object in the game world. I’m wanting to do something similar in UE4.

As a basic example, lets say I want to increase the players score when they touch object X. In the level I have a cube, sphere, and an imported model (with its own scripts, turned into a BP already). How would I have the same thing happen on all of the objects, without having to do the same code on each? And lets say the BP/C++ script has a public variable for how much to add to the score, and each object adds a different amount. How would I achieve this behavior?

From what I’ve gathered, I would have to edit the BP on every object I want the behavior to happen on, add a public variable, and then have them all call an “AddScore” function. Basically setup the same BP over and over. But that’s such a hassle when theres so many different objects

1 Like

Hi,

you can create a Base Blueprint for this.

Lets say you create a new Blueprint / C++ class “BP_ObjectBaseWithScore” and add there your score function and the score variable.

Now you create a new Blueprint “BP_Cube”, “BP_Sphere”, “BP_MyWhateverObeject”.

On the create new object dialog you have on the bottom a class browser. There you can search for “BP_ObjectBaseWithScore” and create the blueprint with that base. Your “BP_Cube” has now all the variables and functions which are defined in the base bp. Within the variables tab you have a small eye icon and there you can set “Show inherited”. Now you should see everything you need.

Now you can place you specific blueprints in the world und you only have to create the blueprints for your objects but don’t need to rewrite everything, everytime.

Kind Regards

freakxnet

Hi,

I suggest you use Function libraries or macro libraries. Architectural wise in case you have a code that is suppose to share between all objects of a type you have one of the followings options in c++

1 - if the functionality depends on the current instance of the object : use inheritance ( implement it in the base class and drive a class from it then it automatically has it )

2 - if it is not dependent on the current instance ( E.g. Vector operations like dot product ) use Function libraries
Blueprint Function Libraries | Unreal Engine Documentation
( in UE4 you have the option of creating macro libraries too )

If you want Unity-like behavior, you should look into creating Actor Components , which you can then add to any blueprint.

What if the blueprint I need to add to already has that? Can I do this with like 5? So a single BP has 5 "Base Blueprint"s with inherited variables for them all?

I had no idea this existed until you said it. Is it new or am I just an idiot? Hahaha. This looks like what I want, but are there any limitations you know of? I’m about to read that page now, so hopefully it covers that

if you have thousand actors on the scene - create manually thousand blueprints, add components to this thousand times. Does it work that way?)

did you find solution?