Writing Your First Bot in C#
In GrindFest, a "bot" is a program that controls your hero automatically. You write these programs in C# to make your hero perform actions like fighting monsters, collecting items, and exploring the world.
The C# code (.cs files) are text and you can edit them in any text editor, but you should use an Integrated Development Environment (IDE) to make the code more readable and easier to write.
Press the "Edit Scripts" button in the top left corner of the game window to open the bundled IDE VSCodium.
Warning
If you know even a little about programming, skip the scripts and top-level statements and start with classes.
Comments
// double slashes mark a comment, something that is not code and will be ignored
Commets are used to explain the code to make it more readable or temporarily disable lines of code
// Hero.FollowCursorAndAttack(); // Makes your hero follow your mouse cursor and attack nearby enemies
To uncomment a line of code, remove the //
at the beginning of the line.
The Hero Class
Before we write our first line of code, let's understand what Hero
is. The Hero class
is what you'll use to control your character in the game. It has all the methods (actions) your character can perform, like:
- Moving around
- Attacking enemies
- Picking up items
- Drinking potions
- etc.
And properties (values) that represent your hero's current state, like:
- Health, MaxHealth
- Level, StatPoints
- Strength, Dexterity, Intelligence
- Inventory, Equipment
- etc.
You'll write code using these methods and properties to make your hero do things automatically.
Method call
Replace contents of your .cs script file with following C# code
Hero.FollowCursorAndAttack();
This is a method call
of a method FollowCursorAndAttack() on a static class Hero representing your current hero.
A method contains a sequence of code statements, this ones makes your hero follow your mouse cursor and attack nearby enemies.
Each statement (like a method call) in C# ends with a semicolon ;
Note: In some cases, semicolon can be omitted, as in this one, because this is top-level statement ouside of a method body.
The statements are executed in the order they appear in the file, from top to bottom. But when you call
a method, the code execution jumps into the method and continues in there. When the method finishes executng it's code, the code execution returns
to the place where the method was called and resumes from there.
If the method takes any parameters, you need to pass them inside the parentheses as argument1, argument2, ...
.
Parameters are a way to pass information to a method, like what to say in the Say(string) method.
Running the Bot
Press the "Run" button in the top left corner of the game window (or the shortcut F5
) to run your script.
Note
Any changes you make in any file will take immediate effect. Your bot needs to be running to see the changes.
What to do next?
- Movement & Combat (variables, conditions)
- Pick up & Equip Items (foreach loops)
- Hero class api reference
- Hero.Idle() basic bot implementation
- Debugging
- Classes (Advanced)
- AutomaticHero class api reference
IDE
This is how the IDE looks like. it should show you method signatures, properties and documentation when you hover over them with your mouse cursor. You can also Ctrl + Click
on a method to jump to its definition.
You should also see compilation errors highlighted with red squiggly lines and you can see the error message when you hover over them.
.NET SDK
If you are missing the .NET SDK, you can download it from here
Alternative IDEs
If you don't want to use the default IDE, you can use any of the following:
JetBrains Rider recommended
Visual Studio Community Edition
Visual Studio Code VSCodium is based on this, but this has more advanced C# integration