Tutorial #4

Advanced

Here, we will create a project that performs START/STOP with a single button operation and randomly changes the dice roll.

Sample file

Commentary video (Part1)

Commentary video (Part2)

 TOMATE is equipped with a function to collect objects drawn in the drawing area and save them as figure data. 

[Creation procedure]

  1. On a new page, create dice rolls from 1 to 6 using square and circle shapes.
     
  2. Select all the “1” dice you created and select “Menu: File: Save parts as image”.
    Similarly, save the dice of 2 to 6 as an image.
     
  3. Select “Menu: Tools: Register Media: Image Image” to open the image list dialog.
    Register the 6 dice image files created in 2.
     
  4. On a new page, draw two rectangles and enter ‘img{[d1]}’  in one script field and ‘img{[d2]}’ in the other.
    Write ‘d1=d2=1’ in the variable column, the dice will be drawn on the two square objects.
     
  5. Draw a START/STOP button, enter the following command in the script field, and enter ‘btn=0’ in the variable field.
dsp{ [btn] : 0=START, white, blue : 1=STOP, white, red } :

if{ (btn==0) :

    int{10 : cmd{ rnd{[d1]:1,6} : rnd{[d2]:1,6} : (btn=1) } } :

    cmd{ int{0: } : (btn=0) : var{d1, d2} }

}

[Script commentary]

  •  The ‘img{ }’ command is a command to display an image image on an object.
    If you specify a number (1-6), the figure registered as an image will be displayed. You can also specify [variables].
    The displayed image is displayed according to the object size while maintaining the aspect ratio of the original image.
    The registration number starts from 1.
    If you specify an unregistered number, nothing will be displayed.
     
  • The ‘dsp{ }’ is a command to display on the object with the specified character string and color scheme according to the value of [variable].
    When btn=0, “START” is displayed in white characters on a blue background, and when btn=1, “STOP” is displayed in white characters on a red background.
     
  •  The ‘if{ }’ tests the truth value of an expression and executes the command assigned to truth or false.
    if{ (Expression) : TRUE Command: FALSE Command }
    Since truth is separated by colons, only one true or false command can be executed.
     
  • The ‘cmd{ }’ can combine multiple commands into one.
    Only one true or false command can be written in the ‘if{}’ statement.
    The same is true for the ‘int{}’ command.
    For this reason, cmd{ } combines multiple commands into one command. 
  • The ‘int{ }’ executes the specified command at regular intervals. The specified time is x1ms, and if 0 is specified, it will stop.
    In the sample, 10ms is specified, but increasing this value slows down the rotation of the dice. 
  • The ‘rnd{ }’ is a command that generates a random number within the specified range and sets it to [variable]. 
  • The ‘var{ }’ is a command that outputs the value of the specified variable to output.txt. Enclosed in { }, the contents of variables are output separated by commas.
    Example) If the die roll is 1,2
    var{ d1, d2 } → Output.txt to {1,2}

When you run the project you created, you will see two 1st dice and a button labeled “START”. When you press the START button, the display will switch to STOP and the dice will start rotating at high speed.
Pressing the STOP button will stop the two dice with random rolls. At this time, the die that stops at  ‘Output.txt’ is output like ‘{1,2}’.

 By reading the character string of ‘Output.txt’ in the user program, it can be used as an interface for the created screen.