Skip to content

Conversation

@Flo12344
Copy link
Contributor

Hello, I started using Lua-CSharp in Godot for my game to implement modding and map making scripts.
While testing my script I realized that in Godot when using floating number the parser would make the script crash and while searching the error I come up with this little change, which fixes the crashes.

Small exemple of a script in my test scene :
https://github.com/user-attachments/assets/34f8c20b-d729-450d-b18f-685081274d3a

(Hope it can be useful ^^)

@Flo12344
Copy link
Contributor Author

If you are interested I could also add the Godot C# setup to the Readme

@nuskey8
Copy link
Owner

nuskey8 commented Oct 24, 2024

ToString() incurs an allocation to create a new string. Use the overload that accepts ReadOnlySpan<char>.

@nuskey8
Copy link
Owner

nuskey8 commented Oct 24, 2024

Also, the video alone is not sufficient to determine the exact cause of the error. Please provide the error message, stack trace and C# and Lua source code.

@Flo12344
Copy link
Contributor Author

Error message :

E 0:00:02:0148   void System.Number.ThrowFormatException<TChar>(System.ReadOnlySpan`1[TChar]): System.FormatException: The input string '1.0' was not in a correct format.
  <C++ Error>    System.FormatException
  <C++ Source>   :0 @ void System.Number.ThrowFormatException<TChar>(System.ReadOnlySpan`1[TChar])
  <Stack Trace>  :0 @ void System.Number.ThrowFormatException<TChar>(System.ReadOnlySpan`1[TChar])
                 :0 @ double System.Double.Parse(System.ReadOnlySpan`1[System.Char], System.Globalization.NumberStyles, System.IFormatProvider)
                 :0 @ double Lua.CodeAnalysis.Syntax.Parser.ConvertTextToNumber(System.ReadOnlySpan`1[System.Char])
                 :0 @ bool Lua.CodeAnalysis.Syntax.Parser.TryParseExpression(Lua.CodeAnalysis.Syntax.SyntaxTokenEnumerator&, Lua.CodeAnalysis.Syntax.OperatorPrecedence, Lua.CodeAnalysis.Syntax.Nodes.ExpressionNode&)
                 :0 @ Lua.CodeAnalysis.Syntax.Nodes.ExpressionNode[] Lua.CodeAnalysis.Syntax.Parser.ParseExpressionList(Lua.CodeAnalysis.Syntax.SyntaxTokenEnumerator&)
                 :0 @ Lua.CodeAnalysis.Syntax.Nodes.ReturnStatementNode Lua.CodeAnalysis.Syntax.Parser.ParseReturnStatement(Lua.CodeAnalysis.Syntax.SyntaxTokenEnumerator&)
                 :0 @ Lua.CodeAnalysis.Syntax.Nodes.StatementNode Lua.CodeAnalysis.Syntax.Parser.ParseStatement(Lua.CodeAnalysis.Syntax.SyntaxTokenEnumerator&)
                 :0 @ Lua.CodeAnalysis.Syntax.LuaSyntaxTree Lua.CodeAnalysis.Syntax.Parser.Parse()
                 :0 @ Lua.CodeAnalysis.Syntax.LuaSyntaxTree Lua.CodeAnalysis.Syntax.LuaSyntaxTree.Parse(string, string)
                 :0 @ System.Threading.Tasks.ValueTask`1[System.Int32] Lua.LuaStateExtensions.DoStringAsync(Lua.LuaState, string, System.Memory`1[Lua.LuaValue], string, System.Threading.CancellationToken)
                 :0 @ void Lua.LuaStateExtensions+<DoStringAsync>d__1.MoveNext()
                 :0 @ void System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                 :0 @ void System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)
                 :0 @ void System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task, System.Threading.Tasks.ConfigureAwaitOptions)
                 :0 @ TResult System.Threading.Tasks.ValueTask`1.get_Result()
                 LuaErrorShowcase.cs:13 @ void LuaErrorShowcase+<_Ready>d__0.MoveNext()
                 :0 @ void System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                 :0 @ void System.Threading.Tasks.Task+<>c.<ThrowAsync>b__128_0(object)
                 GodotSynchronizationContext.cs:51 @ void Godot.GodotSynchronizationContext.ExecutePendingContinuations()
                 GodotTaskScheduler.cs:76 @ void Godot.GodotTaskScheduler.Activate()
                 ScriptManagerBridge.cs:84 @ void Godot.Bridge.ScriptManagerBridge.FrameCallback()

Code :

using Godot;
using System;
using Lua;

public partial class LuaErrorShowcase : Node
{
    public override async void _Ready()
    {
        // Create a LuaState
        var state = LuaState.Create();

        // Execute a Lua script string with DoStringAsync
        var results = await state.DoStringAsync("return 1.0 + 1");

        // 2
        Console.WriteLine(results[0]);
    }
}

@Akeit0
Copy link
Collaborator

Akeit0 commented Oct 24, 2024

It seems to be a problem about decimal separator.
I think
double.Parse(text,NumberStyles.Float, CultureInfo.InvariantCulture)
is most appropriate.

@Flo12344
Copy link
Contributor Author

@Akeit0 that's it my bad, I'm not used to use those function in c#

@nuskey8 nuskey8 merged commit 2070b28 into nuskey8:main Oct 25, 2024
@nuskey8
Copy link
Owner

nuskey8 commented Oct 25, 2024

Merged the PR. Thanks!

@Xeraster
Copy link

Xeraster commented Aug 15, 2025

If you are interested I could also add the Godot C# setup to the Readme

Is it still possible to provide this? I'm not sure exactly what needs to be done for getting this to work in Godot but based on intuition, the src/Lua folder is probably the one I want. There are a lot of red errors when I do this (in almost every file), mostly just missing using statements but there's a lot of missing methods not directly related to using statements. Fixing this with no direction or hints is going to be quite messy and it would be highly beneficial to see official word on how we are supposed to handle these issues.

Edit: if I go through all the usings and also add <AllowUnsafeBlocks>true</AllowUnsafeBlocks> to my Godot csproj file, it almost works except i get several unfixable errors. It complains that (129,25) and (157,17) of Lua/LuaCoroutine.cs "Feature 'ref and unsafe in async and iterator methods' is not available in C #12. Please use language version 13.0 or greater." And there's also the same error for (216,25) in Lua/Runtime/LuaVirtualMachine.cs. I got it to at least compile by making some ugly changes that might cause errors but I don't know if it's going to work or not.

@Flo12344
Copy link
Contributor Author

@Xeraster I have stopped using it for now and switched back to c++ for my project, but if you want it's on the readme on my fork even if the rest is not up to date

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants