The SIMPLEST Screeps Tutorial
Learn Code By Gaming Learn Code By Gaming
28.4K subscribers
48,939 views
0

 Published On Feb 10, 2020

Complete step-by-step tutorial for getting started in Screeps. For non-programmers and those new to JavaScript, I break down all the concepts for how you would write this beginner code. This is a follow up to my Simplest Screeps Code Possible video. I tried to go really slow in this one for anyone struggling to get into the game.

Grab the code from my blog: https://learncodebygaming.com/blog/th...
Try it out for yourself in the Screeps simulator: https://screeps.com/a/#!/sim/survival

If you're new to programming, or new to JavaScript, but you think Screeps looks really cool and you want to play it, in this tutorial I'm going to show you the simplest Screeps code possible and I'm going to walk you through what each line of code means, what it does, and how I got there. Super beginner friendly is my goal here.

To get started for this tutorial we just need to go into the Screeps simulation room.

So go to Screeps.com, scroll down to the "Live Demo", and for "Simulation Mode" select "Training". This will bring you into the training room for Screeps. You don't even need to register or anything to follow along here.

The first thing it's asking you to do is place your spawn. This is a building that's going to create creeps for you. So go ahead and place it somewhere near this energy source and near your controller. And you can just keep the default name, "Spawn1".

Then in the lower left click "Script" to open up your code. Go ahead and expand it up so you can see more of it. So the first bit of code that they give you when you're starting out is this module.exports.loop equals some function. So what this is, it's the main game loop that gets exported and run by the Screeps server. Any code you put inside of this function that's being exported will be run once per tick.

So anything you want to do with your creeps or your structures, all that code needs to go within these two curly brackets, or at least be initiated from here. And you don't need to fully understand how all this is working to get started. Just think of it as boilerplate code that needs to be there in your main script, and remember that the code you want to write all needs to go in here.

The first line of code they give you inside the main game loop is actually just a comment. You create comments in JavaScript by using these two slashes, and they allow you to leave notes inside your code. And you can write whatever you want inside a comment, they're ignored when your code is executed. So this is your space to take notes, to give yourself reminders about what you're doing. And every programming teacher ever is going to tell you to leave lots of comments, because it's really good advice. So let's put a comment here to remind us about how the main game loop works.

// This is the main game loop. The code inside of the curly brackets will run once every tick.

And while we're talking about ticks, in the upper right here you can control the speed of the ticks inside the simulation. You can pause the game, or you can change the tick speed to speed up or slow down the game.

Now, before we start writing any code, let's just first talk conceptually about what we want to do to write the simplest Screeps code possible.

So right now, all we have is a spawn, and a spawn can be used to create creeps. And the object of the game is to go and harvest energy, from an energy source, and we want to take that energy and use it to upgrade our room controller. And as we upgrade our room controller to higher levels, it will give us the ability to build new types of structures and to create stronger creeps.

So to do that, we need to, from our spawn, create a creep. Then we need to program our creep to go over to the source, get some energy from it, and then take that energy over to the room controller and upgrade it. And then we want our creep to just go back and forth doing that process forever.

So now that we've talked about what we want our code to do, the next thing I like to do, when I'm writing something that's new or complex for me, I just like to outline my logic with comments first. So this isn't any code that I'm writing, this is just notes to myself about what I want my code to do. This breaks everything down into smaller chunks that I can then tackle one at a time.

So the first thing we want to do is we want our spawn to create a creep if it doesn't exist.

// if our creep doesn't exist, create it from our spawn

Ok, so once our creep exists, it's going to be helpful to get a reference to that creep, because we're going to use that object a lot as we're telling our creep what to do. This way we don't need to look it up every time.

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

show more

Share/Embed