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

GetCamera functions bug #75

Open
SamiG42 opened this issue Dec 8, 2023 · 8 comments
Open

GetCamera functions bug #75

SamiG42 opened this issue Dec 8, 2023 · 8 comments
Assignees
Labels
bug Something isn't working p1 Critical, and inhibits core functionality

Comments

@SamiG42
Copy link

SamiG42 commented Dec 8, 2023

Before first frame rendered this code always prints

GetCamera was null
GetCamera('Main') was null
GetFirstCamera was null

Other frames print these

GetCamera was null
GetCamera('Main') was null
using Stride.CommunityToolkit.Engine;
using Stride.Engine;
using Stride.CommunityToolkit.ProceduralModels;
using Stride.Core.Mathematics;

using var game = new Game();

game.Run(start: (Scene rootScene) =>
{
    game.SetupBase3DScene();

    var entity = game.CreatePrimitive(PrimitiveModelType.Capsule);

    entity.Transform.Position = new Vector3(0, 50, 0);
    entity.Add(new Test());
    entity.Scene = rootScene;
});
public class Test : SyncScript
{
    public override void Update()
    {
        if (this.GetCamera() is null)
        {
            Console.WriteLine("GetCamera was null");
        }
        if (this.GetCamera("Main") is null)
        {
            Console.WriteLine("GetCamera('Main') was null");
        }
        if (this.GetFirstCamera() is null)
        {
            Console.WriteLine("GetFirstCamera was null");
        }
    }
}

(Sorry for my english)

@Doprez Doprez added the bug Something isn't working label Dec 12, 2023
@Doprez
Copy link
Collaborator

Doprez commented Dec 12, 2023

GetCamera() and GetCamera("Main") are basically the same as they both look for a Camera with the name Main in the GraphicsCompositors. I dont know why but Stride has an issue with initializing the camera 2-3 frames into a running game which is why the camera is null at first.

public static Entity AddCamera(this Game game, string? entityName = null, Vector3? initialPosition = null, Vector3? initialRotation = null)

Maybe something can be done in the above but for now there is only the workaround of waiting for Stride to initialize the Camera in the compositor.

@SamiG42
Copy link
Author

SamiG42 commented Dec 13, 2023

Same but there is another bug Game.SetupBase3DScene and Game.SetupBase does not name main camera as "Main"

game.AddCamera().AddInteractiveCameraScript();

and

bug fixable by changing line like this

game.AddCamera("Main");

@SamiG42
Copy link
Author

SamiG42 commented Dec 13, 2023

In this line I adding Test script to the entity. Is this correct way? I don't know because no documentation or example for adding script to entity in Stride Community Toolkit
entity.Add(new Test());

game.Run(start: (Scene rootScene) =>
{
    game.SetupBase3DScene();

    var entity = game.CreatePrimitive(PrimitiveModelType.Capsule);

    entity.Transform.Position = new Vector3(0, 50, 0);
    entity.Add(new Test()); //  ---- This Line
    entity.Scene = rootScene;
});

@VaclavElias
Copy link
Collaborator

Ohh, that reminds me, I have to add some simple example how to add a script!!

You can have a look at this example (which I am working on), how I am adding a script called RaycastHandler()

but there might be other ways. Maybe @Doprez can suggest other alternatives.

@VaclavElias
Copy link
Collaborator

@Doprez, when I am adding a default camera in the SetupBase3DScene() I don't name it. Should it be called Main?

@Doprez
Copy link
Collaborator

Doprez commented Dec 14, 2023

If I remember correctly it gets the name from the camera slot in the GraphicsCompositor so naming through AddCamera would not work with that extension, BUT you could use that and then search through Scene entities instead of the GC.
SceneSystem.SceneInstance.RootScene.Entities.First(x => x.Name == "Main"); this may work better since it doesnt require messing with the GraphicsCompositor which is a little complex.

But yes @VaclavElias you might want to name it "Main" in the GraphicsCompositor camera slots otherwise that extension will never work unless the user adds a new slot to the GC named "Main". And it wont hurt to name the Entity itself to either "Main" or "MainCamera".

@SamiG42
Copy link
Author

SamiG42 commented Dec 14, 2023

This

var entity = new Entity("GameManager")
{
new RaycastHandler()
};

compiles like this so this is the right way

var entity = new Entity("GameManager") ;
entity.Add(new RaycastHandler());

@VaclavElias VaclavElias added the p1 Critical, and inhibits core functionality label Feb 18, 2024
@Doprez Doprez self-assigned this Feb 19, 2024
@Doprez
Copy link
Collaborator

Doprez commented Mar 2, 2024

Scene.GetCamera()
Scene.GetCamera(string)
Scene.GetFirstCamera()

Have replaced the old GetGCCamera functions. These ones should work on scene load now.

This also releases 2 Entity extensions for making getting componenst in an Entity a little easier.

Entity.FindEntityRecursive()
Entity.GetComponentInChildren<T>()

These are all using recursion so use sparingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working p1 Critical, and inhibits core functionality
Projects
None yet
Development

No branches or pull requests

3 participants