Arrays problems (getting value with randomization)

0 favourites
  • 7 posts
  • 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 !

  • 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.

  • 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

  • Thanks guys ! I'll have a look at those right now.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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.

  • 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.

  • Allright ! Way clearer now. Thanks a lot :)

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