-
Notifications
You must be signed in to change notification settings - Fork 38
Programming Mechanic
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?
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)andtan(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.
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);
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.
internalvariables are default variables you can't edit or overwrite.
privatevariables are variables private to only the robot executing it.
publicvariables 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;
The mod has some default (predefined) commands, variables etc.
Find all of them here:
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 typesomethingor empty. This one is a bit more advanced so here's an example:
Let's say thatrobot.testCommand3takes anumberand anoptional[number]. That means that you can userobot.testCommand3in 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-typedoubleused in many programming languages.
item: A text representing the registry value of a minecraft item likeminecraft:diamondorrobotics:tin_ore
direction: A text representing a direction. Can beDirection.UP,Direction.DOWNand all compass directions likeDirection.NORTH. Equivalent to anenumused in many programming languages.
Description:
Makes the robot pathfind to the given coordinates
Parameters:
numberthe x Position to go tonumberthe y Position to go tonumberthe z Position to go to
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:
numberthe x Position of the containernumberthe y Position of the containernumberthe z Position of the containeroptional[item]the item to take out of the container
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:
numberthe x Position of the containernumberthe y Position of the containernumberthe z Position of the containeroptional[item]the item to put into the container
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:
numberthe x Position of the position to punchnumberthe y Position of the position to punchnumberthe z Position of the position to punchoptional[direction]the direction to punch tooptional[item]the item to punch with
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:
numberthe x Position of the position to right clicknumberthe y Position of the position to right clicknumberthe z Position of the position to right clickoptional[direction]the direction to right click tooptional[item]the item to right click with
Description:
Makes the robot wait for a specific amount of time
Parameters:
numberthe time to wait in milliseconds
Description:
Makes the robot wait until it gets a redstone link signal
Parameters:
itemthe first frequency of the redstone link signaloptional[item]the second frequency of the redstone link signal
Description:
Returns the x position of the robot as
number
Description:
Returns the y position of the robot as
number
Description:
Returns the z position of the robot as
number
Here are some examples of Code to show some example usages:
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.
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.