multiple variables or array?

  • Hello friends!

    I have the following question.

    I have several enemies in the game, each has its speed, spawn time, attack time and etc ...

    What is more correct:

    1 - Each enemy has its global variables.

    2 - Create an array for each attribute of the enemy.

  • Neither, each enemy can have its own instance variables. If enemies share the same instance variables you can put them into an enemy family and use enemy family instance variables.

  • Neither, each enemy can have its own instance variables. If enemies share the same instance variables you can put them into an enemy family and use enemy family instance variables.

    The problem is that the values change. For example, in the first stage enemy1 has 250 speed and 2 of life. When you arrive in phase 2 this same enemy will have 300 speed and 3 of life. If I put the variable inside the own enemy I can not do this, it will always be created with the initial value which is 250 speed and 2 of life. Thanks for answer.

  • If enemy phase 1, set enemy variables to 250/2. If enemy phase 2, set enemy variables to 300/3

  • If enemy phase 1, set enemy variables to 250/2. If enemy phase 2, set enemy variables to 300/3

    For this I would have to create multiple IFs to check phase or create an event for each phase with the appropriate parameters, I think so the code would not be optimized.

  • Correct you would have to create multiple events...so what do you need help with?

  • Correct you would have to create multiple events...so what do you need help with?

    I want to make my code less. Today I use global variables to store information, but for that I need to create many variables in clearing my code is quite small. I thought about creating an array for each enemy, the array would contain the information for each level of the enemy. Just do not know if that would be the right thing to do dare to have better options.

    Sorry, but the code is in my language. :/

    code for enemy creation.

    Variable for enemys

  • Yeah you just use enemy instance variables not global variables. On enemy 1 you have health, speed etc. You could also have a phase variable on the enemy. Then in your code you set if phase is 1, set all the variables to this. If phase is 2, set all variables to this. It looks like you are setting the instance variable values to the value of a global variable, I would base the values on what the enemies are doing and what phase they are in. They will have a default and then anything after that can be based on phase.

  • Yeah you just use enemy instance variables not global variables. On enemy 1 you have health, speed etc. You could also have a phase variable on the enemy. Then in your code you set if phase is 1, set all the variables to this. If phase is 2, set all variables to this. It looks like you are setting the instance variable values to the value of a global variable, I would base the values on what the enemies are doing and what phase they are in. They will have a default and then anything after that can be based on phase.

    I got it. I'll get it then. Thank you for your help.

  • Having an array to hold "initialisation variables" per phase sounds correct as well.

    In fact, you would pick the value from the array on creation of the enemy and fill their instance variable with it, accordingly to the current phase (you would then use an array with at least two dimensions, X being the phase, Y being each of the variables required).

    To store those kind of data, an array is one of the available data structure.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Having an array to hold "initialisation variables" per phase sounds correct as well.

    In fact, you would pick the value from the array on creation of the enemy and fill their instance variable with it, accordingly to the current phase (you would then use an array with at least two dimensions, X being the phase, Y being each of the variables required).

    To store those kind of data, an array is one of the available data structure.

    Yeah, that's what I thought. I know the performance would not change anything, just wanted to make it organized. After many tests I kept the variables on each enemy and created global variables to do the updates. And for me not to get lost in the future I created an event just for the variables of the enemies.

    Thank you for your help.

  • Having an array to hold "initialisation variables" per phase sounds correct as well.

    In fact, you would pick the value from the array on creation of the enemy and fill their instance variable with it, accordingly to the current phase (you would then use an array with at least two dimensions, X being the phase, Y being each of the variables required).

    To store those kind of data, an array is one of the available data structure.

    Are there any good array tutorials you'd recommend? I'm looking back into tutorials in order to figure out save data and usage of arrays. I could never find a good array tutorial video. And your tutorials are great, so... make more? Thanks

  • YoHoho: Thanks.

    I often use arrays within my tutorials, but I don't have a dedicated tutorial for arrays, or a tutorial that uses the array in such a way as described in this topic.

    I have made several capx examples in the past, they surely can be found in the How do I FAQ probably in the "Array" category, but no video tutorial I can remember of at this time.

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