vexe / VFW

MIT License
492 stars 67 forks source link

RequiredComponent attribute doesn't work #68

Closed YacineFenina closed 8 years ago

YacineFenina commented 8 years ago

Hi, I think I missed something with [RequiredFromThis] attribute :

When I use it, it doesn't create a new component or any thing else. I just write : [HasRequirements] upside my class that inherit from BetterBehavior. and [RequiredFromThis(Add = true)] upside my component but when I add my behavior to a gameobject, nothing happens

Did I miss something ?

Thanks in advance ! Yacine

YacineFenina commented 8 years ago

My bad, I've understood my mistake. I didn't see the button in "Tools" ...

vexe commented 8 years ago

Did you turn on automatic requirements resolution? - Basically there's a editor static class with an update function that finds all objects marked with [HasRequirements] and resolves them if auto requirement resolution is turned on. If memory serves, you can toggle it from the menu items Tools/Vexe/Requirements. Try that. Let me know what happens.

[Edit] looks like you found it on your own ^^

vexe commented 8 years ago

The reason I put the resolution code in an external class and not in the drawer, is because if you put it in the drawer that means the code only runs when you have the object selected. That means if you instantiate an object from code that has requirements, it won't have them resolved unless you select the object from the editor.

My personal experience with this system: I never really trust the Unity editor to do my configs and connections, instead I use code to do it, because code is something you can trust. You can step over in the debugger and see how things are getting initialized. It's more predictable and easier to reason about. Mess with the system, see if you like it, see if it's worth it, if yes, and it's working fine for your needs then go ahead and use it!

YacineFenina commented 8 years ago

Yes I found it. I just misunderstood what you intend to do

Now my misunderstunding is, when I create an occurence of an object (in a list) that required component in a BetterBehavior class, it doesn't check if the gameObject get my required component ?

vexe commented 8 years ago

Sorry I'm not sure I fully understand what you mean. Could you rephrase that?

YacineFenina commented 8 years ago

Sorry my english is not really good.

There is my problem : This line => "if (member.Type.IsDefined(true))" you wrote in "Requirements.Resolve" doesn't work for my case : it find a List which can't define "RequiredFromThis" because it's a List. (IComponent is an interface)

Is there a way to check if my variable is a Collection and if true, check the generic type of the list? I think I will find an other issue => it will anyway find an interface and it seems that we can't add an attribute to an interface

vexe commented 8 years ago

Ah you mean you have the requirement attribute on a list/array field. Yeah that's not currently supported. However it could be easily added, for example: check to see if the member is list/array, if it is we check to see if the component already exist, if no we add one, otherwise we don't do anything. See if you can do the modification on your own, ask for help if you need any.

But like I said it would be more easy and robust if you just write the code that does what you want. You could even annotate the function with [Show] and you can click it from the editor, or just put the code in Awake, and annotate your class with [ExecuteInEditMode] if you need it to run in editor. There's many options.

[Show] void AddComponentToList() { ... }
YacineFenina commented 8 years ago

I think I'll do your advice !

Thank you ! :+1: