I'm doing some game with city traffic in BGE. I'd like to spawn driving cars near player using script but I want to give them different Game Logic on spawn dynamically and randomly (for different types of drivers like agressive or calm). Is it possible to import GameLogic from file or object from other .blend file? I know it is possible using bpy but how to do it in bge? Thanks in advance.
1 Answer
Within Blender (and the BGE) the logic is assigned to the game object and can't be exchanged. This means the behavior is fixed assigned to a specific game object.
But there are options to work with that.
Separate objects
Have one object with the behavior and others for representation. This way you can exchange the behavior be assigning another behavior object. You can establish the relationship via parenting.
Composition pattern (Python only)
You object gets fixed high-level behavior. E.g. Perform the driving behavior.
Your code is designed around the composition pattern. For example the car can have a driver. The driver performs the driving behavior. You can have one driver for aggressive driving and one for calm driving etc.. They all share the same interface to the car.
Before driving you assign a specific driver to the car. You can even exchange the driver on-the-fly.
This design fits very well - on a discrete number of driver tpyes - the driver types have not much in common (they are very different) - you want to add further driver types later
The different driver types are Independent of each other.
Parametric
You have one driver's behavior that can be controlled by different parameters e.g. aggressiveness.
This fits well - when there is smooth transition from one behavior to the other. e.g. aggressiveness 0.0 .. 1.0 - when the different behaviors are pretty similar