Write your first script
This tutorial writes a C# script, attaches it to a GameObject, and turns it on in play mode. It uses only the API that ships in Kolpa.Engine.
1. Create the script file
Section titled “1. Create the script file”- In your project root, open
bindings/Kolpa.Demo/. - Copy
NewBehaviour.cs.templateand save it asSpinner.cs. - Rename the class to match the file:
public class Spinner : MonoBehaviour.
Your file should start like this:
using Kolpa.Engine;
namespace Kolpa.Demo;
public class Spinner : MonoBehaviour{ public float DegreesPerSecond = 90f;
public override void OnUpdate(float deltaTime) { transform.Rotate(0f, DegreesPerSecond * deltaTime, 0f); }}OnUpdate runs every frame. deltaTime is the frame time in seconds, so the rotation speed does not depend on frame rate.
2. Build the assembly
Section titled “2. Build the assembly”Build the project so Kolpa.Demo.dll includes the new class. See the engine README.md for the build command.
3. Attach it to a GameObject
Section titled “3. Attach it to a GameObject”- In the editor, select the GameObject you want to spin.
- Add Component -> Script.
- In the Behaviour field, type
Kolpa.Demo.Spinner, Kolpa.Demo. - File -> Save Scene (
Ctrl+S).
The type name is assembly-qualified: Namespace.Class, AssemblyName.
4. Play
Section titled “4. Play”- Press Play.
- The object spins at the default 90 degrees per second.
- Stop play.
5. Change the speed
Section titled “5. Change the speed”Open Spinner.cs and change DegreesPerSecond to 180f. Rebuild, then use Debug -> Reload Assets to load the new assembly without restarting. Play again and the object spins faster.
Note: Hot reload is unavailable in a NativeAOT release build. Scripts there are compiled into the binary.
Continue reading
Section titled “Continue reading”Previous: Scene settings and sky: scene settings.
Next: Add physics: a dynamic body and a push.
