question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEATURE] Allow listening to AtomReference even with Value or Const usage

See original GitHub issue

Is your feature request related to a problem? Please describe.

The choice between using an AtomReference and AtomEventReference is always a bit difficult.

Usually it’s something like

Reference EventReference
Reading Has a garanteed value Doesn’t have a garanteed value (but can replay previous one)
Reacting Can be listened to through GetEvent/GetOrCreateEvent (except on Value/Const usage) Made to be listened to

I’ve started to think of Variables simply as Events with base values. No data can guarantee that it will only has either the “reading a value when you need it” use-case or the “waiting for a value to change” use-case.

It can simply state “do I have a default value or not”.

But right now if I want an reference that guarantees a default value AND can be listened to, I have a problem.

(current example, I want to set fmod parameter using an AtomReference. I need to update the parameter when it changes, but I also want to read that value when a new audio event starts)

Describe the solution you’d like

If this approach makes sense to you, then you should be able to get an event for any type of variable.

In the case of a “Value” usage, then it should do two things:

  • replay its value when subscribed to
  • if the user modified the value from the inspector, emit a value

The API would be harmonized, simply implementing IGetOrCreateEvent for AtomReference

This would cover the “gap” where you need to both read a variable and react to it.

Describe alternatives you’ve considered

    static E GetFakeEvent<T, E>(T replayValue)
      where E : AtomEvent<T>
    {
      E output = ScriptableObject.CreateInstance<E>();
      // Add replay value to buffer
      output.Raise(replayValue);
      return output;
    }
    
    public static E1 GetOrCreateEvent<T, P, C, TV, E1, E2, F, TVi>(this AtomReference<T, P, C, TV, E1, E2, F, TVi> reference)
      where P : struct, IPair<T>
      where C : AtomBaseVariable<T>
      where TV : AtomVariable<T, P, E1, E2, F>
      where E1 : AtomEvent<T>
      where E2 : AtomEvent<P>
      where F : AtomFunction<T, T>
      where TVi : AtomVariableInstancer<TV, P, T, E1, E2, F>
    {
      switch (reference.Usage)
      {
        case AtomReferenceUsage.VALUE :
        case AtomReferenceUsage.CONSTANT : return GetFakeEvent<T, E1>(reference.Value);
        default: return reference.GetEvent<E1>();
      }
    }

This does everything except the inspector stuff, and actually implementing the IGetOrCreateEvent interface.

This could only be done by extending or modifying AtomBaseReference (for example with an OnValidate function).

Additional context

Big problem with this idea, is if people expect that directly setting the value will trigger those events.

I’m not exactly sure if it shouldn’t trigger them. It could be a lightweight alternative to instancers, but I don’t feel comfortable with it.

We could have warning logs if someone modifies those values at runtime after fake events have been created, but that’s obviously simply a band-aid.

Issue Analytics

  • State:open
  • Created 7 months ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
AdamRambergcommented, Jul 20, 2023

If we go with TryGetEvent then I don’t see a reason to not have TryGetOrCreateEvent. GetOrCreateEvent is already implemented by underlying variable or variable instancer.

Postponing this change to v4.5.0 since it is an addition to the API and hence should be in a minor update.

0reactions
lgarczyncommented, Apr 29, 2023

My Idea would be, that it will just use the IGetEvent interface if applicable

In that case, TryGetOrCreateEvent would be nice too

Read more comments on GitHub >

github_iconTop Results From Across the Web

std::atomic passed as a const reference to a non-atomic type
I wouldn't expect to be able to pass an std::atomic value into a function expecting a const reference to a non-atomic type.
Read more >
Guide to AtomicReference in Java | Tech Wrench
Reading or writing to a volatile variable guarantees that the variable value is written to and read from the main memory and not...
Read more >
In C/C++, should I use 'const' in parameters and local ...
Yes, you should use const whenever possible. It makes a contract that your code will not change something. Remember, a non-const variable can ......
Read more >
shared_ptr actually means you don't understand the problem
We use unique_ptr to hold objects a lot; we never pass a unique_ptr by reference or const-ref to some other function, unless it's...
Read more >
Java concurrency — fixing races with AtomicReference
This article describes an approach to using AtomicReference that can resolve race conditions.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found