CraftStudio Wiki
Advertisement

A script can define how your game will behave and react to the player controls. It can act on events such as button is pressed, the passing of time, etc..

Your first script: "Hello CraftStudio!"[]

We will start with the simplest thing: write something in the debug window.

To begin with, create a new scene named "game" then add an object to it.

Tuto1 1

Our scene with a single object

Then add a component in your object and select "scripted behavior" ("text script"). Click the + / - of the component to associate a script and create a new one on the way. Finally click on the name of the script to open it directly. You should get a page with this script:

function Behavior:Awake()
    
end

function Behavior:Update()
    
end

This is the prototype of every script.

The Awake() function will be called when a game object is initialized with this script. The game objects can be created when a scene is loaded or at the request while the game is running.

We'll add a line between the beginning and the "end" of the function Awake():

function Behavior:Awake()
    print ("hello Crafstudio")
end

It now remains to define your scene as initial scene: open the Admin tab, in "General" click on the "+ / -" next to the "initial scene" and select your scene. Press F5 to run your "game" and enjoy the results! Well, ok, there is not much to see just a black window (the game window .. currently empty) and another window named "Execution Report". This window displays runtime errors and everything we send it with "print ()". Now it will show: "hello Crafstudio." The Update function will be called 60 times per second for each game object using this script. Try to add the following line in the Update function:

function Behavior:Update()
    print ("Our script is running") 
end

If you start your game you will see "hello Crafstudio" on the first line followed by "Our script is running" on the following lines.



Now you know how to add a script in Craftstudio! You just have to learn the basics of Lua and the Craftstudio API and you will be one step closer to realizing the game of your dreams!

Advertisement