0 votes

Hello, in my game I have two collectibles that spawn for the player to obtain. A coin, which is worth 10 score, and 1 cash, and a gem, which is worth 50 score and 10 cash. The coin spawns when the game begins, and whenever the player grabs it, it respawns immediately at a random location. The gem on the other hand is "rare" how I did this was set it up to a timer with a minimum of 5 seconds and a maximum of 30 seconds, and will spawn when timer timesout, also at a random location. However, what seems to be happening more often than I would like it to, is that the gem will spawn on top of the coin, or vice versa, and they trigger each other; My cash goes up by 11, and my score goes up by 60, and then they both disappear. (The coin respawns immediately after). They are both hooked up to this part of my script for random generation:

func _spawn_coin():

var coin_spawn_position = Vector2(randi()%500+31, randi()%550+31)

while coin_spawn_position == $Player.position:

    _spawn_coin()

$CoinPosition.position = coin_spawn_position

$Coin._spawn($CoinPosition.position)

The gem spawn function is the exact same but replace the word coin with gem everywhere it is shown here. My question is, is it possible to prevent the two collectibles from spawning on top of one another? Or at least if they do, not to trigger each other? (There is an indent for func I had to make it look that way because it wasn't showing up in the script text for my question.)

Godot version 3.2.3
in Engine by (53 points)

1 Answer

0 votes

To prevent them from activating one another, you can set their collision masks. You cant set the collision layers of the Player to be the top left square, and the collision layer of your Areas to be empty, with a mask on the top left square.

Since you only have a few items, when placing a new one, you can manually check if the spot is free. Just keep an array of placed items to check that no other is close to your chosen spot, and repeat selection the until a spot is found.

To display indents properly, you can put a "." at the start of the line :
. indented

by (2,484 points)

aha wow I don't know how I didn't think of that considering I had to do a lot of mask and layer work to have some mobs interact with each other, and some not interact with each other. I guess I was thinking that the fact they were stationary areas had something to do with it which I'm now realizing doesn't make sense. Thank you!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.