Open Surge Forum

A fun 2D retro platformer inspired by Sonic games and a game creation system

You are not logged in.

Announcement

Our community has moved to Discord! https://discord.gg/w8JqM7m

#1 2020-03-01 15:02:29

Makavo
Member
Registered: 2020-03-01
Posts: 2

Need some help with a script I am doing.

Currently, I am trying to make Sonic's Insta-shield in surge script. Problem is, I can't seem to find out how I can detect if the player has already pressed the jump button in the air, and prevent any other use of the shield until the player jumps and presses the jump button again. Sorry if this is pointless, as there could be a really simple solution, but I am really new to this.

Here is the code:

// ---------------------------------
// Sonic's Instant Shield!
// Now with temporary invincibility!
// ---------------------------------
using SurgeEngine.Audio.Sound;
using SurgeEngine.Actor;
using SurgeEngine.Vector2;

object "Insta-Shield" is "companion"
{
    insta = Sound("samples/InstaShield.wav");
    player = parent;
    
    state "main"
    {
        // The player is currently not using the Insta-Shield.
        if (player.jumping) {
            if(!player.input.buttonPressed("fire1")) {
                player.aggressive = false;
                player.invincible = false;
                state = "watching";
            }
        }
    }
    
    // Wait for the player to jump, then press the jump button again.
    state "watching"
    {
        // Is the player jumping and pressing the jump button? If so, then activate the shield.
        if(player.jumping) {
            if(player.input.buttonPressed("fire1")) {
                insta.play();
                state = "spin attack";
            }
        }
        else
            state = "main";
    }
    
    state "spin attack"
    {
        if(player.midair) {
           player.aggressive = true;
           player.invincible = true;
           state = "main";
        }
        else if(player.input.buttonPressed("fire1")) {
            return;
        }
    }
}

I will appreciate it if anyone is able to help me figure this out! wink

Offline

#2 2020-03-02 02:48:46

Alexandre
Administrator
From: Brazil
Registered: 2009-01-27
Posts: 3,272
Website

Re: Need some help with a script I am doing.

Hello Makavo and welcome to the forums!

You'll see it is simple to do once you grasp the logic. I'll write a more detailed explanation, but it's simple.

I generally suggest to people new at scripting to tackle on problem at a time. In your insta-shield, I see two challenges that need to be tackled:
1. figure out WHEN to activate the insta-shield (when the player is jumping, but only once - and as long as the player has no shield)
2. perform the insta-shield (make the player aggressive for a short while, play a sound, maybe display a graphic)

So at first we focus on step (1), since the second step depends on the first. To figure out the precise moment to activate the insta-shield, we use the state machines of SurgeScript.

There are countless ways to write scripts, and there is not necessarily a "right" answer, only answers that work. One possibility would be: create a companion object (as you did) and use only 3 states: "main", "watch button" and "locked". State "main" will watch for the moment the player jumps. State "watch button" will watch for the precise moment to perform the insta-shield. Since the insta-shield cannot be performed twice on the same jump, we introduce state "locked" to prevent its activation until the player reaches the ground.

It is sometimes useful to draw a diagram of the states and of the transitions between states to help you visualize and create things, but I'll skip it here.

object "Insta-Shield" is "companion"
{
    player = parent;

    state "main"
    {
        if(player.jumping) // if the player is jumping
            state = "watch button";
    }

    state "watch button"
    {
        if(!player.jumping) {
            // the player is no longer jumping
            state = "main";
        }
        else if(player.input.buttonPressed("fire1") && !player.shield) {
            // the player has pressed fire1 (JUMP) while jumping AND has no shield
            Console.print("Perform INSTA-SHIELD!");
            state = "locked";
        }
    }

    state "locked"
    {
        // the companion will remain locked in this state until the player is NOT midair
        if(!player.midair)
            state = "main";
    }
}

Do you get the logic?

(imo you don't need to modify the player.invincible flag, since it has to do with the invincibility stars)

Offline

#3 2020-03-02 22:22:38

Makavo
Member
Registered: 2020-03-01
Posts: 2

Re: Need some help with a script I am doing.

Just tested this script out, and it works. Thanks for the help and advice!

Offline

Board footer

Powered by FluxBB  hosted by tuxfamily.org