Setup an Instantiated "child" UnityObject

FlowCanvas Forums Support Setup an Instantiated "child" UnityObject

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2043
    5tarkaster
    Participant

    First of all, this is a great tool, I am a software developer by day and I see more than enough code to fill my needs at work. To be able to go home and essentially draw out logic in pretty neat blocks is a nice change from the grind of line by line logic. I know full well that I could be more productive with written code, but I guarantee it won’t be half as fun, which is, after all, what making games is all about.

    With that out the way, here’s my problem. I have a BasicTurret which searches for a nearby enemy and once found sets the $Target variable. And as turrets do, it shoots at the target. The problem is that I cannot seem to send the transform of the target to the Bullet after the object is instantiated, the bullets are there in the game world, they just don’t know where they’re going. I found a tutorial which showed the method of sending the value through an event but the event doesn’t even get fired. I am pretty sure (because it doesn’t work) that I am doing something embarrassingly wrong.

    In essence, what is the intended/preferable method to send variable data from one UnityObject to another UnityObject? Additionly how to send variable data to an instantiated UnityObject withing the same script.

    The coding way to do this is sorta like so:

    Attachments:
    You must be logged in to view attached files.
    #2046
    net8floz
    Participant

    Events can be cool but this seems like a bit weird time for events. At the very simplest translation you can do exactly your code version in flow canvas. Expanding on this I like to build kind of “Controller” classes that handle code for me but I *assemble* them in flow canvas. This gives me powerful control of my code in my controllers while giving me the excellent visual design logic. So I might I have [OnInput] => [Self.MyComponent.Shoot()] instead of building longer flow trees.

    Example of code translation below.

    Edit:
    It occurs to me that you’re not wanting to invoke a function as per your code but execute additional flow controller logic. In this case events or a custom function does make sense!

    Attachments:
    You must be logged in to view attached files.
    #2048
    5tarkaster
    Participant

    Thanks net8floz for the reply, I managed to get a decent enough work around for the problem. I took this as an opportunity to delve into custom nodes. I made a custom SpawnPrefab node which takes in all the standard variables but allows you to setup Blackboard values dynamically. I have added the code below for completion sake.

    Although I have to admit I am still confused as to why the standard flow script method doesn’t actually work. It seems to me that there may be something wrong with the Instantiate method where its not properly returning the instantiated object. Regardless, what I needed was to set Blackboard variables the moment the object is instantiated. The reason for that is because on Update the spawned bullet will check if its Target is null and destroy itself. I dont think running a method to setup the variable would of solved that minor complication.

    Without any further ranting, below is my node and attached is a show of it in use.

    Attachments:
    You must be logged in to view attached files.
    #2051
    Gavalakis
    Keymaster

    Hello guys,

    Thank you @5tarkaster. I am glad you enjoy FlowCanvas 🙂

    If you take a look at the Instantiate node in your image, you will notice that it has an “X” and is highlighted in red. This means that there was an error in it’s execution, and most probably you got this error logged in your console:
    InvalidCastException: Cannot cast from source type to destination type..

    What is happening here, is that UnityEngine.Object.Instantiate, returns a UnityEngine.Object, which is then connected to the SendEvent node “Target”, which takes a GraphOwner type. This should normally work by automatically casting the object, but it’s a bug I have to fix as it seems :/
    A solution around that, would be to use the generic “Instantiate (T)” node. This should be the first node that shows up in the node browser when you search for “instantiate”. Using the generic “Instantiate (T)” in your example, will work without any problems, but it’s also generally better to use the generic nodes/methods where available as well.

    InstantiateT

    Creating custom nodes like you did, is always another solution of course 🙂

    Join us on Discord: https://discord.gg/97q2Rjh

    Attachments:
    You must be logged in to view attached files.
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.