how to convert string to int in C#

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By moazdeyaa

so i have a string i want to convert to int but i cant use int() since i use C# so i tried
int.parse in the text editor it gave no errors but when running the game it crashes same with convert.toInt32 `
using Godot;
using System;

public class end : Area2D
{

public static int current_lvl;

private string s;

public override void _Ready()
{
s =(GetTree().CurrentScene.Name);
current_lvl=int.Parse(s);}
void on_end_body_entered(Node hi)
{
GetTree().ChangeScene("res://scenes/level
"+int.Parse(GetTree().CurrentScene.Name)+1+“.tscn”);
}
}
`note that the scene name is level_1,level_2 and so on and i want to extract thatlast number

:bust_in_silhouette: Reply From: Gluon

C# has a ToString method so you should be able to use that for example;

String str;
int num = 210;
str = num.ToString();

Confused how this is the accepted answer when the question was how to convert string to int. But if OP is happy, I guess it’s all good!

Matthew Goulart | 2022-12-24 04:12

Oh yeah good point, I guess I made a mistake that morning and didnt read it properly. In any case for anyone searching this in the future these are all of the string to number conversions from the C# documentation

decimal 	    ToDecimal(String)
float 	        ToSingle(String)
double 	        ToDouble(String)
short 	        ToInt16(String)
int 	        ToInt32(String)
long 	        ToInt64(String)
ushort 	        ToUInt16(String)
uint 	        ToUInt32(String)
ulong 	        ToUInt64(String)

Gluon | 2022-12-24 15:28