DeMaCS-UNICAL / ThinkEngine

A Unity asset for a tight integration of AI in games: asset, showcase, web page.
19 stars 1 forks source link

NullReferenceException inside Generated Sensor Scripts #30

Closed Farfi55 closed 1 year ago

Farfi55 commented 1 year ago

Example generated code:

public override void Update()
{
    if(!ready)
    {
        return;
    }
    if(!invariant || first)
    {
        first = false;
        KOContainerSensorData KOContainerSensorData0 = gameObject.GetComponent<KOContainerSensorData>();
        KitchenObjectASP FirstKitchenObject1 = KOContainerSensorData0.FirstKitchenObject;
        string Name2 = FirstKitchenObject1.Name; // THIS LINE CAUSES NullReferenceException
        if(Name2 == null)
        {
            values.Clear();
            return;
        }
        else
        {
            if (values.Count == 200)
            {
                    values.RemoveAt(0);
            }
            values.Add(Name2);
        }
    }
}

in this case FirstKitchenObject1 should also be checked for null and if so handle it similarly as when Name2 is null

Farfi55 commented 1 year ago

maybe related, when generating new Template a NullReferenceException is thrown inside RetrievePropertyByType because `currentType' is null.

from my debugging I think this problem has origin inside RetrieveProperty since it can't find the type of the property

private static void RetrievePropertyByType(string currentProperty, ref Type currentType)
{
    // NullReferenceException here
    MemberInfo[] members = currentType.GetMember(currentProperty, Utility.BindingAttr);
    if (members.Length > 0)
    {
        FieldOrProperty fieldOrProperty = new FieldOrProperty(members[0]);
        currentType = fieldOrProperty.Type();
    }
    else
    {
        currentType = null;
    }
}
deniseAngilica commented 1 year ago

I don't think it's related. Can you open a new issue describing the sensor that causes the nullreference please?