Page 1 of 1
[Scripting] Game crashes if you attempt to implement extension methods
Posted: Sat Jun 08, 2019 8:26 pm
by JakSparro98
If you try to use the so called extension method, to implement new functionality to apply directly on the original object instead of handling it by means of simple static methods, after compiling, the game will simply crash.
I was trying to extend the Vector2 class and this signature below will do the magic:
Code: Select all
public static Vector2 Rotate(this Vector2 v, float degrees) {}
Re: [Scripting] Game crashes if you attempt to implement extension methods
Posted: Sun Jun 09, 2019 6:27 am
by Sree
Extension methods in c# should be written within their own static wrappers.. ie within a static class that is not wrapped around by another class, which is not possible in scripting
Re: [Scripting] Game crashes if you attempt to implement extension methods
Posted: Sun Jun 09, 2019 12:52 pm
by JakSparro98
Sree wrote: ↑Sun Jun 09, 2019 6:27 am
Extension methods in c# should be written within their own static wrappers.. ie within a static class that is not wrapped around by another class, which is not possible in scripting
I wanted to try anyway by assuming that, since there is a "global wrapper" inside the script section, it could be a static class, but I wasn't sure it could work since from the beginning.
But I was far from thinking it could crash the entire game, you can NOT justify a crash with being unable to implement an unexpected piece of code.
Re: [Scripting] Game crashes if you attempt to implement extension methods
Posted: Sun Jun 09, 2019 3:04 pm
by Sree
JakSparro98 wrote: ↑Sun Jun 09, 2019 12:52 pm
I wanted to try anyway by assuming that, since there is a "global wrapper" inside the script section, it could be a static class
that makes no sense but I don't see the point of fixing something when you're gaining nothing from it, fixing it still wouldn't let you implement extensions, so why bother lol
Re: [Scripting] Game crashes if you attempt to implement extension methods
Posted: Sun Jun 09, 2019 6:22 pm
by Gurt
The crash will be fixed after v.1.2.0g and you will correctly see the compilation error "CS1106 Extension method must be defined in a non-generic static class".
For extensions in the ScriptAPI:
You can't defined your own top-level static class in SFD today and therefor you can't create extension. I'm not sure if I will add it at all. For now you will have to create your own helper functions inside your script.
Code: Select all
public static class V
{
public static void Rotate(ref Vector2 vector, float radians)
{
// Operations in here
}
}
...
Vector2 testy = Vector2.Zero;
V.Rotate(ref testy, 20f);