NodeCanvas Forums › Support › Set Property on an instantiated GameObject
Hey,
I have just recently purchased NodeCanvas and I am toying around a little with the possibilities. Before I started working with NodeCanvas I used my own FSM implementation that worked with Dependency Injection and because it was all custom made, I could easily inject whatever I wanted on GameObjects I instantiated through the FSM.
One thing I like working with, is having a statemachine that dictates the flow of the game. So in this case (with NodeCanvas) I was experimenting with a small setup: Init -> Splash screen(s) -> Main Menu.
I got this working as it seems NodeCanvas is very flexible, I did encounter something of which I am not sure if I am doing this right, and that is working because of perhaps a bug. So the situation is as following:
In the Main Menu Node, I call the Instantiate Game Object action which spawns the Main Menu prefab. On this Prefab I have a script, ButtonEventSender.cs, which simply sends an event to the FSMOwner that would allow me to transition to another node (start game, options, etc). The script looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class ButtonEventSender : MonoBehaviour { public FSMOwner Owner { get; set; } public string EventName { get; set; } [SerializeField] private Button eventButton; protected void OnEnable() { eventButton.onClick.AddListener(OnButtonClicked); } protected void OnDisable() { eventButton.onClick.RemoveListener(OnButtonClicked); } private void OnButtonClicked() { if (Owner != null) { Owner.SendEvent(EventName); } } } |
The Main Menu is instantiated and the clone is set to a DynamicVar: _MainMenu (as it will only be relevant for that Node). After instantiation I set the 2 properties (Owner and EventName in the ButtonEventSender script) to that of the current FSMOwner and the name of the event I want to fire. (Both the prefab and the Event string are on a blackboard)
Now I got this working, but it was a bit of a hit and miss getting the properties from the ButtonEventSender script. Sometimes they’re there and sometimes they’re not. I think this was due to the fact that I initially targeted the Main Menu prefab (instead of the DynamicVar) and then changed it to DynamicVar and the properties were still in memory or something? I’m not sure.. I’m not even sure if this is the way to get this done to be honest.
This is the node:
So if this is the way, is this a bug? Or am I using it incorrectly? If I am using it incorrectly, what would be the correct way of doing this? I can probably create a custom task/action for instantiating GameObjects with some prerequisites, but wanted to stick to the built in stuff first.
Thanks in advance!
I have been playing around and it seems to be a bug (or a happy coincidence in my case).
If I first assign the prefab (which is on the blackboard) on which the property I want to set resides and then change it to a dynamic var, it initially allows it, but as soon as I press play (or re-validate the graph), the dynamic var changes back to the prefab on the blackboard. I have to do this little dance of changing the target, close the graph editor window, save and re-open it to have the dynamic var stick.
I guess it works because it’s the same property, on the same script, but on the instance of the prefab, instead of the prefab itself.
I’m guessing NodeCanvas isn’t really designed for this specific case. I figure there are multiple ways to achieve what I want to do, but I was hoping this would be an option to allow some more “on the go” spawning of objects and setting some variables on the instance, without manually looking up stuff in the scene.
Perhaps it is still possible (to a certain degree), but I just haven’t properly figured it out yet. 🙂
Hello and thank you for your detailed information.
I tried replicating the issue but the setup is a bit tricky to reproduce unfortunately.
Is it possible for you to please send me a small reproduction project (eg the example shown above in your post) so that I can see what exactly could be wrong or a bug? (support_AT_paradoxnotion.com).
By the way, have you tried using non-dynamic variables instead? Does the issue persist on your end without dynamic variables?
Thank you!
Join us on Discord: https://discord.gg/97q2Rjh