*** Title supposed to be ’ Except certain positions’ and not ’ Accept’ *** sorry for that mistake
I’m wondering if its possible to have a table of coordinates and inside a spawn function that creates a display object, make it exclude those positions inside the table when positioning.
For example, I have another object that is spawning at these coordinates only…
In your case rnd will never be 0? If you want to generate a random number between 1 and 3 use math.random(1,3).
Two ways of approaching your problem. 1) Have an array of allowable spawn points - good if the count of spawn points is low. 2) Check if a spawn point is not being used and then continue.
For for the comment adrianm, I think you might have misunderstood my question which is actually my fault. sorry.
The snippet from the code including the rnd part was just a demonstration of how I’m using yPosition variable. That function has more rnd values inside the function and works fine.
I guess the main question is: How can I make a new spawn exclude the values inside yPosition.
Ah OK… was just pointing out what appeared a logic bug (this is the trouble with partial code).
If you want to exclude a range of values do something like this
local yPositions = {100, 160, 220, etc} local yPos = 0 local canPlace, foundSpawnPoint = true, false while not foundSpawnPoint do yPos = math.random(1,240) for i = 1, #yPositions do if yPos == yPositions[i] then canPlace = false break end end if canPlace then foundSpawnPoint = true end end
Note: this is not the most optimal way but I’ve coded it like this so it is easy for you to understand.
In your case rnd will never be 0? If you want to generate a random number between 1 and 3 use math.random(1,3).
Two ways of approaching your problem. 1) Have an array of allowable spawn points - good if the count of spawn points is low. 2) Check if a spawn point is not being used and then continue.
For for the comment adrianm, I think you might have misunderstood my question which is actually my fault. sorry.
The snippet from the code including the rnd part was just a demonstration of how I’m using yPosition variable. That function has more rnd values inside the function and works fine.
I guess the main question is: How can I make a new spawn exclude the values inside yPosition.
Ah OK… was just pointing out what appeared a logic bug (this is the trouble with partial code).
If you want to exclude a range of values do something like this
local yPositions = {100, 160, 220, etc} local yPos = 0 local canPlace, foundSpawnPoint = true, false while not foundSpawnPoint do yPos = math.random(1,240) for i = 1, #yPositions do if yPos == yPositions[i] then canPlace = false break end end if canPlace then foundSpawnPoint = true end end
Note: this is not the most optimal way but I’ve coded it like this so it is easy for you to understand.