Is C# easier to learn than C++?

By “read and write”, I mean that I can’t actually enter multiple lines in a single file unless I hypothetically use the ‘\n’ character literal.

Oh wait!

1 Like

I think this may help: (c = getchar()) != EOF in C#? - Stack Overflow, since that’s what cat does when it isn’t given any files to read. (It reads everything from stdin (the user input) until it receives EOF (by pressing ^D)).

A question: imagine I’m editing a file. I want to specify the index of a string with my cursor position. Not my mouse cursor, but where my “text” cursor is located within the current string.

Update: I’m using the Append() method for StringBuilder within a while loop. It works fine except that the very first character of everyline is always ignored.

Sample text:

// new line
ey, I'm Jon Doe. What's your name?
'm Jane Doe. How was your day?
 don't know because all of this is a test made by a newbie programmer.

Sample text from cmd:

Hey, I'm Jon Doe. What's your name?
I'm Jane Doe. How was your day?
I don't know because all of this is a test made by a newbie programmer.

Here’s the relevant piece of code.

Summary
                    Console.WriteLine("Please enter whatever you like.");
                    while(true)
                    {
                        if (Console.ReadKey().Key == ConsoleKey.End)
                        {
                            break;
                        }

                        sb.Append('\n');
                        sb.Append(Console.ReadLine()); // seems to ignore the first character of each line and doesn't create a new line
                        content = sb.ToString(); // hypothesis: perhaps it simply needs some help to create a new line.
                    }

                    File.WriteAllText(filePath, content);

Please ignore the comments as they aren’t relevant anymore. Now, it can create a new line. It just always ignores the first character of each line.

Oh wait!

Ah, nevermind. I thought that using AppendLine() would solve the issue, but no.


I’m really tempted to try and start learning more C# with Godot 2D. I’ve downloaded Godot Mono 3.3. I’ll share what I’m learning once more.

Source:

Nodes

So nodes are some kind of entities that can perform various processes. They have properties and they can also have children, which can form a tree. So nodes are kinda like classes but not necessarily for programming?

Scenes

Scenes are basically a bunch of nodes. More on that later.

You are calling

Which probably eats the first character. You need some other way to check for end of input.

You could have debugged this issue yourself if you had put a print or breakpoint after doing a Console.ReadLine and printing what that outputs. That tells you where the problem is:

  • Either the ReadLine result is incorrect, and you know that the problem is there
  • Or alternative if the ReadLine result is correct, then the later code is a problem.

This way you can quickly bisect your program to find where a problem is.

1 Like

You were right! Using the End key condition at the start of the loop wasn’t good albeit that’s not what you suggested. I put it at the end of the loop and now it only seems to eat the first character of the last line. I do need however a new method of ending the edit. Perhaps calling a custom command could solve it.

YES!

while(true)
{
    sb.AppendLine(Console.ReadLine());
    content = sb.ToString();

    if (content.Contains("$end") == true) // if the string contains the command '$end'...
    {
        content = content.Remove(content.IndexOf("$end")); // then remove the first occurence command and everything following it
        break;
    }
}

Problem solved! Now, the only issue is that it modified the content string instead of modifying the StringBuilder object.

Anyway, me I’m big smart! Big brain!

OH YES sadsdksajd$#$#@J$NWL#WKEwK

blackjacksike.exe stopped working…

Rebooting…

Ah, here’s the solution!

            while(true)
            {
                sb.AppendLine(Console.ReadLine());
                content = sb.ToString();

                if (content.Contains("$end") == true) // if the string contains the command '$end'...
                {
                    sb = sb.Remove(content.IndexOf("$end"), sb.Length - content.IndexOf("$end")); // then remove the first occurence command and everything following it
                    content = sb.ToString();
                    break;
                }
            }

MATURED NEURON


INTELLIGENCE

Processing

Idle Processing

Done by using the _Process(float delta) method, which is called every frame. delta is the time variation, which can be used to multiply another variable.

Physics Processing

Done by using the _PhysicsProcess(float delta) method, which is called every physical frame.

Question: How do I make the difference between each other? In the tutorial, it says movement could be used within _Process(), but then isn’t it a physical thing?

Groups

Can be used for nodes with common features (e.g. allies, enemies, etc.).

Overridable functions

_Ready() makes sure that all children nodes are active in a scene.

Creating nodes with code

_nodeName = new NodeName();

Does it mean like NodeName is the parent or they just don’t explain it yet?

_nodeName.Free(); // removes a node from a scene
_nodeName.QueueFree(); // same thing but when it's safe

Instancing Scenes

var scene = GD.Load<PackedScene>("res://scene.tscn");
var node = scene.Instance();
AddChild(node);

That depends entirely on if you use physics for moving or not. Thrive uses physics for moving so we need to do camera interpolation in _PhysicsProcess to avoid jitter.

No. NodeName there is just the class name of the node you want, for example:
new Spatial() or in Thrive we have custom node classes like new Microbe()

1 Like

Just in case this kind of thing happens albeit unlikely, how does licensing work? I mean, you can’t just create a project and say “Hey, it has the MIT license now!” Right?

You in fact can. Assuming any third party code you used, doesn’t have restrictions on derivative works. For example if you use any GPL code in your project, your whole project must be licensed under the GPL license.

1 Like

By the way, in terms of 2D vectors, is there any math notions I should learn before doing complex things? Do I need to know matrices? Quaternions? Complex numbers?

Not really, i think just knowledge of vector math, like this tutorial can provide: Vector math — Godot Engine (stable) documentation in English

1 Like

So things only get weird when going in 3D, right?

EDIT: Eh, I think I’ll get more experience with basic C# instead.

Or maybe I just need to make a game without following a tutorial. That would be best, yes. That doesn’t mean I won’t make or update CMD programs anymore, though.

I think I’ll work on a Pong clone.

Update: I’m trying to call Rect2.Rect2() but it says that Rect2 doesn’t have a definition for that method.

Constructors aren’t methods. Also I find using an IDE or text editor with plugins that has auto complete for C# to be really nice to quickly see if what you are typing is a thing that exists or not.

1 Like

Yeah, I just figured that it was a constructor, lol.

I’m trying to draw a rectangle and set it as a texture for a sprite.

Finally, I used a rectangle from Inkscape as a placeholder. Wouldn’t it be better to draw it instead? Imagine I implement an option to change the resolution, wouldn’t that be difficult to recompute the player’s rectangle size if it’s purely based on an image?

For some reason, when I use a key to move my player1 bar, it immediately goes up on top and on bottom when closing the game.

using Godot;
using System;

public class Main : Node
{   
    private Vector2 _screenSize;
    private Vector2 velocity;

    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        _screenSize = GetViewport().Size;
        StartGame();
    }

    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(float delta)
    {
        if (Input.IsActionPressed("ui_up"))
        {
            velocity.y -= Convert.ToSingle(_screenSize.y * 0.25);
        }
        else if (Input.IsActionPressed("ui_down"))
        {
            velocity.y += Convert.ToSingle(_screenSize.y * 0.25);
        }

        if (velocity.Length() > 0 || velocity.Length() < 0)
        {
            GetNode<Area2D>("Player1").Position = velocity * delta;
        }

        GetNode<Area2D>("Player1").Position = new Vector2(
             x: Mathf.Clamp(GetNode<Area2D>("Player1").Position.x, Convert.ToSingle(_screenSize.x * 0.0328125), Convert.ToSingle(_screenSize.x * 0.0328125)), // Position.x is always the same
             y: Mathf.Clamp(GetNode<Area2D>("Player1").Position.y, Convert.ToSingle(_screenSize.y * 0.0625), Convert.ToSingle(_screenSize.y - _screenSize.y * 0.0625))
        );
    }

    public void StartGame() 
    {
        GetNode<Area2D>("Player1").Position = new Vector2(Convert.ToSingle(_screenSize.x * 0.0328125), Convert.ToSingle(_screenSize.y * 0.5));
        GetNode<Area2D>("Player2").Position = new Vector2(Convert.ToSingle(_screenSize.x * 0.9671875), Convert.ToSingle(_screenSize.y * 0.5));
    }
}

delta is there for a reason. If you don’t use delta in your calculations, you are tying the game speed to framerate… and here if you have vsync on it takes 15th of a second to move entire screen size because you don’t take delta into account.

So using GetNode<Area2D>("Player1").Position = velocity * delta; isn’t enough? Interesting.

Actually I didn’t read that far. That line is probably fine, but the speed or something else might be bad.

Also you are constantly calling GetNode in your code, that is very bad for performance. You should only call GetNode once in your _Ready method.

1 Like

What do you mean by that? Do you mean the rectangle instantaneously goes to the top or bottom when pressing a key to move?

Kind of, but only at the beginning.

I just noticed you aren’t resetting velocity. Since you are using the += operator to set the velocity, the velocity will increase the longer you hold the key. Use = or set velocity to Vector(0, 0) at the end of _Process.
Also, I think velocity.Length() > 0 || velocity.Length() < 0 can be rewritten as velocity.Length() != 0 or velocity.y != 0.

1 Like