How do I select a number from array

0 favourites
  • 10 posts
From the Asset Store
Array Editor
$1 USD
80% off
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • I have a procedurally generated map done with an array, 0=filled tile, 1=empty tile

    Now I want to generate enemies (yes again enemies) in the empty tiles, wich are the floor

    I tried lots of things but cant figure out how to do it

    I know how to spawn enemies in every empty tile, filling the hole map, but I just want to create at random locations and a certain number of them

    The non-proffesional way to do it is creating N sprites and re-positioning them until they are not overlaping the tilemap, but i want the first solution

    I wonder it's by choosing random cells from the array and creating there the sprites, but dont know how

    Thanks again!

  • If you have approximately 100 empty tiles, and want about 10 enemies, then loop through the array and give empty tiles a 10% chance of having an enemy. (use Compare two values where the first value is Round(Random(10)) and the second value is 5, then create enemy at that tile location.

    if you want an exact amount of enemies, then keep looping through until the desired number have been spawned.

  • I dont know if thats what you mean, but this worked, but im not happy with it, didnt like the method

    I thought I could just pick random array.x and array.y with certain values

    Thanks a lot

  • or this, but gives you less control (I can attach map screeshots of the end result if you want)

  • Vaank the first method was the way I was suggesting. It gives every empty space an equal chance of getting an enemy with one quick loop through the array.

    If you just randomly pick array.x and y locations, you would be picking non empty locations and occasionally be picking the same empty location, so it isn't going to be more efficient - unless your array is huge and you want relatively few enemies.

    The first method gives you the potential to set up your array to adjust the possibility for creating enemies in certain areas - you could use a range of numbers to influence where the enemies are: 0 means location is filled, 1 means empty but no chance of enemy, 2 = small chance, 3 = medium chance, 4 = high chance. You could also use different number ranges to indicate what types of enemies can be created in various areas...

    But, as always, there are many ways to do things - you just have to decide what works for you and gives you the result you want.

  • If you just randomly pick array.x and y locations, you would be picking non empty locations and occasionally be picking the same empty location, so it isn't going to be more efficient - unless your array is huge and you want relatively few enemies.

    I understand, perfectly explained, thanks!

    The first method gives you the potential to set up your array to adjust the possibility for creating enemies in certain areas - you could use a range of numbers to influence where the enemies are: 0 means location is filled, 1 means empty but no chance of enemy, 2 = small chance, 3 = medium chance, 4 = high chance. You could also use different number ranges to indicate what types of enemies can be created in various areas...

    About this method, I dont quite understand how to do it, I mean... Do I create a new array with new numbers at locations where "1" in the first array were? Do I use the same event I showed first and restrict it to arrays.coordinates? Or what?

    (I think im not doing what you suggested at the top, maybe yes and Im idiot)

    Thanks a lot

  • You are actually looking to get something like this :

    You check the current value for the current cell inside your array. 1 means the tile is empty, so you possibly want to spawn an enemy into it.

    A cell of value 0 is simply ignored.

    At this point, what AllanR meant, is that here, instead of having strictly 0 and 1 as values, you can have even more values (2, 3, etc...) with each value, meaning what "probabilities" of enemy spawn you are going to have.

    In that case, you would actually check for this value as subevent of the "For each XY" condition and have several "branches" depending on the formula you are looking to apply.

    In that example, with the use of the local variable, I'm simply checking and making it so that if the random value I get for each cell is greater than 7 then I'm spawning an enemy.

    That is in this event as well that you can add a counter of enemy if you want to limit their number.

    About probabilities and using arrays I had done a tutorial on random drop loot that can be relevant to a certain extent. You might want to check it out.

  • This is getting off topic, but one comment anyway. While I think the ideas presented here are good, I don't quite see why everyone rounds random numbers to integers when dealing with propabilities. I would simply go with random(0,1) and that can be compared to anything (e.g. <0.001 if I want one in a thousand propability).

    Maybe this has something to do with human psychology and intergers being easier to comprehend. I have been more involved with mathematics, and grown used to propabilities expressed as a number between 0 and 1.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • oooook! thats good! just what I was looking for! thanks a lot!

    i'm newbie and im doing my best to understand loops but this is too much for my little brain for the moment

    I'll take a look at your tutorial, one of the few I have not read yet

    Lumik, I was wondering the same, I will definitely use just random without int

    Thanks again

  • Ok, so thanks to your tutorial I made crates with random loots, those loots are weapons and these weapons has their own statistics (accuracy, rate of fire, dmg, etc)

    When the player shoots a weapon, he spawns the appropiate bullet for the weapon he is carrying and sets the bullet.angle to the "aim+-weapon.accuracy" I can do this with all my weapons but... is there a way to pick the statistics from an array and apply them to the shot?

    What I was trying to achieve for the last few hours is:

    Weapon family:

    weapon1

    weapon2

    Bullet family:

    bullet1

    bullet2

    array has stored damage, accuracy, rate of fire, ammo spended on the shot and type of ammo for each weapon type

    is it possible to do this?

    Player on shot spawns bullet type depending on the current weapon

    Set bullet towards mouse+-weapon.accuracy

    Substract ammo needed from the chamber

    What i'm trying to do its to teach myself better ways to do things, i can do it weapon by weapon (probably more than 75) but if thre is a way to save time in the future this is worth

    thanks again

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