Over the past day or two, I have been investing my time into learning OOP, as the next project that I am planning on working on will be very complex, and I need to learn how to structure my game well.
I have created a small project in which when the player joins a game, a spaceship is created for them using what I hope is an object orientated approach. The project uses Composition instead of Inheritance, as I have heard that Composition is better for this kind of thing. Here is the github repo of the project:
(The code is in the âsrcâ folder, as I am using VSCode + Rojo - you can view where each folderâs code is stored in-game by viewing default.project.json)
I am looking for feedback about what I have done well, and what I could have done better, so basically just any feedback is appreicated!
Your attempt at OOP is very good. One thing about OOP, only use it if your making a lot of something. (eg. TDS games) You donât need to make it for the thrusters, combine it with your ship module. Overall, keep on going with this!
Thanks for the response! There are going to be more things similar to the thrusters class (e.g. shield generators and cargo racks), which is why I put it in itâs own seperate script, because with the amount of ship modules there are going to be, the main script would get very long having them all in it.
Question
One question I have is: let's say I want the ship to move forwards when W is pressed, how should I go about doing this? Seeing as key presses are detected on the client, I would check when the player is holding 'W' in a RenderStepped function, but then what?
I could create a method in the thrusters class called 'Move()' or something, but how would I pass that method to the client, as metatables don't get sent with tables in remote events.
Also, wouldn't doing this create lag, as I would be calling the method every frame?
BTW Iâm not asking you to write the code for me, I would just like a pointer in the right direction. Do I even need to use OOP for this, should I just put the move code inside the RenderStepped function?
(Edit: I have now updated the repo with some changes to ship module creation, feel free to check them out! I am a little bit unsure if Iâve gone overboard with all the different scripts for different module types, so any feedback on that would be great!)
Use context action service, itâs fast, simple, and quite useful. You already have a ship handler for the client so add the key binds. Just unbind it when the player leaves the ship.
Using OOP would be redundant ( unless youâre making it in the client ship module ). Have it in the client script.
No, this would be very slow and laggy. CAS will take care of that.
Yes, you could. You just have to fit it in the bind.
Donât put the method in the ship unless you want messy code. Yes it would create lag doing it every frame. This is because youâre always checking for a keycode which is unnecessary.
TLDR
Use context action service in the client ShipHandler script to check if to move.
(EDIT)
Some feedback
The modules are now nice, you have a module for important parts of your code. I may ask, why do you use BodyMovers. They are old and should be used for games like RetroStudio.
Am I to understand that I should use a remote function from server > client to send the ship with the metatable? And how does this differ to just using a Remote Event? I thought Server>Client remote functions werenât used as it could leave the server hanging?
RemoteEvents do not return anything while RemoteFunctions do return. RemoteEvents are used for simple actions from client to server or server to client.
From my knowledge, yes, this could and will happen. Thatâs why I only talked about it for one sentence.
If you do really want to do this route, then yes. I never tried it so my bad.
I donât use body movers, I use AlignOrientation and LinearVelocity (unless thatâs what you meant by body movers), I have their variables named gyro and velocity for simplicityâs sake.
I am currently doing exactly that: using UserInputService:IsKeyDown() in a RenderStepped function to check when I should yaw and move the ship forwards, and so far there are 0 performance issues, and I am using a rubbish laptop, so I donât think there is really any problem using RunService to detect key presses.