Skip to content

Commit

Permalink
Merge pull request #78 from crashkonijn/feature/get-component-in-parent
Browse files Browse the repository at this point in the history
Added GetComponentInParent to references
  • Loading branch information
crashkonijn authored Oct 4, 2023
2 parents 8de7bc7 + 7e533f2 commit ef8b93b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ private object GetPropertyValue(PropertyInfo property)
if (property.GetCustomAttributes(typeof(GetComponentInChildrenAttribute), true).Any())
return this.GetCachedComponentInChildrenReference(property.PropertyType);

if (property.GetCustomAttributes(typeof(GetComponentInParentAttribute), true).Any())
return this.GetCachedComponentInParentReference(property.PropertyType);

return null;
}

Expand Down Expand Up @@ -90,5 +93,20 @@ public T GetCachedComponentInChildren<T>()
{
return (T)this.GetCachedComponentInChildrenReference(typeof(T));
}

private object GetCachedComponentInParentReference(Type type)
{
// check if we have a reference for this type
if (!this.references.ContainsKey(type))
this.references.Add(type, this.agent.GetComponentInParent(type));

// get the reference
return this.references[type];
}

public T GetCachedComponentInParent<T>() where T : MonoBehaviour
{
return (T)this.GetCachedComponentInParentReference(typeof(T));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace CrashKonijn.Goap.Classes.References
{
[AttributeUsage(AttributeTargets.Property)]
public class GetComponentInParentAttribute : Attribute
{
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ T GetComponentInChildren<T>()

T GetCachedComponentInChildren<T>()
where T : MonoBehaviour;

T GetCachedComponentInParent<T>()
where T : MonoBehaviour;
}
}

0 comments on commit ef8b93b

Please sign in to comment.