I am trying to teach myself how to Roblox. I have a basic understanding but there is a lot I do not know.
I have a test game where I am using parts as terrain instead of using the built-in Terrain system.
I currently have a system scripted that spawns a number of objects (trees, rocks, plants, etc) at random positions on a part’s Top surface, but there are pre-placed roads and cliffs that I do not want objects to spawn on top or inside of.
I am using GetTouchingParts after the object spawns to determine if it spawned touching a “no-spawn zone” and if it did, I destroy the object and spawn a new one at another random position.
Now this seems to work fine, but my uneducated, OCD brain is wondering if this is the ‘proper’ way to do this. I’m worried that every time an object randomly spawns on a road or inside a cliff, and then has to be destroyed and respawned elsewhere, is just an unnecessary use of server resources. Due to the RNG of my system, a single object might have to be respawned multiple times before it lands on an acceptable position, and I could have thousands of objects in my world.
Should I (can I) check for touching parts before I spawn the object? Is there another method that I haven’t though of?
I am just curious how everyone else might handle this system.
Any advice is appreciated. Thanks.
(apologies if this is not the correct place for this type of question)
You could predefine locations where the item could spawn with x, y and z coordinates using a table. For example ; local spawnZone= {Vector3.new(x,y,z), Vector3.new(x,y,z), Vector3.new(x,y,z)}
Using this table, you could then randomly select a location from it using math.random() and then clone your part to the randomly selected coordinate.
While your method would work, I do believe that it’s dangerous because there would (like you said) be a chance of an object spawning on the road a dozen times causing server lag each time it gets destroyed. The method I mentioned should save you from those issues.
I maybe should clarify that the bulk object spawning will only happen when the server initializes, populating the world with objects. Afterward, objects will only attempt to respawn individually after a player destroys it. So in this scenario, perhaps the server might stutter a bit on start, but maybe will not be an issue afterward?
@BarDowned – As far as predefining spawn locations, I would rather not. I really like the idea that every time you enter the ‘forest area’, the trees, rocks, bushes, etc., are in different locations. This way you are not harvesting the ‘same exact’ tree/rock every time you enter the forest.
Side thought: Instead of blindly creating a tree at a position, and then checking TouchingParts, what if I have an existing dummy part that moves to the position and then checks TouchingParts. It would essentially turn a (Clone-Check-Destroy) in to a (Move-Check-Move). Would this save on server resources? (My guess is that moving an existing object is simpler than cloning one and then destroying it?)
Put down blocks where you would want objects to spawn at
Use some RNG to determine what object to put there (or put no object at all)
Ideally, this allows for much finer placement, avoiding areas you don’t want objects to spawn in, and use less server resources by completely eliminating the use of GetTouchingParts().
Now, if you’re planning to place a ton of complicated (or large) objects, I would still use GetTouchingParts(), but give objects a basic part hitbox (covering it’s size).
If the hitbox is touching anything, don’t make the object, OR spawn a different, smaller object, making sure to check again for touching parts, etc
Adding this general hitbox also allows you to calculate the height of what your object is going to be placed at, if you’re interested in doing that
Yes, you could save resources by using a moving part.
However you do understand that you can have individual tables for each object?
For example you could have a table for your trees specifically, which has 20+ predefined locations in it, ultimately randomizing the forest area each time.
Maybe I should have been more specific in what I ment.
@INT_L Hmm. I like this idea. It will be more work up front, but will be worth it for a better-performing game in the end. I will look in to implementing a solution like this. Thanks.
Why don’t you create a few invisible parts covering the areas you DO want to spawn the objects on, instead of using the actual ground? This would allow you to keep your current system, with no pre-defined placements. This means you wouldn’t have to spend a ton of time placing each object position, and is much more easily editable.
I’m experimenting with using separate parts as spawn areas (shown by the bounding boxes in the image). I was a bit OCD about all the extra parts required, but they are all Anchored, No Collision, Full Transparent, No Shadow, so I think it will be okay.
I believe that I will still need to use GetTouchingParts to make sure I am not spawning objects on top of other objects, but at least I have eliminated the need to check for cliffs/roads/etc. And I think I should give each area it’s own ‘weighted value’ so the smaller areas generate less objects than the larger areas.
I feel like tables are the best way to store and access all the data for each object (SpawnQty, RespawnTime, Size, Rotation, Angle, etc.) So I will have to go learn how to do that.
Anyway, thanks to all who offered advice! If anyone has anything else to add, please feel free to comment. I am open to all suggestions.