Dear forum users! In compliance with the new European GDPR regulations, we'd just like to inform you that if you have an account, your email address is stored in our database. We do not share your information with third parties, and your email address and password are encrypted for security reasons.

New to the forum? Say hello in this topic! Also make sure to read the rules.

[Scripting] Game crashes if you attempt to implement extension methods

Here you can find all solved gameplay problems and bugs (beginning from Pre-Alpha 1.8.8).
Forum rules
By using the forum you agree to the following rules. For this forum you also need to follow these additional rules.
Locked
User avatar
JakSparro98
Superfighter
Superfighter
Posts: 477
Joined: Fri Jul 15, 2016 7:56 pm
Started SFD: PreAlpha 1.0.5
Location: Rome, Italy
Gender:
Age: 20

[Scripting] Game crashes if you attempt to implement extension methods

Post by JakSparro98 » Sat Jun 08, 2019 8:26 pm

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) {}
1 x

User avatar
Sree
Superfighter
Superfighter
Posts: 321
Joined: Sun May 08, 2016 8:19 pm
SFD Account: phasmic
SFD Alias: sree
Gender:
Age: 19

Post by Sree » 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
0 x

User avatar
JakSparro98
Superfighter
Superfighter
Posts: 477
Joined: Fri Jul 15, 2016 7:56 pm
Started SFD: PreAlpha 1.0.5
Location: Rome, Italy
Gender:
Age: 20

Post by JakSparro98 » Sun Jun 09, 2019 12:52 pm

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.
0 x

User avatar
Sree
Superfighter
Superfighter
Posts: 321
Joined: Sun May 08, 2016 8:19 pm
SFD Account: phasmic
SFD Alias: sree
Gender:
Age: 19

Post by Sree » Sun Jun 09, 2019 3:04 pm

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
0 x

User avatar
Gurt
Lead Programmer
Lead Programmer
Posts: 1590
Joined: Sun Feb 28, 2016 3:22 pm
Title: Lead programmer
Started SFD: Made it!
Location: Sweden
Gender:
Age: 29

Post by Gurt » Sun Jun 09, 2019 6:22 pm

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);
1 x
Gurt

Locked