Arrays problems (getting value with randomization)

Get help using Construct 2

Post » Sun Jan 26, 2014 9:49 pm

Hi everyone, I've checked, probably 5 or 6 arrays tutorials and also all the arrays related stuff in the "How do I" topic by Kyatric but I haven't found the solution for my problem.

I have an 2D array filled with monsters. Here's an example :

(0,0) "Blue Slime" //name
(0,1) 1 // level
(0,2) 3 // HP
(0,3) 1 // XP

(1,0) "Red Slime
(1,1) 1
(1,2) 5
(1,3) 3

I also have a monster sprite will all animation frame setted to 1 monster visual.
Frame 0 for Blue slime and Frame 1 to red slime here.

I want to spawn one any of the level 1 monster in my array. In this case both Blue Slime and Red Slime can be spawned since they are both level 1. To have the right visual I want to changed the animation frame of the sprite related to the monster.

So, how can I find all Level 1 monsters in my array (for instance I have 89 monsters in it) and choose one of them at random and spawn it by switching my sprite animation frame related to the monster chosen?

Thanks !
B
8
S
1
Posts: 22
Reputation: 1,148

Post » Mon Jan 27, 2014 2:16 am

Here is an example. The meat of it is the function "Populate"

https://www.dropbox.com/s/gosbr09liscl1e7/pickArray.capx

If you need help with the random picking, let me know. But now you have an array that is populated with all level 1 monsters, you can just pick a random one.

ArcadEd2014-01-27 02:25:52
B
101
S
32
G
12
Posts: 1,549
Reputation: 21,993

Post » Mon Jan 27, 2014 3:43 pm

I completed @ArcadEd example with another function to retrieve the index of a random monster of the wanted level.

You can then store that index in a local variable to get all the info you want on that monster !

https://dl.dropboxusercontent.com/u/7004246/pickArray.capx
B
76
S
31
G
27
Posts: 1,015
Reputation: 20,790

Post » Tue Jan 28, 2014 2:21 am

Thanks guys ! I'll have a look at those right now.nordentipwel2014-01-28 02:21:15
B
8
S
1
Posts: 22
Reputation: 1,148

Post » Tue Jan 28, 2014 4:21 am

Thanks again guys, I've tried this and that works perfectly.
@Magistross, there's one thing I don't understand though.

After the "for each" on the array, you have another condition to check if Array.CurX, 1) = Function.Param(0)

I understand what it does but I don't understand how this works with the for each condition.
B
8
S
1
Posts: 22
Reputation: 1,148

Post » Tue Jan 28, 2014 1:48 pm

It's an iteration on the X axis only. So for each X (or monster in our case), check if its level (y = 1) is equal to the first function parameter (desired level). If it is, I add the current index to the list of candidates.

I could have done a normal for loop, but since the array was set in such a manner, using the built in for each X axis was easier at that time.Magistross2014-01-28 14:26:27
B
76
S
31
G
27
Posts: 1,015
Reputation: 20,790

Post » Tue Jan 28, 2014 2:40 pm

Allright ! Way clearer now. Thanks a lot :)
B
8
S
1
Posts: 22
Reputation: 1,148


Return to How do I....?

Who is online

Users browsing this forum: SEGamestudio and 4 guests