0 votes

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 onendbodyentered(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

Godot version 3.5.1
in Engine by (25 points)

1 Answer

+1 vote
Best answer

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

String str;
int num = 210;
str = num.ToString();
by (3,321 points)
selected by

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!

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)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaste[email protected] with your username.