Skip to content

Programming Mechanic

OutCraft edited this page Apr 17, 2023 · 19 revisions

Basics

Here at Create Robotics you can program you robots with our own script language.
Here you can find all commands properly documented and a lot more!

To program a robot like a Code Drone you first need a Code Editor Block. Right click it with a Program Item to get started!

Now we can start to code. Let's say we have a command called testCommand that takes one parameter from type ... let's make it simple: number.

robot.testCommand(5);

Easy, right? Don't forget the ; at the end of each command!

Now something a bit harder:
Let's say we have testCommand2 that takes two parameters from type number and one from type item. Parameters are seperated by a ,.
Try it yourself first!

robot.testCommand2(5, 8, minecraft:diamond_block);

What you can do now is add math to number parameters:

robot.testCommand2(5, 2 + 2 * 3, minecraft:diamond_block);

And what is 2 + 2 * 3? Yes, 8!
The math system follows basic algebraic rules (parentheses before multiplication/division before addition/subtraction)

Wasn't too hard, right?

Supported math equations

Addition: 9 + 9

Subtraction: 9 - 9

Multiplication: 9 * 9

Division: 9 / 9

Power: 9 ^ 9

Square root: sqrt(9)

Sine, Cosine and Tangent: sin(9), cos(9) and tan(9)

And of course parentheses: 5 * (1 + 3) = 20 and not 16

These are all basics you should know! Let's get to something harder.

Variables

Our script language also supports variables!

Let's say you have a variable called myVar. This is how you can you use that variable in your code:

robot.testCommand(${myVar} + 8);

Defining own variables

There are three types of variables: internal, private and public.
Note that when there are internal, private and public variable with the same name it will first try to get the internal, if it doesn't exist the private and if that also doesn't exist the public.

internal variables are default variables you can't edit or overwrite.

private variables are variables private to only the robot executing it.

public variables are shared between all robots. Editing one affects all robots using it.

So how do you define your own variable?

private myVar = 8;

That's it! But let's say you want to add 3 to myVar. How would you do that?

private myVar = ${myVar} + 3;

Defaults

The mod has some default (predefined) commands, variables etc.
Find all of them here:

Default Parameter types

There aren't really "arguent types" in our script language. Everything here is just text (String). But most commands expect a specific formatted parameter.
All of them are listed here, as easy to understand as possible:

optional[something]: an optional parameter. Can be of type something or empty. This one is a bit more advanced so here's an example:
Let's say that robot.testCommand3 takes a number and an optional[number]. That means that you can use robot.testCommand3 in two different ways:

robot.testCommand3(5);
robot.testCommand3(5, 8);

number: A floating-point number with max 15 digits after the ., equivalent to the data-type double used in many programming languages.

item: A text representing the registry value of a minecraft item like minecraft:diamond or robotics:tin_ore

direction: A text representing a direction. Can be Direction.UP, Direction.DOWN and all compass directions like Direction.NORTH. Equivalent to an enum used in many programming languages.

Default Commands

robot.goTo

Description:

Makes the robot pathfind to the given coordinates

Parameters:

  • number the x Position to go to
  • number the y Position to go to
  • number the z Position to go to

robot.getItems

Description:

Makes the robot get items from the container at the given coordinates.
The robot needs to be in a 4 block radius of the container to be able to access it.

Parameters:

  • number the x Position of the container
  • number the y Position of the container
  • number the z Position of the container
  • optional[item] the item to take out of the container

robot.pushItems

Description:

Makes the robot push items into the container at the given coordinates.
The robot needs to be in a 4 block radius of the container to be able to access it.

Parameters:

  • number the x Position of the container
  • number the y Position of the container
  • number the z Position of the container
  • optional[item] the item to put into the container

robot.punch

Description:

Makes the robot punch on the block or entity at the specific coordinates.
The robot needs to be in a 4 block radius of the position to be able to punch it.

Parameters:

  • number the x Position of the position to punch
  • number the y Position of the position to punch
  • number the z Position of the position to punch
  • optional[direction] the direction to punch to
  • optional[item] the item to punch with

robot.use

Description:

Makes the robot right click on the block or entity at the specific coordinates.
The robot needs to be in a 4 block radius of the position to be able to right click it.

Parameters:

  • number the x Position of the position to right click
  • number the y Position of the position to right click
  • number the z Position of the position to right click
  • optional[direction] the direction to right click to
  • optional[item] the item to right click with

robot.wait

Description:

Makes the robot wait for a specific amount of time

Parameters:

  • number the time to wait in milliseconds

robot.waitForRedstoneLink

Description:

Makes the robot wait until it gets a redstone link signal

Parameters:

  • item the first frequency of the redstone link signal
  • optional[item] the second frequency of the redstone link signal

Default Variables

xPos

Description:

Returns the x position of the robot as number

yPos

Description:

Returns the y position of the robot as number

zPos

Description:

Returns the z position of the robot as number

Examples

Here are some examples of Code to show some example usages:

Chest Delivery

Let’s say you want your robot to take ores from a chest and deliver them to another chest. Here’s how you would do that:

Position Chest 1: [16, 59, -89]
Position Chest 2: [20, 60, -72]

The first thing we should do is save the chest positions into a variable so that we can easily modify them later.

private chestPosOne = 16, 59, -89;
private chestPosTwo = 20, 60, -72;

Here we created two private variables that contain 16, 59, -89 and 20, 60, -72.
They are private because only our robot needs to know them and not every robot in existence.

Now let's tell our robot to go to the first chest and take the items.

robot.goTo(${chestPosOne});
robot.getItems(${chestPosOne});

The command robot.getItems takes four parameters. The variable chestPosOne already has the three parameters 16, 59, -89. Now, where's the fourth parameter?
As seen in the documentation of robot.getItems, the fourth argument is an optional argument. That means that we can just leave it out. The description is the item to take out of the container and we don't want our robot to only take a specific item so we just leave it blank.

If you want your robot to only take diamonds the second line would look like this:

robot.getItems(${chestPosOne}, minecraft:diamond);

After that we want our robot to go to the second chest and push all items into it. The code we need for this is:

robot.goTo(${chestPosTwo});
robot.pushItems(${chestPosTwo});

The command robot.pushItems takes four parameters as well. The explanation why we don't use it is the same.

But if you want your robot to only put raw tin into the chest the second line would look like this:

robot.pushItems(${chestPosTwo}, robotics:raw_tin);

This is the complete code:

private chestPosOne = 16, 59, -89;
private chestPosTwo = 20, 60, -72;

robot.goTo(${chestPosOne});
robot.getItems(${chestPosOne});

robot.goTo(${chestPosTwo});
robot.pushItems(${chestPosTwo});

If we want to be extra fancy we can document our code with /* Comments */. We can also (optionally) let the robot wait a bit:

/* We first define the chestPos variables */
private chestPosOne = 16, 59, -89;
private chestPosTwo = 20, 60, -72;
private waitTime = 100; /* optional */

/* Then we let the robot go to the first chest and take items out */
robot.goTo(${chestPosOne});
robot.getItems(${chestPosOne}); /* We have no last argument because we want the robot to take everything */

/* Then we can optionally wait some time */
robot.wait(${waitTime});

/* Then we let the robot go to the second chest and put all items it has in */
robot.goTo(${chestPosTwo});
robot.pushItems(${chestPosTwo});

That's it! You wrote a program that makes your robot transfer items from one chest to another one! Remember that the code will not loop! An easy way to make it loop is to build a Deployer at the second chest with a Wrench in hand. Right-clicking with a wrench will make the robot run the code again.

Optimized Documentation for GPT-Models

If you're lazy you can just let GPT-Models like ChatGPT write the code for you!
Remember that this is experimental and may not always work!

To do so explain what you want (Example: Make a robot go to a chest ten blocks above it and get all items inside) and paste this block of text below it.