BL Coding How to create custom scripts to use in scene editor

Users who are viewing this thread

duck123

Recruit
In the bannerlord scene editor you can add a script to an object. I was wondering how I could make my own script that I could then assign to an object in the scene editor to get a behavior I want. Im mainly looking to find out where I would need to store the script for it to show up in the script drop down list and where I can find the existing scripts to look at as an example? I know this is possible, ive seen it done in a youtube video.
 

duck123

Recruit
I was able to figure out how to do this by piecing together info from various sources. Ive created a crappy youtube video that says how to do it which includes a demo of an example map I made that utilizes a custom script I made. Ive also included the script code:

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaleWorlds.Engine;
using TaleWorlds.Library;

namespace MyMods
{
    public class CS_MoveBackAndForth : ScriptComponentBehavior
    {
        public bool EnabledInGame = false;
        public bool EnabledInEditor = false;
        public float Destination_X1 = 0f;
        public float Destination_Y1 = 0f;
        public float Destination_Z1 = 0f;
        public float Destination_X2 = 0f;
        public float Destination_Y2 = 0f;
        public float Destination_Z2 = 0f;
        public float MoveSpeedX = 0f;
        public float MoveSpeedY = 0f;
        public float MoveSpeedZ = 0f;
        public float SlowAtDist = 0f;
        public float SlowSpeedPercent = 0f;
        protected bool FirstRun = true;
        protected bool GoToDest1 = true;
        protected bool DestXGreater = true;
        protected bool DestYGreater = true;
        protected bool DestZGreater = true;
        protected float sixtyFpsFrameTime = 0.0166f;

        private void Move(float dt)
        {
            float frameTime = dt;

            // We need to compensate for frame differences vs my standard 60 fps. Need to figure out what fps the server uses.
            float movementFrameModifier = frameTime / sixtyFpsFrameTime;

            if (GoToDest1 == true)
            {
                MakeMove(Destination_X1, Destination_Y1, Destination_Z1, movementFrameModifier);
            }
            else
            {
                MakeMove(Destination_X2, Destination_Y2, Destination_Z2, movementFrameModifier);
            }

        }

        private void MakeMove(float Destination_X, float Destination_Y, float Destination_Z, float movementFrameModifier)
        {
            float curPositionX = base.GameEntity.GetFrame().origin.x;
            float curPositionY = base.GameEntity.GetFrame().origin.y;
            float curPositionZ = base.GameEntity.GetFrame().origin.z;
            bool reachedDestination = false;
            float movementX = MoveSpeedX * movementFrameModifier, movementY = MoveSpeedY * movementFrameModifier, movementZ = MoveSpeedZ * movementFrameModifier;

            // Set compare types for first run in current route to destination
            if (FirstRun)
            {
                if (curPositionX >= Destination_X)
                {
                    DestXGreater = false;
                }
                if (curPositionY >= Destination_Y)
                {
                    DestYGreater = false;
                }
                if (curPositionZ >= Destination_Z)
                {
                    DestZGreater = false;
                }
                FirstRun = false;
            }
            /////////////////////////////////////

            // Do compares based on compare types we set to see what direction we move or if we are done

            movementX = DetermineMovement(movementX, curPositionX, Destination_X, DestXGreater);
            movementY = DetermineMovement(movementY, curPositionY, Destination_Y, DestYGreater);
            movementZ = DetermineMovement(movementZ, curPositionZ, Destination_Z, DestZGreater);

            ///////////////////////////////////////////////////

            // Check if we have reached destination
            if (movementX == 0f && movementY == 0f && movementZ == 0f)
            {
                reachedDestination = true;
            }
            //////////////////////////////////////////////

            // If we have reached our destination then setup for going to the other destination and return
            if (reachedDestination == true)
            {
                GoToDest1 = !GoToDest1;
                FirstRun = true;

                DestXGreater = true;
                DestYGreater = true;
                DestZGreater = true;

                return;
            }
            //////////////////////////////////////////////////////////////

            // Do the actual vector move for the entity
            Vec3 vec = new Vec3(movementX, movementY, movementZ, -1f);
            MatrixFrame frame = base.GameEntity.GetFrame();
            frame.origin += vec;
            base.GameEntity.SetFrame(ref frame);
            //////////////////////////////////////////
        }

        private float DetermineMovement(float movement, float curPosition, float destination, bool DestGreater)
        {

            if (DestGreater == true)
            {
                if (destination <= curPosition)
                {
                    movement *= 0f;
                }
                else if (destination <= (curPosition + SlowAtDist))
                {
                    movement *= SlowSpeedPercent;
                }
            }
            else
            {
                movement *= -1f;
                if (destination >= curPosition)
                {
                    movement *= 0f;
                }
                else if (destination >= (curPosition - SlowAtDist))
                {
                    movement *= SlowSpeedPercent;
                }
            }

            return movement;
        }

        protected override void OnInit()
        {
            base.OnInit();
            base.SetScriptComponentToTick(this.GetTickRequirement());
        }

        public override ScriptComponentBehavior.TickRequirement GetTickRequirement()
        {
            return ScriptComponentBehavior.TickRequirement.Tick | base.GetTickRequirement();
        }

        protected override void OnTick(float dt)
        {
            if (EnabledInGame == true)
            {
                this.Move(dt);
            }
        }

        protected override void OnEditorTick(float dt)
        {
            base.OnEditorTick(dt);

            if (EnabledInEditor == true)
            {
                this.Move(dt);
            }
        }

    }
}
 
Last edited:
Upvote 0

NPC99

Baron
M&BWBWF&SVC
I was able to figure out how to do this by piecing together info from various sources. Ive created a crappy youtube video that says how to do it which includes a demo of an example map I made that utilizes a custom script I made:
Thanks for sharing your findings. @Dejan any way this can be shifted to modding tutorials for greater visibility?
 
Last edited:
Upvote 0
Top Bottom