multiple variables or array?

Get help using Construct 2

Post » Sat Nov 05, 2016 9:14 pm

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.
Hi I am Samuel :)

My google play
B
42
S
10
G
1
Posts: 126
Reputation: 3,278

Post » Sat Nov 05, 2016 9:52 pm

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.
B
46
S
16
G
78
Posts: 2,168
Reputation: 46,353

Post » Sat Nov 05, 2016 10:35 pm

plinkie wrote: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. :D
Hi I am Samuel :)

My google play
B
42
S
10
G
1
Posts: 126
Reputation: 3,278

Post » Sat Nov 05, 2016 10:41 pm

If enemy phase 1, set enemy variables to 250/2. If enemy phase 2, set enemy variables to 300/3
B
46
S
16
G
78
Posts: 2,168
Reputation: 46,353

Post » Sun Nov 06, 2016 12:56 pm

plinkie wrote: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.
Hi I am Samuel :)

My google play
B
42
S
10
G
1
Posts: 126
Reputation: 3,278

Post » Sun Nov 06, 2016 2:15 pm

Correct you would have to create multiple events...so what do you need help with?
B
46
S
16
G
78
Posts: 2,168
Reputation: 46,353

Post » Sun Nov 06, 2016 2:43 pm

[url][/url]
plinkie wrote: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.

Image

Variable for enemys
Image

Image
Hi I am Samuel :)

My google play
B
42
S
10
G
1
Posts: 126
Reputation: 3,278

Post » Sun Nov 06, 2016 2:51 pm

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.
B
46
S
16
G
78
Posts: 2,168
Reputation: 46,353

Post » Sun Nov 06, 2016 3:05 pm

plinkie wrote: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.
Hi I am Samuel :)

My google play
B
42
S
10
G
1
Posts: 126
Reputation: 3,278

Post » Mon Nov 07, 2016 10:21 am

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.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
292
S
115
G
96
Posts: 7,295
Reputation: 70,785

Next

Return to How do I....?

Who is online

Users browsing this forum: Microbex and 20 guests