# When to use \<transacts\> specifier?

**URL:** <https://forums.unrealengine.com/t/when-to-use-transacts-specifier/1155825>\
**Category:** Verse\
**Tags:** unreal-editor-for-fortnite, fortnite-creative, question, fortnite\
**Created:** [May 5, 2023, 8:36pm UTC](https://forums.unrealengine.com/t/when-to-use-transacts-specifier/1155825 "2023-05-05T20:36:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![poltrr](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/poltrr/32/677016_2.png) [@poltrr](https://forums.unrealengine.com/u/poltrr)\
**Post date:** [May 5, 2023, 8:36pm UTC](https://forums.unrealengine.com/t/when-to-use-transacts-specifier/1155825/1 "2023-05-05T20:36:14Z")

</div>

Hi,

I have a problem understanding when the specifier should be used. How do I know if the behavior inside a function can be rolled back or not?

For example, according to the docs:  
`The transacts effect is required any time a mutable variable (var) is written.`

Does that mean it’s required when,  
a. a mutable variable is **declared** inside the function?  
b. any mutable variable is **mutated (set)** from inside the function? (even if it’s declared outside the function scope)

Overall I’m just quite confused 😔

---

<div class="post-metadata">

**Author:** ![CosmicDanAU](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/cosmicdanau/32/642728_2.png) [@CosmicDanAU](https://forums.unrealengine.com/u/CosmicDanAU)\
**Post date:** [May 6, 2023, 2:00am UTC](https://forums.unrealengine.com/t/when-to-use-transacts-specifier/1155825/2 "2023-05-06T02:00:58Z")

</div>

Basically, the transacts effect is needed whenever you want to update a class variable. WIthout this effect, the updated value in the var will only be visible within the current scope.

Consider this example:

```auto
My_Device := class(creative_device):
    var CurrentFrame : int = -1
    
    OnBegin<override>()<suspends>:void=
        loop:
            set CurrentFrame += 1
            Sleep(0.0)

    GetCurrentFrame():int=
        return CurrentFrame

```

If you were to use this code, and call GetCurrentFrame() from some other place at a later time, it will always be -1. Or maybe it will be different but it won’t be accurate/reliable (unsure, not tested). This can be solved with a “Setter method” which does that transaction for you. Since a lot of other API calls can’t be used under the `<transacts>` effect, Setter methods are something I use a lot.

Fixed version of the above example:

```auto
My_Device := class(creative_device):
    var CurrentFrame : int = -1
    TickFrameCounter()<transacts>:void = { set CurrentFrame += 1 }
    
    OnBegin<override>()<suspends>:void=
        loop:
            TickFrameCounter()
            Sleep(0.0) # wait for next server tick

    GetCurrentFrame():int=
        return CurrentFrame

```

Hope that makes sense!

Note that this is just a super basic example. Using a “setter method” is not always ideal, it’s kind of old-school - but in cases like this it might make sense because you just want to get the current frame number from all kinds of places but with this design you only need one counter.

So in summary, transacts effect is for making sure a class variable update is visible in every scope. If you’re wondering why this is even a thing, my educated guess is because that “making it visible in every scope” part is relatively expensive - so Verse requires us to specify it explicitly for performance reasons. So, as a final tip, try to use as little transacts functions as possible (i.e. combine as much code as possible into one transacts function, but try to keep those transacts functions as concise as possible). Probably not a huge deal unless you’re doing many transactions per frame though.

---

<div class="post-metadata">

**Author:** ![im\_a\_lama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/im_a_lama/32/995063_2.png) [@im\_a\_lama](https://forums.unrealengine.com/u/im_a_lama)\
**Post date:** [May 14, 2023, 9:41pm UTC](https://forums.unrealengine.com/t/when-to-use-transacts-specifier/1155825/4 "2023-05-14T21:41:24Z")

</div>

Are you sure about all of this ? I’m never using `<transacts>` and I still get to update tens of variables in seperated scopes even from `<suspends>` functions.

Also, you didn’t talk about rollback, for me the `<transacts>` allows you to rollback all the edits you’ve made if your function were ever gonna fail, and that’s why all your inner calls need to `<transacts>` too.

However, it is said in [the doc](https://dev.epicgames.com/documentation/en-us/uefn/specifiers-and-attributes-in-verse) that

> The transacts effect is required any time a mutable variable (`var`) is written

So I wanna believe you, but go ahead and try that :

```auto
var Test:int = 0

PrintTestVal()<suspends>:void=
    Print("{Test}")

OnBegin<override>()<suspends> : void =
    set Test = 1
    spawn{PrintTestVal()}

```

Scopes are different, we even spawned a thread, still prints 1 though…

---

<div class="post-metadata">

**Author:** ![CosmicDanAU](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/cosmicdanau/32/642728_2.png) [@CosmicDanAU](https://forums.unrealengine.com/u/CosmicDanAU)\
**Post date:** [May 16, 2023, 2:27am UTC](https://forums.unrealengine.com/t/when-to-use-transacts-specifier/1155825/6 "2023-05-16T02:27:11Z")

</div>

Yeah “scope” is the wrong term, apologies. What I meant was “entry-point”. If I recall correctly you’ll need it for variable synchronization in examples like:  
a) Having a `loop` in `OnBegin` that waits forever until a game Event sets some `logic` to `true`;  
b) Referencing another creative\_prop in your code, and trying to get values from it during runtime;

Something like that. I’ve just taken to the habit of it, because it _has_ been an issue for me.

As for `no_rollback`, thank you for sharing that concise tidbit - I’m still getting to grips with the speculative execution stuff. What you said made a LOT of sense to me though, so perhaps my understanding of `<transacts>` is not quite correct after all.

---

<div class="post-metadata">

**Author:** ![Grandma\_Bertha](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Grandma\_Bertha](https://forums.unrealengine.com/u/Grandma_Bertha)\
**Post date:** [June 6, 2023, 10:25am UTC](https://forums.unrealengine.com/t/when-to-use-transacts-specifier/1155825/7 "2023-06-06T10:25:20Z")

</div>

Did you guys figure it out? Is transacts necessary any time a mutable data is written or only when you need the mutable data to avoid race conditions?

---

<div class="post-metadata">

**Author:** ![Ep8Script](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ep8script/32/1170494_2.png) [@Ep8Script](https://forums.unrealengine.com/u/Ep8Script)\
**Post date:** [June 6, 2023, 3:51pm UTC](https://forums.unrealengine.com/t/when-to-use-transacts-specifier/1155825/8 "2023-06-06T15:51:06Z")

</div>

It’s only when you need a method to roll back - accessing and setting variables in methods without `<transacts>` like this works just fine:

```auto
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }

my_device := class(creative_device):

    @editable
    Button:button_device = button_device{}

    var Pressed:logic = false

    OnBegin<override>()<suspends>:void=
        Button.InteractedWithEvent.Subscribe(OnButtonPressed)
        loop:
            Sleep(0.0)
            if (Pressed?):
                Print("Pressed!")
                break

    OnButtonPressed(Agent:agent):void=
        set Pressed = true

```

---

<div class="post-metadata">

**Author:** ![ManualPotato](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/manualpotato/32/1144902_2.png) [@ManualPotato](https://forums.unrealengine.com/u/ManualPotato)\
**Post date:** [June 7, 2023, 4:25pm UTC](https://forums.unrealengine.com/t/when-to-use-transacts-specifier/1155825/9 "2023-06-07T16:25:59Z")

</div>

I had an issue which I thought might be caused by this. It turned out it wasn’t, instead, it was because I was setting a variable on a referenced device before its `OnBegin`. Even though I wasn’t doing anything in the `OnBegin` of the class, I believe the class was setting its variables to initial values at this time after I’d set them from another device.

While I was trying to replicate in an isolated example I created the code below, which does appear to show that `transacts` is not needed to change variables on referenced devices or have mutable variables in functions.

 ![Capture](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/c/d/ecd26579ec4460a351e6c175d20325821cde4f28.png)

Output:

`RefDevice.RefDeviceVariable: 1.00000000`  
`RefDevice.RefDeviceVariable: 1.00000000`
