I would like my enemy characters to occasionally drop random powerups. Currently I have two built: powerupHealth.go and powerupBomb.go (several more planned). Does my enemy gameobject need separate factories for each type of powerup, or is there a way to spawn different gameobjects dynamically?
Probably powerup Health and powerupBomb differ only by a Sprite image and the property they modify for the player, right?
I can propose to you to create a single powerup game object, where you can in the code have a game object property that will specify it kind - health, bomb, etc.
In the code then you can change Sprite’s animation in init and when player picks up the powerup, send to him a message with what kind of powerup was picked.
Having 1 factory per powerup type and then dynamically modifying the factory url when creating the object seems pretty clean & simple (even though you need 1 factory per powerup type).
But it depends on how different/specific your powerups are, if they use the same powerup script or not etc.
Anyway:
local random = rnd.range(1,nb_powerup_types)
factory.create("/powerup#factory_"..random, pos, nil, param)
/powerup#factory_1 would spawn powerupHealth.go. /powerup#factory_2 would spawn powerupBomb.go.