I made a collectibles system and I need to make sure that it’s not too heavy in terms of performance.
Note: It is all done on server. I’ve tried the system and It worked fine.
Basically what I do is:
In a for loop, I clone the collectible (in this case a Coin) and store the clone in an index of an array. (I do this for the amount of coins I want to place over the map)
In a new for loop, I iterate over the array which has the Coins and I position each coin randomly within a Region3.
While positioning the coin, I raycast downwards to see that it has been placed somewhere where there isn’t anything besides the floor. This is useful to avoid trees for example. I keep changing position & raycasting until I don’t find anything besides the floor.
Since I am changing position & raycasting continuously (until conditions meet) will this affect performance too much?
In the same for loop, I have a .Touched Event for all coins which checks whether a player touched it and whether the coin is transparent or not. If all conditions meet, the coins will be set to transparent and after some time, I generate a random position in the map and basically repeat step 3.
Do you think it’s efficient enough or will it cause performance issues? If you have any suggestions please go for it and drop them down below.
Just cast a ray with a new random position before actually changing the coin position. Once you know a safe location, put the coin there.
You can do the Touched (better: raycasting or distance checking) checks on the client entirely and “ask” the server to pick up a coin whenever you qualify; the server then checks if you could have actually touched the coin (compare the server-known character distance to the server coin position within a forgiving radius).
So if I got you correctly, before setting the coin to the new position (and THEN raycast) , I raycast using the newly generated random position and change the coin position after. Does that actually have an effect on performance?
For the picking up, I can make magnitude checks on client and fireserver to do some serverchecks (check the distance between the character and the coin), if all conditions meet I award the player and fireallclients to hide the coin. Did I get the concept right?
I would run your current code in the command bar in studio to create some random parts which you can then save as a model; then I would pick a random part from this preset model and spawn the coin in the picked part’s position. Without going into the performance of your current system, using this suggested method you will have eliminated the use of Region3 and Raycasting by opting for a cheaper system which gives you more or less the same results.