-1

so I have been developing a basic game in Godot (I am a beginner) I have an enemy scene and a level scene I want to add a lot of enemies to my level scene one way to do that is to attach the enemy scene as a child of the level scene and duplicate it as much as I want but is that the eight methods is there a better way that will give better performance to the game

this is the way I know

level
-enemy
-enemy1
-enemy2
-enemy3

this is what I want to achieve

level
-a group of enemies
Abhiram
  • 145
  • 1
  • 1
  • 13

1 Answers1

0

You seem to be part of a crowd that is arriving to Godot but wanting to do everything by code.

If your goal is performance, then yes, there might be a way: It is using VisualServer and probably either PhysicsServer or Physics2DServer (for 3D and 2D respectively), and perhaps some other similar classes. With that a single node will handle the equivalent of thousands of instances with better performance.

See: Optimization using Servers.

But:

  1. The code would be very specific to what you are doing. Without knowing how your enemy scene and code looks like, I cannot properly guide you to do it.
  2. Because of the above, it is hard to iterate. It is better to design your game with multiple scenes and instance first.
  3. Common optimization advice applies: Measure the performance (Godot has a profiler), and work on the bottlenecks first. So, only if it turns out this is a performance problem, rewrite it with VisualServer et.al.
  4. This is not an approach intended for beginners. You would be bypassing most of Godot usability to write code at a lower level in the name of performance.

So, rule of thumb: do you really need thousands of instances, or do you need a handful? If you need those thousands of instances then do it first the normal Godot way, and once your design is done, port it to use VisualServer et.al.


I suppose there is another reason you might want to optimize this aside from having too many instances: perhaps those methods are really CPU intensive. I would not know, since I don't have your code. So, let us think about optimization more broadly.

I remind you that when it comes to optimization:

  • You should first try to get the same results by doing less (cutting out things that are not necessary, and picking better algorithms and data structures).

    Perhaps only allow enemies that are nearby the player to move, and disable those that are away.

    Resist that urge to do everything by code for a minute, and see the classes that Godot provides. You don't need to hard-code your own animations, you don't need to write your own path finding algorithm, you don't need to code your own physics, etc.

  • When you have exhausted that try doing less often (defer execution, initialize lazily, memoize/cache results, break processes so they can be done steadily across multiple frames).

    Perhaps instance the enemies when the player gets nearby. Perhaps use InstancePlaceholder. Perhaps write an instance pool. You might ever recycle enemies that are away from the player.

    Also if the enemies need to do so some complex operations, find something they can use as temporary result, so you can defer the complex operation without making the enemies stop working.

    Perhaps you should look into having a background thread that takes requests from the enemies and executes them serially. So it does not have an impact of frame rate. And perhaps you can hide the fact that your enemies are waiting with some thinking animation.

  • When you have exhausted that, then yes, try doing what you are doing but faster.

    For which there might be another tool: Write your code in C# using the Mono build of the engine, or in C++ using GDNative.

Theraot
  • 31,890
  • 5
  • 57
  • 86