Is this a good way to use composition?

I’ve been experimenting with different design patterns(?) for my player code and I’ve been considering using composition because I really wanna try to keep things modular especially since I plan to add enemies so I wanna be able to reuse some things.

The issue is I think the way I plan to use composition is incorrect. First of all my understanding of composition is it’s basically a bunch of mini classes to make one big class so with that in mind my plan is to have a player class

function Player.new()
	local self = setmetatable({},Player)

	LocalPlayer.CharacterAdded:Connect(function(character)
		self:OnCharacterAdded(character)
	end)

	LocalPlayer.CharacterRemoving:Connect(function(character)
		self:OnCharacterRemoving(character)
	end)

	if LocalPlayer.Character then
		self:OnCharacterAdded(LocalPlayer.Character)
	end
	
	RunService.Stepped:Connect(function(dt)
		self:Update(dt)
	end)
	
	return self
end

And then add classes that make up the player so player input, movement, dashing, jumping, evading, etc so I’d end up with something like this.

function Player.new()
	local self = setmetatable({},Player)
	
	self.Input = Input.new()
	self.Movement = Movement.new()	
	self.Combat = Combat.new() -- or maybue just self.PrimaryAttack, self.SecondaryAttack, etc
	self.Dash = Dash.new()
	-- etc
end

Not sure if this will end up causing issues though so looking for some advice.

Examples provided here look pretty good, but you need to remember that Roblox itself is also an object-based system. Don’t try to make dependencies or anything, make everything generic. Modular systems may help with that as well, but if it requires a single remote event that it cannot make, then it’s no longer generic, and it cannot be used in another game without that remote event.

I was developing BaseAdmin for quite a while now and I realized that the generic stuff shouldn’t apply too much to it. On the other hand, not enough generic stuff will prevent it from loading if just one thing goes wrong. A few years ago, it was fully self-contained, and now it is very modular.

Now, if you are considering making multiple classes for creating a larger class, yes, it will cause issues, especially performance issues. If you create them once and you’re done, that’s completely fine. If you keep on building new classes during gameplay this might affect performance of your code. Remember, every function call decreases performance.

On top of that, storing all of the classes that are unused will take up memory. This won’t be a huge problem if you manage your game wisely, but if your game takes over 8GB of memory (OUTSIDE studio) then you’ll probably want to rethink that.


For client operations I’m not completely sure if modules are a good idea or not. If these are something like abilities, then I’d say this is a great idea. If this is for camera stuff (exception to SpringService), then I think there might be performance drops. It’s up to you to decide.

For the movement part, I don’t think modules are necessary. Are you going to need to construct a 1000-line movement class? Probably not. Having smaller classes might be unnecessary.

Hey, sorry for the late reply. I’d like some clarification on some of the things you mentioned.

Could you explain what you mean by dependencies? iirc that’s when code relies on other code, but what I did I don’t think has that happening(?) although I did change it so the player code passes it’s self to those other classes so that they can communicate with the other code.

I just started working on my game so I’m not doing anything like this at the moment besides making that player class when they first join. I then use events such as CharacterAdded to update things such as the character reference. When I do enemies though I have a feeling I might make this mistake of making a new enemy class everytime I wanna spawn one.

Well good movement is gonna be pretty important since my game is gonna have fast paste combat, but you reckon I just use normal scripts?

I don’t really understand what you said here. Could you explain it in more detail?

Sounds good.

I would keep the same class when spawning an enemy, making it general for all enemies.

I think you meant pace, but modules aren’t necessary since all the movement can be in one script, which will make it faster. Organizing it is necessary, but modules actually won’t reduce performance too much depending on how you use them. If you’re using a module, try to use the self argument when possible, but I don’t know how this affects performance.

You mentioned dependencies and I assumed you meant don’t make code that’s dependent on other code which I don’t think my code does, but perhaps you were talking about something else.

1 Like

No, that’s exactly what I was talking about. Your code should be general, like if you put it in a separate place, it should work fine. Sure, you should probably have a few dependencies, but keep it to a minimum. Don’t make your code independent if it interrupts your workflow or causes performance issues.

1 Like