Array of sprites

0 favourites
  • 7 posts
From the Asset Store
Advanced inventory mechanics for your RPG game (Array-based). Take Items, split them, pick up them, read the description
  • Hi everybody.

    I've tried searching in forum and in the manual, but I wasn't able to find what I was searching for...

    Is there a way to have an array of sprite? Or, to change the perspective, is there a way to choose which element of a family has to be created?

    My goal is to have an array of "template" and then being able to choose which one create... in pseudo code it will be something like:

    i = Random(0,10)

    SpriteToCreate = ArrayTemplate(i)

    Can you suggest a way or a plugin that can do for me?


  • You can set a variable from 0,10 then have sub-events like:

    var==0 -> spawn a

    var==1 -> spawn b


    I just checked the manual and found this also,


    Create object

    Create a new instance of an object type on a layer at a given position. If a Family is chosen, a random object type from the family is picked, and an instance created for that.

  • Thanks for suggestion, Davioware, but that's not what I was searching for :)

    As you said, creating a new istance of a Family, a random object will be created, but you don't know which one.

    About the first solution, that is for sure the most straight one, but also the less "pure" one :) As a matter of fact, if you use that method, each time you add a sprite to your project for any reason (e.g. new release with new sprites, or different sprites depending of user, or whatever) you must add a new condition to the "switch case" and this will slow your algorithm, instead of picking the i-th istance of an array...

    However, if there is no other solution, I will of course pick that one, thank you :)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • You would have to add a new event with each new sprite you want, but how is that different than adding a new entry to the array?

    If you're concerned that picking the i-th slot in an array would be marginally faster than checking a variable x amount of times you can go a bit further to "optimize it" to be more array like. Note the use of quotations to signify that it may or not be any faster. It might even be slower. Here's how:

    you can use the function object, and have calls like:

    on function "arrayslot1" -> spawn a

    on function "arrayslot2" -> spawn b


    then, to create your random object, or object of choice, you go:


    call function "arrayslot"&i

    (& is the concatenation operator in construct)

    This eliminates the need to check a variable x amount of times.

  • Optimisation: don't waste your time.

    Apart from the performance tips nothing really impacts performances code-wise.

    Of course if you use major collision checks and physics use, it will have an impact, but as far as creating sprites from a loop, not that much.

  • Here's one way. Add the UIDs to the array and then Pick from a random index into the array.


    Edit: Doh! I answered the wrong question. This helps for picking, not creating. Sorry. I don't see any way other than what has already been discussed. Each sprite has to be created explicitly.

  • Thanks everybody for suggestions: maybe it's just my "old school programming head" that is worring me too much.

    Thanks a lot.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)