Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add possibility to add ScriptComponent before running game #2230

Merged
merged 1 commit into from
Apr 21, 2024

Conversation

Jklawreszuk
Copy link
Collaborator

PR Details

Description

This PR allows to add scripts using code approach. Before, following example will throw an exception:

using Stride.Engine;

using var game = new Game();
game.Script.Add(new FooExampleScript()); // SyncScript logic doesnt matter
/*
Unhandled exception. Stride.Core.ServiceNotFoundException: Service [IContentManager] not found
   at Stride.Core.ServiceRegistryExtensions.GetSafeServiceAs[T](IServiceRegistry registry) in stride/sources/core/Stride.Core/ServiceRegistryExtensions.cs:line 23
   at Stride.Engine.ScriptComponent.Initialize(IServiceRegistry registry) in stride/sources/engine/Stride.Engine/Engine/ScriptComponent.cs:line 59
   at Stride.Engine.Processors.ScriptSystem.Add(ScriptComponent script) in stride/sources/engine/Stride.Engine/Engine/Processors/ScriptSystem.cs:line 164
   at Program.<Main>$(String[] args) in Program.cs:line 5
*/
game.Run();

This was fixed by registering Systems earlier

Motivation and Context

This way engine is more flexible and supports code only approach

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My change requires a change to the documentation.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have built and run the editor to try this change out.

@Eideren
Copy link
Collaborator

Eideren commented Apr 21, 2024

I think it should still work fine, but have you tested inputs under winforms/SDL ?
What's up with the EffectSystem changes ?

@Jklawreszuk
Copy link
Collaborator Author

@Eideren When It comes to Input and EffectSystem nothing has changed, I tested JumpyJet and CustomShader sample and code only approach.

Here is example script I made to CustomShader project before running the Game:

public class FooScript : SyncScript
{
    private InputManager _InputService;
    private EffectSystem _effectSystem;

    public override void Start()
    {
        base.Start();
        
        _InputService = Services.GetService<InputManager>();
        _effectSystem = Services.GetService<EffectSystem>();
        
        // both values are not null as exptected
        if(_InputService == null || _effectSystem == null)
            throw new Exception("Should not fail");

        var es = _effectSystem.LoadEffect("Effect", new()).WaitForResult() ?? throw new Exception("Should not fail");
        Console.WriteLine("Effect system loaded effect correctly");
    }
    public override void Update()
    {
        //InputManager 
        if(Input.IsKeyReleased(Stride.Input.Keys.Space))
        {
            Console.WriteLine("PRESED JUMP KEY !");
        }
    }
}

Everything worked fine 😊

@Eideren Eideren merged commit 3a9ed13 into stride3d:master Apr 21, 2024
12 checks passed
@Eideren
Copy link
Collaborator

Eideren commented Apr 21, 2024

Thanks !

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

Successfully merging this pull request may close these issues.

None yet

2 participants