How best to randomly populate a UI with sprites

In the app that I am currently experimenting with I would like to display a random subset of 20 sprite animations out of a possible 100+. The way I have gone about it thus far is this

  • I have a root level images folder with subfolders holding the individual sprite imagery for each of the 100+ animations.

  • A second root level atlases folder is being used to store atlases - at the moment only one in which I create multiple animation groups that refer to the appropriate images/animation_subfolder/image***.png

  • A third root level prototypes folder is being used to store GO prototypes. At present I have just the one prototype there with one sprite object and a script to animate it

  • In main.collection in addition to a background game object with a suitable sprite I have another game object containing a factory that points to the prototype created above

  • The script sibling of that factory has the following code

      function init(self)
    factory.load("#factory",afterLoadFactory);
     end
    
    function afterLoadFactory(this,factoryName,result)
          local gameWidth = tonumber(sys.get_config("display.width"))
          local gameHeight = tonumber(sys.get_config("display.height"))
          for i=1, 20 do
              local x1 = math.random(gameWidth)
              local y1 = math.random(gameHeight)
              local pos = vmath.vector3(x1, y1, 1)
              local id = factory.create("#factory", pos, nil,attribs, 1)	
              local anim = "AnimOne"
              if (i % 2 == 0) then anim = "AnimTwo" end
              msg.post(msg.url(nil, id, "sprite"), "play_animation", { id = hash(anim) })
      end
    

    end

where AnimOne and AnimTwo are animation sequences defined in the atlas created earlier. This works - I get 10 AnimOne sprites and 10 AnimTwo sprites on my screen when I run this.

My questions

  1. Does this seem like a reasonable approach
  2. At a later stage I would like to be able to use not just one factory but one of many factories to spawn the UI content based on several criteria. While it does not seem inpossible to have multiple factories within main.collection it is not clear to me that doing so does not entail imposing an unwarranted memory burden on the system

Finally two unrelatatedquestion -

  • I assume that the id returned by factory.create is an autogenerated affair that is guaranteed to be globally unique?
  • Can I reference a dynamically created sprite in this way via its ID and assign it one or more additional attributes? In my case I would like to “tell” it about other, friendly, sprites that have been generated in its neighborhood by communicating their IDs as they get created by the factory.

Yes

Sure. Send info via messages or track all instances in a Lua module and provide functions to get nearby instances.

What makes you think it is expensive memory wise?

It’s not really expensive to have multiple factories

Thanks. What I was thinking of when I wondered about the memory overheads was this - each factory would use its own prototype which in turn would dip into its own atlas to instantiate all of the sprites it animates. So each atlas would get loaded and consume memory.

So suppose I have N factories but at any given time only N/5 of them are in active use to render sprites on screen. The other 4N/5 factories are not placing a burden on the system because their associated atlases have been loaded? Presumably not until factory.load has been called for that factory.

  • But subsequently if that factory is not one of the N/5 that are in use it is still loaded and consuming resources?
  • Perhaps one must make it a point to call factory.unload when it is no longer required?
  • Or is it best practice to simply unload a factory just as soon as one is finished with creating all the objects that one wants out of it?

Ah, ok, yes I see what you mean.

Yes, I think this must be done to unload the texture. @JCash can you confirm this?

Yes, if you have the Load Dynamically flag set, you can call the factory.unload() and collectionfactory.unload() to unload its resources.