Visual programming environment for all programming languages.

"Create Branch Sample" Step-by-step

"Create Branch Sample" Step-by-step

Introduction.

This is a step by step tutorial for understanding StateGo.
Now you learn branch.

 

Sample Program Specification

The sample specification you will make is below.

Pick up a number from 1 to 10 randomly.
If the number is 1, display "1st".
If the number is 2, display "2nd".
If the number is 3, display "3rd".
If the number is more than and equal to 4, display the number and "th".

 

Finally, display "You are XX.". XX will be replaced to the above word.

 

Preparation.

This is a continuation of "Hello World" Step-by-step tutorial.
If you are not ready, please start from the previous tutorial.

 

Get a random number.

Create a new default state named to"GET_RAND".
Define "m_val" member variable for retaining the random number to use following states.
Put the below code to "members" item.

 

[members]

int m_val = 0;

 

A random function "rand(a,b)" will be define later.
Put ther below code to "update" item.

 

[update]

m_val = rand(1,10);

 



 

Create branch

You will create branches by the picked number.

 

Click "GET_RAND" state.
The state context menu is shown.
Select "End Branch".
Branch Editor is opened.

 

Click empty space.
A contex menu is shown.
Select "New" and "IF".
Write a condition for branch.
Close Branch Editor by pushing "OK" button.

 

You will see a branch box bottom of "GET_RAND" state.

 



 

Create a state for each branch.

You will need states for each branch.

 

For displaying "1st", Create "1ST" state, put the bellow code into "update" item.

var o = new GameObject();
o.AddComponent().text = "You are 1st";
o.transform.position = Vector3.down;

 

Notice that avoiding "Hello World" cases to set the bellow position.
Notice to set the below position avoiding "Hello World".

 

 

The same applies below.

 

For 2nd.

var o = new GameObject();
o.AddComponent().text = "You are 2nd";
o.transform.position = Vector3.down;

 

For 3rd.

var o = new GameObject();
o.AddComponent().text = "You are 3rd";
o.transform.position = Vector3.down;

 

For 4th and more.

var o = new GameObject();
o.AddComponent().text = string.Format("You are {0}th",m_val);
o.transform.position = Vector3.down;

 

Connect arrows as below.

 



 

Convert.

 

Push "Save and Convert" button to put the output into "TestControl.cs".

 

Implement rand method.

 

You cannot execute it because "rand" method has not been implemented.
rand関数が実装されていなため、このまま実行はできません。

 

Open "TestControl.cs" by a source editor.

 

Note that you cannot edit the StateGo output part. If you do it, it will be overwritten when converting.

 

The implementation can be performed using "Embed State", but this tutorial introduces you to learn implementing it in source code.

int rand(int x, int y)
{
  var r = UnityEngine.Random.Range(x,y+1);
  return r;
}

 



 

Execute

Push "Play" button on Unity Editor repeatedly for confirming working finely.


 

Archinve.

psgg-unity-tutorial

 

What you learned.

The followings are you learned.

  1. How to define member.
  2. How to branch.
  3. How to implement methods using editor.

 

That's all.

GO TOP