Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7575897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:47:27+00:00 2026-05-30T16:47:27+00:00

I’m new to Reactive Extensions for .NET and while playing with it I thought

  • 0

I’m new to Reactive Extensions for .NET and while playing with it I thought that it would be awesome if it could be used for games instead of the traditional update-render paradigm. Rather than trying to call Update() on all game objects, the objects themselves would just subscribe to the properties and events they are interested in and handle any changes, resulting in fewer updates, better testability and more concise queries.

But as soon as, for example, a property’s value changes, all subscribed queries will also want to update their values immediately. The dependencies may be very complex, and once everything is going to be rendered I don’t know whether all objects have finished updating themselves for the next frame. The dependencies may even be such that some objects are continuously updating based on each other’s changes. Therefore the game might be in an inconsistent state on rendering. For example a complex mesh that moves, where some parts have updated their positions and other have not yet once rendering starts. This would not have been a problem with the traditional update-render loop, as the update phase will finish completely before rendering starts.

So then my question is: is it possible to ensure that the game is in a consistent state (all objects finished their updates) just before rendering everything?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:47:28+00:00Added an answer on May 30, 2026 at 4:47 pm

    The short answer is yes, it is possible to accomplish what you’re looking for regarding game update loop decoupling. I created a proof-of-concept using Rx and XNA which used a single rendering object which was not tied in any way to the game loop. Instead, entities would fire an event off to inform subscribers they were ready for render; the payload of the event data contained all the info needed to render a frame at that time for that object.

    The render request event stream is merged with a timer event stream (just an Observable.Interval timer) to synchronize renders with the frame rate. It seems to work pretty well, and I’m considering testing it out on slightly larger scales. I’ve gotten it to work seemingly well both for batched rendering (many sprites at once) and with individual renders. Note that the version of Rx the code below uses is the one that ships with the WP7 ROM (Mirosoft.Phone.Reactive).

    Assume you have an object similar to this:

    public abstract class SomeEntity
    {
        /* members omitted for brevity */
    
        IList _eventHandlers = new List<object>();
        public void AddHandlerWithSubscription<T, TType>(IObservable<T> observable, 
                                                    Func<TType, Action<T>> handlerSelector)
                                                        where TType: SomeEntity
        {
          var handler = handlerSelector((TType)this);
          observable.Subscribe(observable, eventHandler);
        }
    
        public void AddHandler<T>(Action<T> eventHandler) where T : class
        {
            var subj = Observer.Create(eventHandler);            
            AddHandler(subj);
        }
    
        protected void AddHandler<T>(IObserver<T> handler) where T : class
        {
            if (handler == null)
                return;
    
            _eventHandlers.Add(handler);
        }
    
        /// <summary>
        /// Changes internal rendering state for the object, then raises the Render event 
        ///  informing subscribers that this object needs rendering)
        /// </summary>
        /// <param name="rendering">Rendering parameters</param>
        protected virtual void OnRender(PreRendering rendering)
        {
            var renderArgs = new Rendering
                                 {
                                     SpriteEffects = this.SpriteEffects = rendering.SpriteEffects,
                                     Rotation = this.Rotation = rendering.Rotation.GetValueOrDefault(this.Rotation),
                                     RenderTransform = this.Transform = rendering.RenderTransform.GetValueOrDefault(this.Transform),
                                     Depth = this.DrawOrder = rendering.Depth,
                                     RenderColor = this.Color = rendering.RenderColor,
                                     Position = this.Position,
                                     Texture = this.Texture,
                                     Scale = this.Scale, 
                                     Size = this.DrawSize,
                                     Origin = this.TextureCenter, 
                                     When = rendering.When
                                 };
    
            RaiseEvent(Event.Create(this, renderArgs));
        }
    
        /// <summary>
        /// Extracts a render data object from the internal state of the object
        /// </summary>
        /// <returns>Parameter object representing current internal state pertaining to rendering</returns>
        private PreRendering GetRenderData()
        {
            var args = new PreRendering
                           {
                               Origin = this.TextureCenter,
                               Rotation = this.Rotation,
                               RenderTransform = this.Transform,
                               SpriteEffects = this.SpriteEffects,
                               RenderColor = Color.White,
                               Depth = this.DrawOrder,
                               Size = this.DrawSize,
                               Scale = this.Scale
                           };
            return args;
        }
    

    Notice that this object doesn’t describe anything how to render itself, but only acts as a publisher of data that will be used in rendering. It exposes this by subscribing Actions to observables.

    Given that, we could also have an independent RenderHandler:

    public class RenderHandler : IObserver<IEvent<Rendering>>
    {
        private readonly SpriteBatch _spriteBatch;
        private readonly IList<IEvent<Rendering>> _renderBuffer = new List<IEvent<Rendering>>();
        private Game _game;
    
        public RenderHandler(Game game)
        {
            _game = game;
            this._spriteBatch = new SpriteBatch(game.GraphicsDevice);
        }
    
        public void OnNext(IEvent<Rendering> value)
        {
            _renderBuffer.Add(value);
            if ((value.EventArgs.When.ElapsedGameTime >= _game.TargetElapsedTime))
            {
                OnRender(_renderBuffer);
                _renderBuffer.Clear();
            }
        }
    
        private void OnRender(IEnumerable<IEvent<Rendering>> obj)
        {
            var renderBatches = obj.GroupBy(x => x.EventArgs.Depth)
                .OrderBy(x => x.Key).ToList(); // TODO: profile if.ToList() is needed
            foreach (var renderBatch in renderBatches)
            {
                _spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
    
                foreach (var @event in renderBatch)
                {
                    OnRender(@event.EventArgs);
                }
                _spriteBatch.End();
            }
        }
    
        private void OnRender(Rendering draw)
        {
            _spriteBatch.Draw(
                draw.Texture,
                draw.Position,
                null,
                draw.RenderColor,
                draw.Rotation ?? 0f,
                draw.Origin ?? Vector2.Zero,
                draw.Scale,
                draw.SpriteEffects,
                0);
        }
    

    Note the overloaded OnRender methods which do the batching and drawing of the Rendering event data (it’s more of a message, but no need to get too semantic!)

    Hooking up the render behavior in the game class is simply two lines of code:

    entity.AddHandlerWithSubscription<FrameTicked, TexturedEntity>(
                                          _drawTimer.Select(y => new FrameTicked(y)), 
                                          x => x.RaiseEvent);
    entity.AddHandler<IEvent<Rendering>>(_renderHandler.OnNext);
    

    One last thing to do before the entity will actually render is to hook up a timer that will serve as a synchronization beacon for the game’s various entities. This is what I think of as the Rx equivalent of a lighthouse pulsing every 1/30s (for default 30Hz WP7 refresh rate).

    In your game class:

    private readonly ISubject<GameTime> _drawTimer = 
                                             new BehaviorSubject<GameTime>(new GameTime());
    
    // ... //
    
    public override Draw(GameTime gameTime)
    {
        _drawTimer.OnNext(gameTime);
    }
    

    Now, using the Game‘s Draw method may seemingly defeat the purpose, so if you would rather avoid doing that, you could instead Publish a ConnectedObservable (Hot observable) like this:

    IConnectableObservable<FrameTick> _drawTimer = Observable
                                                    .Interval(TargetElapsedTime)
                                                    .Publish();
    //...//
    
    _drawTimer.Connect();
    

    Where this technique can be incredibly useful is in Silverlight-hosted XNA games. In SL, the Game object is unavailable, and a developer needs to do some finagling in order to get the traditional game loop working correctly. With Rx and this approach, there is no need to do that, promising a much less disruptive experience in porting games from pure XNA to XNA+SL

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.