Coder Social home page Coder Social logo

se-scripts's Introduction

EasyScript

This project aims at lowering the entry barrier for creating nice and re-usable scripts for SpaceEngineers. It builds upon the MDK-SE by Malware, by offering an opinionated structure and helpers for common tasks.

How to use

  • Download the project.
  • Copy-paste the EasyScript directory into your project.
  • Inside Visual Studio:
    • right click on your project in the solution explorer.
    • select "Add โ†’ Reference..." from the menu.
    • in the Shared Projects group, tick EasyScript.

A first script

You no longer write your Program directly. Instead, you will be writing the MainScript, and EasyScript will call the appropriate functions at the right time.

Let's look at this scripts that shows a counter on all screens of the ship:

class MainScript : MyMainScript
{
    int Counter = 0;
    List<IMyTextPanel> Screens = new List<IMyTextPanel>();

    public override void onLoad(MyIni data) {
        Runtime.UpdateFrequency = UpdateFrequency.Update100;
    }

    public override void ScanBlocks(IMyGridTerminalSystem grid)    {
        grid.GetBlocksOfType<IMyTextPanel>(Screens, Block.OnGridOf(Me))
    }
    
    public override void onTick(UpdateType source) {
        Counter += 1;
        foreach (var screen in Screens) {
            screen.ContentType = ContentType.TEXT_AND_IMAGE;
            screen.WriteText(Counter.ToString());
        }
    }
}

Here is what each part does:

  • onLoad: is called when the world has finished loading and the script configuration has been checked and it is ready to run. Use it to perform initialization. Here we just set our script to run every 100 ticks of game time.
  • ScanBlocks: is called whenever you should rescan the grid. We use it here to get all screens that are on the same grid/subgrid than the ProgrammableBlock.
  • onTick: is called at the frequency defined in onLoad. Here we go through the screen list and write the counter on each.

Try to run it.

Try to add a screen after starting the counter: it will not update. This is because ScanBlocks is not called automatically for performance reason. But you can make it trigger easily: simply click "run" with no argument, or run the PB from a timer with no argument.

Keeping the counter in saved game

Easy: we will need to implement onSave and modify onLoad.

First, to avoid duplicating things, let's give a name to the save counter:

// At the top of MainScript class
static readonly string SaveVersion = "1";
static readonly MyIniKey CounterKey = new MyIniKey(SaveVersion, "counter");

We will use it to remember current counter value when the game is saved:

public override void onSave(MyIni data)
{
    data.Set(CounterKey, Counter);
}

And modify onLoad to actually load the value from the saved game:

public override void onLoad(MyIni data) {
    Counter = data.Get(CounterKey).ToInt32(0);
    Runtime.UpdateFrequency = UpdateFrequency.Update100;
}

Note: ToInt32 converts the value to an int, the type we used for our Counter. There are also ToIn64() for long, ToString() for string, โ€ฆ The value in the parenthesis will be used as default value if no saved value was found.

Allow resetting the counter

We want now to let the user reset the counter manually, or using a button or sensor.

We simply add a function to handle the command:

public override void onCommand(MyCommandLine args)
{
    if (args.Argument(0) == "reset") { Counter = 0; }
}

That's it! You can now reset the counter by calling the ProgrammableBlock with argument reset, from the terminal menu or from some trigger.

Done! You can review the full code in EasyCounter/Program.cs

se-scripts's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.