YarnSpinnerTool / YarnSpinner-Unity

The official Unity integration for Yarn Spinner, the friendly dialogue tool.
MIT License
491 stars 85 forks source link

YarnFunction-tagged methods that take converted types fail type checking #238

Open desplesda opened 1 year ago

desplesda commented 1 year ago

What is the current behavior?

YarnFunction-tagged methods that take converted types do not type check correctly.

Please provide the steps to reproduce, and if possible a minimal demo of the problem:

The following Yarn Function will produce import errors when compiling:

[YarnFunction("test_function")]
public static bool TestFunction(GameObject gameObject) {
   return true;
}

While this function is understood by the runtime (a call to TestFunction("Player") would result in the first parameter being passed as any GameObject in the scene with the name "Player"), the conversion from GameObjects to strings is not made known to the type checker. This results in the following type checking error: Function test_function cannot be used in Yarn Spinner scripts: parameter gameObject's type (UnityEngine.Game0bject) cannot be used in Yarn functions.

What is the expected behavior?

The type checker should be informed that, in the context of Yarn scripts, the parameters of functions like this always have valid Yarn types.

Please tell us about your environment:

This error was reported in Yarn Spinner v2.3.0-beta2 by Discord user Tay.

desplesda commented 10 months ago

Looked into this. One way we can do this is to make the actions source code generator wrap all functions that take Unity type (like Component or GameObject) with a thunk that takes a string in place of the Unity type (and performs the work of converting from string to the Unity type, e.g. via a GameObject.Find).