Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Can a C#/Godot developer explain this to me?

A topic by CyberRobotnix created Dec 30, 2021 Views: 275 Replies: 1
Viewing posts 1 to 2
(1 edit)

How comes this snippet of code produces no error:

using Godot;
using System;
public class Animation : AnimatedSprite
{
    private AnimationPlayer Direction;
    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        // Must have the node AnimationPlayer attached to it.
        Direction = GetNode<AnimationPlayer>("AnimationPlayer");
    }
    public override void _Process(float delta)
    {
        if (Input.IsActionPressed("move_right")) Direction.Play("Right");
        if (Input.IsActionPressed("move_left")) Direction.Play("Left");
        if (Input.IsActionPressed("move_down")) Direction.Play("Down");
        if (Input.IsActionPressed("move_up")) Direction.Play("Up");
    }
//  // Called every frame. 'delta' is the elapsed time since the previous frame.
//  public override void _Process(float delta)
//  {
//      
//  }
}

But this snippet of code which is practically identical causes an error with the debugger telling me that I forgot to close the curly brackets on line 7:

using Godot;
using System;
public class Cycles : AnimatedSprite
{
    public override void _Ready()
    {
        public anim string = "5";
        //
    }
    //Changes the player's animation depending on which direction button is pressed.
    public override void _Process(float delta)
    {
        //if (Input.IsActionPressed("move_right")) play(String anim="right", bool backwards=false);
        //if (Input.IsActionPressed("move_left")) play(String anim="left", bool backwards=false);
        //if (Input.IsActionPressed("move_down")) play(String anim="down", bool backwards=false);
        //if (Input.IsActionPressed("move_up")) play(String anim="up", bool backwards=false);
    }
}

Also that being my first time ever coding a game (Started a month ago), I really wish the debugger would give me precise instructions instead of being vague and printing me a load of random errors so much so I don't know where to start and where to end. I'm considering installing an IDE on the side with helpful widgets to better understand how to code using C#.

(+1)

I'm not familiar with Godot, but in line 7 you declare a local variable (=inside a method, the method _Ready in this case) starting with the keyword "public".  That won't work. Access modifiers (like public, private, protected) can only be used for variables defined at class level, not local variables. So your compiler assumes that this declaration is meant to be at class level, and you skipped the closing curly brace for the _Ready method.