How to define an atomic variable in Blueprint

I am trying to design a system wherein there will be:

  • one static mesh with a box collider attached
  • multiple objects with a custom blueprint attached (consisting of a sphere collider)

The static mesh will have one property associated with it (say sum which is initialized to 0). As other objects with sphere collider collide with this static mesh, the sum should be updated.
Each object with sphere collider will have some max. value to be added to the sum on collision.
For example, if there are three objects colliding with that object with max. values 10, 23, 54 respectively, then the final sum value of static mesh should be 87.
I feel this sum value should be an atomic class variable so that result is same always.

How to create an atomic variable in the static mesh blueprint?

Unfortunately blueprint doesn’t have anything like that.
If you absolutely need it to be atomic, you’ll have to use std::atomic<> in CPP- you can make getter and setter functions that are blueprint exposed if you don’t like CPP.

The rest of that logic can just be done with the OnHit event and basic addition.

1 Like

Thanks for the response. Yes, this looks like a nice workaround. I was hoping to avoid using C++ as much as possible, but if there are no options in Blueprints then I don’t mind using this C++ solution.

If you really want to avoid C++, I could throw together a solution for an atomic subsystem.
That way you could use atomics without having to actually use C++

2 Likes

That would be awesome!

Here’s that system- try to look past any grammatical errors, It’s late. Do ask any questions you have though.
AtomicSubsystem.h (4.8 KB)

You don’t need a .cpp for it, just the .h file. Unreal is sometimes funky with the generated cpp file though- you may need to create a GameInstanceSubsystem of the same name (AtomicSubsystem) first:


All you need to do to make it yours is to replace YETANOTHERTESTING_API
in
class YETANOTHERTESTING_API UAtomicSubsystem : public UGameInstanceSubsystem
to be your own project’s API name- that’ll be default. The name is just your project name in all caps followed by _API.

Instructions on how to add types to it are at the top of the class. I’ve done my best to keep it simple, but you’ll need to do some copy pasting.

Keep in mind the identifiers are one name to one type per object. Meaning you can’t have an atomic bool “SomeVar” and an atomic int “SomeVar” in the same object. Doing this won’t crash anything, but it will throw an error and fail the get function if you attempt to get a value as a type the identifier isn’t.
You can however override a name. So if you set “SomeVar” to a bool and then set it to an integer, that will work just fine. That bool will be lost to the void, but you can read the integer just fine.
Of course this is all within the context of a single owner. You can have two identical identifiers with different types as long as you aren’t using the same owner.



Actually using the subsystem is really simple. From practically anywhere, add the atomic subsystem node. There’s only one of them- you don’t need to create it or anything.
image

From there, you can add, set, and remove atomics. I’ve only implemented bool float & integer, but it’s very easy to add more.

These are the types of nodes (set/get/remove)


The identifier is the unique name for a property. I strongly recommend making name variables so that you can just use those instead of writing the same identifier every time.
This identifier can be anything you can think of. Ie “BubblegumCounter”.

Equally as important is the Owner. I’ve made it default to self, but it can be any Actor. Just keep in mind that you can’t have duplicate identifiers with the same owner (unless you really know what you’re doing and it makes absolutely perfect sense).

Last things last: return values.
Every get has two return values: a value, and a return value. A bit unintuitive, I know. The value is the value of the variable- ie true/5.0/2. The return value is whether or not it succeeded. Same case with RemoveAtomic.
Get will fail if the identifier doesn’t exist on that owner or it isn’t the type you’re trying to get it as. RemoveAtomic will fail only if it doesn’t exist.

1 Like

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