Slow instantiation of gameobject -help!

NodeCanvas Forums General Discussion Slow instantiation of gameobject -help!

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #11734
    haro2424
    Participant

    Hi

    I’m following a tutorial to create a basebuilding game at the moment. Currently, I’m trying to instantiate 10,000 gameobjects(with only a sprite renderer component attached) in a double for loop. The code is shown below in the image, which creates the 10k tile game objects under 1 second. My attempted translation to NC created the game objects visually one by one… and when it runs, took 2 whole seconds to instantiate just 100 objects. Can I get any tips on how I can optimize this process? Any help is appreciated, thank you.

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

    Hello,

    The reason why this is happening is not about performance, but relevant to how Behaviour Trees work in general.
    Behaviour Trees are only updated (Ticked) once per frame. So this basically means that each task’s (action/condition) Execute or Update method will have the chance to be called at most once per frame as well. This is just how BTs work.
    So in your BT image above, what is happening is that only one instantiation is taking place per frame too, which also makes sense of the math you provided, in that 100 objects are instantiated within 2 sec, with 2 sec. being 120 frames more or less.

    For your case, I’d really like to suggest that you instead simply create a custom Action Task to perform the for-loop tile instantiation and optionally use parameter too.
    Just a small example to clarify:

    With such a self-contained action you can now also use it as a modular block of code within any BT (or FSM), instead of visual scripting a for-loop with a BT.
    Let me know if that helps.
    Thanks!

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

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.