#can someone look at my code and give me
1 messages · Page 1 of 1 (latest)
Please share the code to review
Okay Iv uploaded the script.
Oh boy
📃 Large Code Blocks
Use links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/, https://paste.myst.rs/, https://hastebin.com/
📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
Consider using a paste site
First of all, do not use public fields. This is something you should never do because there is no clear indication who can modify the value
Instead make it private, and either
- Add a private method that validates it
- Make it a property with a public setter that validates it
- Make it a private field with
[SerializeField]
Considering these look like a bunch of configurable fields for in the editor, make it a private field and add [SerializeField] to expose it in the editor.
Just make sure to never use public fields. If a field should be grabbed by another class then make it a property with a public getter and private setter. Always put as little accessibility to data in a class until it is required
okay iv seen the SerializeField but never used it yet but ill do that
Second point, when using private fields they start with an underscore. Assuming public Rigidbody rigidbodySelf; takes the component from the editor you end up with [SerializeField] Rigidbody _rigidbodySelf;
This is not just because .NET conventions say so, it makes it very clear what the accessibility of the field is, but also the scope of it.
You should only have a name with no underscore and camelcase if it's a method parameter, so you know where you can use it
It's that little bit of consistency that makes programming way easier
Anyway that's mostly it for the variables, I haven't even scrolled down yet
You got a Start method with a lot of GetComponent calls but you should put this in an Awake method
Awake is for initialization, start is for start of actual behaviour
So
if (!sSInfo.player)
{
StartCoroutine(CheckIfObjInSightCoroutine());
}
Should be in a Start method for example
Also, just for the sake of convenience, considering you have these GetComponent calls, Unity has this: https://docs.unity3d.com/ScriptReference/RequireComponent.html
Just puts a hard restriction so these components are required, but it does also make them if they don't exist which can be annoying
i appreciate the tips so far. but thats just the variables. im sure the rest of the code is worst.
should a character controller script be having over 100 variables or am i doing something deeply wrong.
The underscore is just naming convention that some people follow. You don't have to start following of you don't want to. As long as you keep your code consistent.
I see a lot of fields that are basically responsible for the same thing.🤔
As well as a lot of fields that is not clear what they're responsible for.
First thing I'd do is separate the movement logic from ai logic. Ai doesn't belong in a movement controller.
iv been thinking of it, and it does seem like it would help differentiate the 3 from each other, public, private, and method parameters. iv just never implememet it because im still a bit new to it
Ehh kinda hard to see that so quickly
It's not that bad to have a ton, you should just keep it as simple as possible for what you need
You can separate them in different classes if you want maybe, create like a system of special action handlers that will group them together
Like JumpHandler class, and this will group everything for jumping and validate + invoke it
most of the variables do have a different purpose, though similar, but theres definite some that might be duplicates, and even not used because i forget what they are for. im tryingto clean them up / consolidate them,
You don't have any context in the editor though, so if you want to be able to configure jumping you would actually have to put it in the main class
Or serialize the Jump handler so they show, or create a custom editor
Honestly if you don't use them you might be better off having a general constant in a static file for them, or just rmeove them altogether
Magic numbers are not bad, considering you probably have a single value here
Yes, I did mention I would comment on conventions before they made the thread though
Also considering the size of the file it doesn't hurt to add this little bit of readability
Maybe look into being able to do this, it would group stuff together and it doesn't actually hurt the complexity
JumpHandler, CrouchHandler
@tardy cloak so about public variables. i could probably use serializedfield only many of them, but i also have many of them that need to be modified from other scripts. this might be because i am not using things like structs, classes, etc. and mostly i am just using variables to do things.
Which ones are modified for example?
so thats one reason why iv stuck with using public
If there's some that need to be modified then you should probably use a separate method, or change them into properties. Properties allow you to add a bit of extra behaviour to check if the variable is correct but usually you'd make a method
For example, jump height. If you set this you want to validate the height is not negative or above a certain limit, so you make the field private and then add a method to set it, which validates it
Properties do the same thing technically, these are converted internally to private fields with a set method
But people don't expect properties to have complex setters as much as methods would do
its mainly because i am not 'familiar' or comfortable with using 'methods or properties to do such things. effecting variables for me seems easier. but its something i think about
now about this, now that iv looked through, i think almost none of them...
but im sure i have some scripts that need to effect some of them, i just cant ID any at the moment.
If you end up not looking at the code for a while, and then come back you'd have no idea what even modifies it
It would lead to bugs where stuff modifies data and you didn't expect it for example
You might as well just make them all private and then compile the project
The compiler will list all the missing references
This is exactly what I mean too 😛
If you configured VS correctly you can also list references on properties, methods and classes
You can't do that with fields
That's one of the reasons why your script became a huge spaghetti plate. You should limit what other scripts can access in this one to methods and properties.
well there is the right click ' find all references'. but with this specific script i think most if not all of them can be private. now that youve questioned it.
It's also just making sure that it's not being modified without having to spend time looking for if there is an instance of this
With private you're sure only the code modifies it
A lot of your variables seem to be providing the same functionality, but in different states, so maybe a state machine is also one thing that can make things more ordered.
Or rather, encapsulating data in states.
yeah i literally only know how to use variables thats why there is so much of them and i am not using anything like state machines, structs, etc.
i know so much needs to be worked on
i imagine an expert would achieve the same logic / functionality more efficiently in under 1,000 lines of code. maybe under 500