RobotJS Tutorial for Beginners - Learn JavaScript by playing RuneScape 2/4
Learn Code By Gaming Learn Code By Gaming
28.4K subscribers
42,960 views
0

 Published On Mar 23, 2020

Part 2 of this tutorial will teach you the basics of how to automate mouse inputs using RobotJS. We go over require statements, functions, how to sleep in JavaScript, while loops, variables, and how to use the mouse methods provided by RobotJS. By the end of this video we'll have a very simple stand-in-place RuneScape bot, and you'll have an understanding of JavaScript's fundamental concepts.

* This tutorial is for educational purposes only *

Full tutorial playlist:    • Learn JavaScript by playing RuneScape  

View the Source Code
GitHub repo: https://github.com/learncodebygaming/...

Download Links
Node.js: https://nodejs.org/
RobotJS: http://robotjs.io/
Visual Studio Code: https://code.visualstudio.com/download

Recommended JavaScript Resources
Eloquent JavaScript: https://amzn.to/3deUEfb or online edition at https://eloquentjavascript.net/
W3Schools: https://www.w3schools.com/js/default.asp

0:18 How require statements work in Node.js
2:05 Using a main function
3:48 Sleep function in JavaScript
5:43 Function parameters explained
6:59 RuneScape private server setup steps
9:08 Clicking with RobotJS
12:09 Multiple automated mouse clicks
13:21 RobotJS documentation
15:47 Functions vs Methods
16:10 While loops
21:10 Recap

* If your mouse is not moving to the correct coordinates *
Try this: https://github.com/learncodebygaming/...

In the first video, we installed Node.js and the RobotJS library. Now we're ready to start building our bot.

A require statement allows you to import code from another file or another package. Here we use it to import the RobotJS package so that we can use the functions inside of it. You can read more about how require statements work here: https://nodejs.org/en/knowledge/getti...

In VSCode, you can ctrl + click on require("robotjs") to open up the file that's being imported. In there, you can see all the available functions.

The universal print statement for JavaScript is console.log("");. Anything you put inside those quotes will print to your terminal when your program runs.

On the robot object, we are calling the mouseMove() function, and we are providing to it two parameters (the x and y coordinates for the position where we want to move the mouse to).

The next thing I want to do is wrap our code inside a main function. We can declare a function like function main(), where function is a keyword and main is the name we are giving the function. Because this function doesn't need any parameters, nothing appears inside the parentheses. This is followed by curly brackets, and the content of our function must be put entirely inside of these brackets. These brackets define the scope inside the function.

Once a function is defined, the code inside of it is not run until that function is called. At the bottom of our file is where we will call our main() function.

The reason we use functions is it encapsulates our logic inside a nice package that we can use and re-use later.

One of the things we know we're going to have to do with our bot is move the mouse, and then wait a little bit of time, and then move the mouse somewhere else. To do that, we need a way to pause the execution of our code while we're waiting to take the next action. This type of behavior is called a sleep function. Most programming languages have an easy built-in way to perform a sleep, but JavaScript does not. There are a few different ways to perform a sleep in JavaScript, but I find it easiest for our purposes to use this function:

function sleep(ms) {
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
}

To call this function, we would use sleep(), like we did before with main(), except this function expects us to pass in an argument for its one parameter. This parameter, labeled ms, is how many milliseconds we want to sleep for. So to wait for 2 seconds, we would call sleep(2000); Test this to confirm sleep is working by adding a sleep call in between the starting and done console.log statements.

Now let's start up the game. It's going to be very important that we maintain a consistent window size and location when the game is running. That way pixel coordinates will be consistent while we're developing our code. I recommend you go to the graphics options in the game settings and use "resizable" mode, and then you can double click on the top bar of the game window to maximize the window size.

Inside RuneScape, you'll need to procure an "Iron hatchet" to begin woodcutting. Once you've done that, find some trees to chop down. I've been practicing on the trees just north of Lumbridge. You cut down trees just by clicking on them. So let's write some code to do that clicking for us.

Continue with the written tutorial here: https://learncodebygaming.com/blog/ro...

show more

Share/Embed