Spawning objects at random X co-ordinates and in fixed intervals

Hi, I am making a game inside Monogame, using c#. I need my objects in the game to spawn in random X co-ordinates. I know how to spawn more than one in a specific state, but I cannot figure out how to spawn them randomly. I've tried a few methods; having a random number generator decide which block to spawn, setting the x and y co-ordinates to random number, which both returned error messages. I'm hoping I can get some help with this? Thanks! I'll attach some screenshots when I have them.
20 Replies
Anton
Anton4w ago
not screenshots $code
MODiX
MODiX4w ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Anton
Anton4w ago
copy the error messages
☆ nubbs ☆
☆ nubbs ☆OP4w ago
okay one second okay i cant seem to get the error code to pop up but the code isn't working as intended one sec let me copy it is in RandomSpawn.X = randomnum.Next(0, 10000); Globals._enemysprites = new List<LandEnemy>() { new LandEnemy(new Dictionary<string, Animation>() { {"EggPawnLeft", new Animation(content.Load<Texture2D>("EggPawn02"), 10) }, {"EggPawnRight", new Animation(content.Load<Texture2D>("EggPawn01"), 10) } }) { EnemyPos = new Vector2 (randomnumberspawn, 1120), EnemyRect = new Rectangle () }, };
Angius
Angius4w ago
What's the expected behavior? What's the actual behavior?
☆ nubbs ☆
☆ nubbs ☆OP4w ago
its supposed to spawn the object in a random X cord, it doesn't spawn it at all or just spawns it at 0, 0 and for some reason it glitches out only one of them, the rest are fine but one breaks for some reason?
Angius
Angius4w ago
If you're instantiating new Randoms with each loop, that could cause all the values being the same It's better to use Random.Shared.Next() Or create your own private static instance of it to reuse It would be best, of course, to use the debugger That way you could see exactly what the issue is
☆ nubbs ☆
☆ nubbs ☆OP4w ago
so for the shared.next should i instantiate that in the class i want to use it in, or like you said a static one in like a globals class for example? i want the random number to change for every spawn if that makes sense, so i tried putting it in a for loop
Angius
Angius4w ago
For Random.Shared.Next() you just use it as-is, instead of randomInstance.Next() If it's not available on your .NET version or whatever, you can create a
private static _rng = new Random();
private static _rng = new Random();
and use _rng.Next()
☆ nubbs ☆
☆ nubbs ☆OP4w ago
okay i'll give that a try and see what happens, should i still use randomnumberspawn as the X for the object though? i tried doing the random.shared where the random.next was and i got this message Member "Random.Shared" cannot be accessed with an instance reference; qualify it with a type name instead
Angius
Angius4w ago
Well, yeah
☆ nubbs ☆
☆ nubbs ☆OP4w ago
does that just mean chane randomnum to Random?
Angius
Angius4w ago
Random.Shared.Next() Literally Verbatim Not randomnum.Shared.Next()
☆ nubbs ☆
☆ nubbs ☆OP4w ago
yeah i just did that thank you, i'll test it now and see what happens it works! thank you so much now i need to figure out how to make it so they spawn again in intervals would you know how i'd go about spawning objects in intervals? like spawning those objects say 1000 times at random points within the next interval
Angius
Angius4w ago
Interval in time?
☆ nubbs ☆
☆ nubbs ☆OP4w ago
i suppose so yeah, i need them to spawn as you move forward in the level basically its in infinite runner btw thats probably important information to have
Angius
Angius4w ago
Right, you probably don't want to do it over time, but rather in proximity to the player Or just about off-screen
☆ nubbs ☆
☆ nubbs ☆OP4w ago
yeah see that's the thing, collisions don't work and I have no idea how to do them yet lol
Angius
Angius4w ago
Unless the player moves at a constant speed, then sure, a timer would work I never worked with Monogame, or whatever it is you're using here, so I won't be much of help there Googling "monogame timer" should yield some results, though
☆ nubbs ☆
☆ nubbs ☆OP4w ago
i do know how to do one, i have multiple in my game already, i tried just now throwing the drawing into a for loop up to 1000, but that glitched it out so i took them out. I was thinking having a random number combine with like a current spawn vector so that when it passes through a loop, it goes up by say 2000 and then the objects draw again but that doesnt seem to work i'll do some thinking and i'll see if i can figure it out thanks again so much for your help
Want results from more Discord servers?
Add your server