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.

Entity.Set<T> Slowdown Monogame FPS

See original GitHub issue

Hello,

i have trouble to find the solutions for the following Problem: If i add this Line of Code my Fps go from 3000 to 60 in Monogame tiles[tile + j].Set<Show>(); I have 3 Systems, the first one Update my Tiles i want to draw if they are inside of my View:

protected override void Update(float elaspedTime, ref Map component)
        {
            ReadOnlySpan<Entity> tiles = mapTiles.GetEntities();
            Camera cameraData = camera.Get<Camera>();
            var tilesizex = 32;
            var tilesizey = 8;
            var XMapZeichnen = new Point((int)cameraData.VisibleArea.X / tilesizex, (int)(cameraData.VisibleArea.X + cameraData.VisibleArea.Width) / tilesizex);
            if (XMapZeichnen.X < 0) XMapZeichnen.X = 0;
            if (XMapZeichnen.Y > component.Size.X) XMapZeichnen.Y = component.Size.X;

            var YMapZeichnen = new Point((int)cameraData.VisibleArea.Y / tilesizey, (int)(cameraData.VisibleArea.Y + cameraData.VisibleArea.Height) / tilesizey);
            if (YMapZeichnen.X < 0) YMapZeichnen.X = 0;
            if (YMapZeichnen.Y > component.Size.Y) YMapZeichnen.Y = component.Size.Y;

            for (int i = YMapZeichnen.X; i < YMapZeichnen.Y; i++)
            {
                var tile = i * component.Size.X;
                for (int j = XMapZeichnen.X; j < XMapZeichnen.Y; j++)
                {
                    tiles[tile + j].Set<Show>();
                }
            }


        }

And my DrawSystem:

 [With(typeof(MapTile), typeof(MapTextureShared), typeof(Show))]
    public sealed class DrawMapSystem : AEntitySystem<float>
    {
        private readonly SpriteBatch _batch;
        private readonly TileProperty[] _tilePropertys;
        private readonly World _world;
    
    
        public DrawMapSystem(SpriteBatch batch, World world)
            : base(world)
        {
            _batch = batch;
            _tilePropertys = world.GetAllComponents<TileProperty>().ToArray();
            _world = world;


        }
             
        protected override void Update(float elaspedTime, in Entity entity)
        {
            ref MapTile mapTile = ref entity.Get<MapTile>();
            ref MapTextureShared mapTextureShared = ref entity.Get<MapTextureShared>();
            _batch.Draw(mapTextureShared.TextureSheet, mapTile.Position, _tilePropertys[mapTile.ID].Source, Color.White);
        }

        protected override void PreUpdate(float state)
        {


            _batch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, _world.GetAllComponents<Camera>()[0].Transform);
        }

        protected override void PostUpdate(float state)
        {
        
            _batch.End();
        }

    }

And one of the Start of the new Update Cycle to do the following:

[With(typeof(Show))]
    public sealed class ShowSystem : AEntitySystem<float>
    {
        public ShowSystem(World world)
          : base(world)
        {


        }

        protected override void Update(float elaspedTime, in Entity entity)
        {
            entity.Remove<Show>();
        }
    }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:38 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
PodeCaradoxcommented, Nov 16, 2020

Nice that will help alot for faster writing, yeah i just changed it to public xD

0reactions
Dorakucommented, Nov 16, 2020

Hey congrats on finishing the jam! Would love to look around your code but I think your repo is private.

To simplify usage, since the new release of Visual Studio last week I have been working on a source generator analyzer to be much more expressive and safe, basically instead of

        private sealed class DefaultEcsSystem : AEntitySystem<float>
        {
            public DefaultEcsSystem(DefaultWorld world, IParallelRunner runner)
                : base(world.GetEntities().With<DefaultSpeed>().With<DefaultPosition>().AsSet(), runner)
            { }

            public DefaultEcsSystem(DefaultWorld world)
                : this(world, null)
            { }

            protected unsafe override void Update(float state, in Entity entities)
            {
                DefaultSpeed speed = entity.Get<DefaultSpeed>();
                ref DefaultPosition position = ref entity.Get<DefaultPosition>();

                position.X += speed.X * state;
                position.Y += speed.Y * state;
            }
        }

You would be able to just declare

        private sealed partial class DefaultEcsGeneratorSystem : AEntitySystem<float>
        {
            [Update]
            private static void Update(float state, DefaultSpeed speed, ref DefaultPosition position)
            {
                position.X += speed.X * state;
                position.Y += speed.Y * state;
            }
        }

and the constructors and update override would be automatically generated for you with the correct EntitySet composition. (I will never finish a game haha)

Read more comments on GitHub >

github_iconTop Results From Across the Web

The use of a fixed time step
So what is the best way to make my engine fast enough to hold a lot of entity (if fixed time step slow...
Read more >
[SOLVED] MonoGame drastically drops FPS at higher ...
When I tried to set my resolution to 800x600 to get a hi-res snapshot, the frame rate dropped tremendously (it could barely reach...
Read more >
My 3 year experience with MonoGame
1- MonoGame is slow: I develop on a 2011 iPhone 4, which quickly shows any performance issues. Simply drawing a full screen blank...
Read more >
Is Updating Sprite/Entity Movement Using Framerate ...
So if frames run longer than that, the simulation is allowed to fall behind real-time, so the gameplay can slow down - hopefully...
Read more >
Lag if update() takes longer than frametime
I've just started using monogame and fiddled around with the update method. I wanted to test what would happen should my update method...
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