Skip to content
This repository has been archived by the owner on Aug 22, 2018. It is now read-only.

Priority doesn't work for AsyncScript #652

Open
JohnFromPrague opened this issue Feb 23, 2018 · 2 comments
Open

Priority doesn't work for AsyncScript #652

JohnFromPrague opened this issue Feb 23, 2018 · 2 comments

Comments

@JohnFromPrague
Copy link

JohnFromPrague commented Feb 23, 2018

Hello, it looks like Priority property doesn't work for AsyncScript. In my prototype I have two async scripts, one with priority -1 and one with 0. In this case I expects that async script with priority -1 will be executed before that one with priority 0, but it doesn't.

It's possible to easily reproduce this bug by:

  1. create first async script, e.g.:
    ` public class TestScript1 : AsyncScript
    {
    public bool IsInitialized { get; private set; }

     public override async Task Execute()
     {
         this.IsInitialized = true;
    
         while (this.Game.IsRunning)
         {
             await Task.Delay(5000);
             await this.Script.NextFrame();
         }
     }
    

    }`

  2. create second async script, e.g.:
    `public class TestScript2 : AsyncScript
    {
    public bool IsInitialized { get; private set; }

     public override async Task Execute()
     {
         this.IsInitialized = true;
    
         var firstScript =  this.Entity.Get<TestScript1>();
         if (!firstScript.IsInitialized && firstScript.Priority == -1 && this.Priority == 0)
         {
             throw new InvalidOperationException("First script isn't initialized.");
         }
    
         while (this.Game.IsRunning)
         {
             await Task.Delay(5000);
             await this.Script.NextFrame();
         }
     }
    

    }`

  3. attach both scripts to an entity (order probably doesn't matter, but to be safe, first TestScript2, then TestScript1)

  4. set priority -1 for TestScript1 and 0 for TestScript2

  5. run game and see "First script isn't initialized." exception

In documentation there is "Priorities apply to all kinds of scripts. This means that, for example, synchronous and asynchronous scripts don't have separate priority lists. They both join the same queue." so to me this definitely looks like a bug.

@Kryptos-FR
Copy link

Does it have the same behavior if you await once at the beginning of each script:
await Script.NextFrame().
Maybe the priority is only taken into account when the script get scheduled for the next frame, which is still a bug but at least that would be a workaround.

@JohnFromPrague
Copy link
Author

await Script.NextFrame() works as workaround, thx that's much better than what I had

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants