0
Fixed

AOT Stubs Namespacing issue with Singleton

serialkamikaze 2 years ago updated by Lazlo Bonin (Lead Developer) 2 years ago 7

I have some problems with the AOT Pre-Build. It was working on Bolt 1.3.0 and Unity 2017.4.3f1 + .NET 3. And I haven't changed any scripts after it as I have used only Bolt's Flow or State Machines.

Now using Unity 2018.2.1f1, Bolt 1.4.0f5, .NET 3

Now when I build the AOT, every one of my singleton using "managers" results in compile error and the generate AotStubs needs to be deleted to continue working or trying again to AOT build.

Here is one e.g where the problem is my PlayerFigureManager.

Error 1 on console:
Assets/Ludiq/Ludiq.Core/Generated/AotStubs.cs(4804,35): error CS0311: The type `PlayerFigureManager' cannot be used as type parameter `T' in the generic type or method `Ludiq.Singleton<T>'. There is no implicit reference conversion from `PlayerFigureManager' to `Ludiq.ISingleton'
Error 2 on console:
Assets/Ludiq/Ludiq.Core/Generated/AotStubs.cs(4804,66): error CS0117: `Ludiq.Singleton<PlayerFigureManager>' does not contain a definition for `Instance'

Here is the actual PlayerFigureManager class

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerFigureManager : Singleton<PlayerFigureManager> {
 
 public bool IsBought(int index) {
 // real logic here
 return false;
 }
 
 public bool Buy(int index) {
 // real logic here
 return true;
 } 
}

And here is the singleton class:

using UnityEngine;
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
    private static T instance;
    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                var t = typeof(T);
                instance = (new GameObject(t.Name)).AddComponent<T>();
            }
            else if (instance != FindObjectOfType<T>())
            {
                Destroy(FindObjectOfType<T>());
            }
            DontDestroyOnLoad(FindObjectOfType<T>());
            return instance;
        }
    }
}

Any idea why this does happen now with newer Bolt (and Unity) ? And how I might solve this issue?

Bolt Version:
Unity Version:
Platform(s):
Scripting Backend:
.NET Version (API Compatibility Level):
GOOD, I'M SATISFIED
Satisfaction mark by serialkamikaze 2 years ago
Pending Review

Hi serialkamikaze,

Sorry you're having this issue, it seems like a regression introduced while fixing AOT stubs for extension methods.

Can you paste the method in AotStubs.cs with the line where you get this error?

Hello Lazlo,

Here are the lines 4906-4913 from the AotStubs.cs:

// PlayerFigureManager.Instance
[UnityEngine.Scripting.PreserveAttribute()]
public static void PlayerFigureManager_Instance()
{
PlayerFigureManager accessor = Singleton<PlayerFigureManager>.Instance;
Ludiq.StaticPropertyAccessor<PlayerFigureManager> optimized = new Ludiq.StaticPropertyAccessor<PlayerFigureManager>(default(System.Reflection.PropertyInfo));
optimized.GetValue(null);
}

I see, this seems like a namespacing issue. The AOT stubs appears to be looking for Bolt.Singleton instead of your own custom global::Singleton class. I'll try to fix that, but in the mean time, it can be avoided by putting your singleton class in a namespace.

Thanks! Creating a namespace did get it working for me.

Fixed in Next Version

This will be fixed for v.1.4.0f6, all type references in AOT stubs will now explicitly specify the global:: prefix.