Skip to content

Instantly share code, notes, and snippets.

@TheHelpfulHelper
Created January 15, 2024 01:31
Show Gist options
  • Save TheHelpfulHelper/e94c44764e1bf9e73d24e52f2983431d to your computer and use it in GitHub Desktop.
Save TheHelpfulHelper/e94c44764e1bf9e73d24e52f2983431d to your computer and use it in GitHub Desktop.
U# Fake Custom Classes Pattern
// 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