Oh wait!
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)).
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);
Oh wait!
AppendLine()
would solve the issue, but no.Source:
Nodes
Scenes
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.
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;
}
}
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;
}
}
Processing
Idle Processing
_Process(float delta)
method, which is called every frame. delta is the time variation, which can be used to multiply another variable.Physics Processing
_PhysicsProcess(float delta)
method, which is called every physical frame._Process()
, but then isn’t it a physical thing?Groups
Overridable functions
_Ready()
makes sure that all children nodes are active in a scene.Creating nodes with code
_nodeName = new NodeName();
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()
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.
Not really, i think just knowledge of vector math, like this tutorial can provide: Vector math — Godot Engine (stable) documentation in English
I think I’ll work on a Pong clone.
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.
I’m trying to draw a rectangle and set it as a texture for a sprite.
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.
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.
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
.