good day. I have an enemy (undeadtree) that spawns randomly along the x position of object (undeadtreespawn). the problem is the enemy often spawns on the players position how do I prevent the enemy spawning near the player but still at a random position on undeadtreespawn?
This is a way of getting round that:
great thanks very much! simply adding collision deleting the enemy and resetting the timer works ahah.
This is an alternate formula for the object X() value. pick a random number for the distance away from the player and then multiply by it by a random number that is either 1 or -1
player.CenterX() + RandomInRange(40,150) * RandomWithStep(-1,1,2)
thanks so much! this is more like how I was thinking it could be done and it works with the events I have
UPDATE I have a problem still because the enemy has to spawn within the width of undeadtreespawn object and not the player or it will cause the enemy to spawn places it shouldn’t. I’m trying to spawn enemy randomly within the width of an object but not near the width of the player on the object
Sorry, I misunderstood. How about this.
pick a random number, if it’s 0 pick a random position to the left of the player, if it’s 1 pick a random place to the right of the player. I don’t know if the player is always under the tree, so I added a check that would pick a location the whole width of the tree if the player isnt under it. It sets the random side variable to 2 so it doesn’t add a second object. I tested it using a button.
hello thanks no problem at all. the player is not under the enemy but spawns at the same y position as player. after trying a few times I can’t get this working?
here is my events but the enemy still spawns on the player and if player is on the right boundary of spawn object the enemy will spawn outside the width of the object?
For the spawnright=1 line the playerhitbox.centerX should be first in the randomInRange(#,#). The lower of the 2 values needs to be first. The randomInRange picks a random number in one direction so 10,20 would work because it’s from 10 to 20 but 20,10 wouldn’t work because it’s 20 to 10 and would alway pick 20.
To get everything lined up, you may need to add or subtract here or there depending on where the points are. If you’re center point is in the default center then you might need to add half the width of the spawn object to the boundingBoxLeft and subtract the same from the boundingBoxRight.
IDKW what your scene looks like. So, it’s tough to figure out exact numbers. This might help.
ok I understand the first part and for spawnright=1 changed playerhitbox.center order that fixed enemy spawning always at the right boundary of spawn object. the spawn object width is 128 pixels and player hitbox is 16 pixels. in the scene layout the spawn object (purple block) and playerhitbox (blue bock) are tiled sprites could that be why it’s not working?
events enemy still spawns on player?
It should work. It worked for me although I didn’t use the same setup. I’m not at my PC to test anything. I can check my events later.
that’s ok thanks for the reply. I ended up getting it to work with MrMen solution of deleting enemy on collision with player and respawning enemy when deleted.
but I will still want to try and get your solution working on my end will update post if I get it working.
Does it create an object on the player no matter where the player is or just when the player is near the edges of the spawn object?
If it’s just when near it’s probably the test to see if the player is above events. It’s not taking into acccount the point the spawn position. A more accurate test would be
If one of the following is true
hitbox.BoundingBoxRight < spawn.BoundingBoxLeft
hitbox.BoundingBoxLeft > spawn.BoundingBoxRight
You might also need to add or subtract from the random in range values to adjust for the center point. Instead of BoundBoxLeft, it might be BoundBoxLeft + 8 (or half the width of the object being spawned). You might need to subtract from the right bound.
hi thanks for getting back. I sort of got it working the enemy spawns randomly and within the spawn object now but enemy still spawns on player no matter where the player is.
Sorry, it sounded like a sound and easy concept but you don’t always think about all of the possibilities. Choosing a position is one thing but making sure there’s enough room or taking into account for the center point is another thing. I still think using a formula is more reliable than a sort of clean up after a failed attempt. It’s a bit like intentionally allowing an error to happen.
If that solution works that’s great. I mean no disrespect. People have different ways of coding. It’s all good. It’s just different. There’s a tradeoff between form and function. There’s a point where the solution is overcomplicated and inefficient.
Let’s examine the concept. I didn’t see any issues in my testing although I can see where there would be issues near the ends. I probably used a wider spawn area. So, it wouldn’t be as noticable.
I’m guessing instead of just checking if there’s an available point to the left or right that you would need to see if there’s enough space for the object that gets spawned.
I’m beginning to wonder if your current approach would be less complex and more efficient.
I don’t like to give up and I also love a challenge. I’ll do some more testing. If I figure out my problem I’ll share it. Obviously, you’re free to use whatever method you want. Good luck and good fun.
hi thanks so much for helping. I don’t like to give up ether ahaha and I think your solution is more efficient than delete on collision. I have got it to work! but there is an issue. everything is working as it should but when the player is on the left edge of spawn object the enemy will often spawn on the player?
UPDATE I think you’re right about not enough space to spawn. not sure how it will work with smaller spawn objects. still having an issue where enemy spawns outside the spawn object and on the player when player is on left or right end of the spawn object.
here my updated working events
Yeah, the object width has to be taken into account although there’s a problem bc you can’t get the objects size before it’s added. I guess you could add the object to some insignificant location like 0,0 and then get it’s size and immediately move it to a free position. The move wouldn’t be noticable.
that’s ok thanks for all the help. I should move on and use the delete on collision solution for now… I’m still having issues with the enemy spawning on player no matter where player is as well as spawn outside the spawn object
UPDATE I finally got your solution to work . the delete on collision was not working when a specific number of enemies need to spawn. I went back to your solution and now have it working as intended and enemy no longer spawns on player.
Excellent. (these words are here bc I have nothing else to say but there’s a minimum character count)
UPDATE hello using chatpgt I got these events working as another solution. I still have the issue where if the players x position is at the spawn objects right boundary the enemy can spawn outside the spawn object opposite the player. I’m not sure how to prevent that but this is the best solution I have tried so far.