[Solved] Help with Camera Entity and Scripts

So yeah, I’ve started to dabble in the wonderous world of the Paradox Engine.
I love it so far, especially being component based, however, documentation is terrible at best.
I’m not a fan of using studios since I feel like it limits control of what happens down the line (so my question is based solely on how to fix my issue within the VS IDE):

“How do I add a Script to an Entity?”

or more specifically (if anyone knows the answer to this)

“How do I add a Camera Script to a Camera Entity?”

I’ve searched the forum thick and thin, and only come up shorthanded because most answers are outdated (one of the cons of being in beta stage). The only guides out there, focus primarily on the studio, which I honestly want to avoid.

Something like this should work:

public class StartScript : StartupScript
{
    public override void Start()
    {
        //Find camera, I've only got one so this works. If you've got more 
        //than one, I guess you'll need to name them or something...
        var camera = SceneSystem.SceneInstance.Scene.Entities.First(e => e.Components.ContainsKey(CameraComponent.Key));
        camera.Add(new ScriptComponent
        {
            Scripts = { new CameraScript()}
        });
    }
}

public class CameraScript : AsyncScript
{
    public override async Task Execute()
    {
        //Do something...
    }
}

You will need to add a reference to the StartScript in your scene (and you’ll need a camera, or you can add one in code).

1 Like

[Edit] AlistairClark beat me to it! :stuck_out_tongue:

Welcome to Paradox!

Adding a Script to an Entity from code is actually really simple:

Where MyGreatScript derives from either SyncScript or AsyncScript.

Adding a Script to a Camera Entity is basically the same as any other Entity, however retrieving the Camera Entity is a bit of a hassle:

That’s how I’m currently getting it.

Hope this helped!

1 Like

Wow thanks!
Yeah I was thinking it would be something like this, but I had a hard time figuring out what component class to use. I’m quite sure this won’t be my last post, as I’m having a blast coding with this engine.

[Follow up]

@Hugo_Peters

I noticed that your scene reference for adding the entity to the scene is obsolete.
It works, but isn’t there some other way to do it (correctly)?

^And this is why I’m confused half the time (a lot of help on the forum, but most advice has become obsolete with the release of 1.3)

Having a look at the source helps (or use something like ReSharper to look at the decompiled sources) :slight_smile: .

This is what the obsolete method does:

[Obsolete]
public void AddChild(Entity entity)
{
  this.Entities.Add(entity);
}

So you can use something like:

this.SceneSystem.SceneInstance.Scene.Entities.Add(myEntity);
1 Like

facepalm
I feel stupid when the answer is right in front of me. Had been looking at those lines of code for like half an hour and didn’t catch on. Thanks though for pointing it out! :smile:

Sorry for Necroing this thread, but I had to return to this after some revised code.

I suddenly find myself with a nullReference when trying to add the camerascript.
This is some of my code:

            playerCamera = new Entity
        {
            new CameraComponent
            {
                UseCustomAspectRatio = true,
                AspectRatio = GameRes.X/GameRes.Y,
                FarClipPlane = 100,
                NearClipPlane = 1,
                VerticalFieldOfView = MathUtil.RadiansToDegrees(0.6f),
		UseCustomViewMatrix = true,
                ViewMatrix = Matrix.LookAtLH(new Vector3(2,1,-2), new Vector3(), Vector3.UnitY)
            },
            new TransformComponent
            {
                Position = new Vector3(2,1,-2)
            },
            new ScriptComponent
            {
                Scripts = { new Camera() }
            },
        };

The last part “Scripts = { new Camera() }” utterly fails. Am i doing something wrong here? Semantically it shouldn’t be any different from the answers mentioned above.

[EDIT]
After debugging a little, I found my camerascript was trying to get the character physics component, which I haven’t implemented yet. I’ve tried with something like this:

                new PhysicsComponent
            {
                Elements = { new CharacterElement { Collider = new ColliderShape() { Type = ColliderShapeTypes.Sphere}  } }
            }

But I’m unable to do it like that, since the accessor won’t allow me. Any hints on how to add a character physicscomponent with a sphere collider shape?

So after a using a lot of time looking through the source code, I realize my mistake. Apparently there’s a distinct difference between ColliderShape and ColliderShapes. Using the latter solved my problem:

                new PhysicsComponent
            {
                Elements = { new CharacterElement { ColliderShapes = { new SphereColliderShapeDesc()}} },
            },

This works.
[ProTip for myself] Use a refactoring tool.