Created
January 15, 2024 01:31
-
-
Save TheHelpfulHelper/e94c44764e1bf9e73d24e52f2983431d to your computer and use it in GitHub Desktop.
U# Fake Custom Classes Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NOTE: The current U# compiler will log an error, it however still compiles and functions correctly! | |
public class MyCustomClass : DataDictionary | |
{ | |
public static MyCustomClass ctor() // you can call this whatever you want, New(), Create(), or here ctor() | |
{ | |
var instance = (MyCustomClass)new DataDictionary(); | |
instance["player"] = new DataToken(Networking.LocalPlayer); // Example of something you might want to do in the constructor | |
return instance; | |
} | |
} | |
public static partial class Extensions | |
{ | |
// Instance methods must be written as extensions, because of U# limitations | |
public static VRCPlayerApi GetPlayer(this MyCustomClass instance) | |
{ | |
return (VRCPlayerApi)instance["player"].Reference; | |
} | |
public static void LogPlayerName(this MyCustomClass instance) | |
{ | |
Debug.Log(instance.GetPlayer().displayName); | |
} | |
} | |
// Example use | |
MyCustomClass custom = MyCustomClass.ctor(); | |
custom.LogPlayerName(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment