I’m working on a shooter game and without projectiles it’s impossible to make a shooter, i decided to go with OOP as it’s very intuitive + allows me to make my code more organized, but here is the catch, caching bullets doesn’t seam fine to me, especially because there might be a lot of bullets and not only one at the time, + storing OOP objects is costly
Question is, if i do this:
local Bullet = Projectile.new(...)
local Hit = Bullet:Cast(Start, Direction, Force)
Bullet:Destroy()
Wouldn’t it lag if hundreds of projectiles are fired?
In theory yes, it should lag, because creating and destroying hundreds of objects have an impact on the perfomance, but i dont know how roblox studio handles the destroying of an object, if you worry about perfomance, use PartCache, its a module script that allows you to store a certain amount of objects in a folder, so it moves the objects that are children of the container. It never destroys the objects but move them so far above of the workspace
Idk if Roblox instances are important here, because bullets itself will be OOP objects, not parts or smth like that, of course i’ll add option to render them as parts, but some bullets may be simply raycasting, nothing more
Methood itself will be used once for player’s weapons, also most weapons will have bullet-travel which means there will be delay between calling function and receiving hit
Also additional question, is adding rendering methood for bullets to move model or part visuals is good idea? or simply casting bullet twice, first time instantly to get points, and second time for math, later i’ll lerp part or model to given points to achieve bullet travel?
I dont think its a good idea, depending in the implementation, like, if your game haves automatic and fast weapons, its a bad idea, because doing twice the job. A better solution its using a module like fastcast, because the raycast is divided in sections, where you can place a cosmetic bullet, without first getting the point and then lerping
I have problem with getting those sections, because i don’t feel like binding bullet math module and bullet rendering is good idea, i mean in one function, like i can decide if i want to render but still
isn’t that what oop is made for? having multiple objects(bullets) at the same time
I am not sure about this, but I don’t think that they are costly to store there are
CFrames/Vector3s/UDims and a lot of other oop stuff that are used, and they don’t cause much lag
I think what would cause lag is the core logic
for example
rather than connecting a heartbeat event to every object, you can add all the objects to a table and loop through them in a single heartbeat event to update their position and so on
I planned on having option to control time, rather than using heartbeat, with lower frequency of raycasts bullets shouldn’t be that costly i think
Another thing i started to worry about rn is how to render bullet, because if raycast wouldn’t be instant i can’t get all points, and casting it twice wouldn’t be performant
you can add a speed/time variable to the bullet and update its position based on that
I am doing that in a td game with client-side rendering and the server is able to update the positions of all enemies in a single heartbeat with constant 60 fps and it starts dropping to 55 when the enemy count is about 5k
like what welotaLuke_gamer226 has said you can use FastCast which update the positions in a heartbeat (I haven’t used it yet through)