#Code Review
1 messages · Page 1 of 1 (latest)
simple mistakes
rb2D = gameObject.GetComponent<Rigidbody2D>();
why are you doing that in start?
if the rigidbody is not allready on the component the game will break
so you are suggesting use the Inspector?
so just put it into
private void OnValidate()
{
rb2D = GetComponent<Rigidbody2D();
}
```
as this method gets triggered in editor only
and each time you enable the script in editor
it will search teh rigidbodey and set it for you
that means if those scripts arent automatically set => you forgot them to add
if you do it in start you have to => compile => run into start => search for the null pointer
if you make it like me
you see it through the entire time taht all necessary scripts are there
also you dont have to call it on start
its allready set
which is much more performant
you should watch docs for that
_isDead = value;
Destroy(this.gameObject);
GameManager.RestartGame();
``` this doesnt make sense
birb.IsDead = false;
then it still gets killed???
if(_isDead = value){
Destroy(this.gameObject);
GameManager.RestartGame();
}
would make more sense
Oh
i hope you understand what i mean
Ye
if not ask about it in the channel
No, i understand
as im explaining like "understand it or research"
Vector2.up * 250 this sucks ass
this is called magic numbers
or magic strings
none knows what the 250 is
if you would make
Oh
private const int FLY_SPEED = 250;
and use in code
Vector2.up * FLY_SPEED
everyone would know what it is
Sorry i just moved from Roblox Studio, i was programming on Lua around 4 years
So i got used to the most things
But i can actually understand what you want to tell me
Makes sense
you would forget the other one
So i should rely on variables more?
for "magic numbers" and "magic strings"
rely on consts yes
consts are very valuable for replacing the magics
Yeah, i got it
but now you want "birb"
you will forget scripts
// bool canJump = true; zombie code sucks ass
zombie code?
Oh
Got it
if you comment out code for test its fine
but not in release
or in code review
comments are for giving information about complicated things
Makes sense
float lastTime;
bool _isDead;
where is the accessor?
?
i dont believe you meant it to be internal
Oh
Isnt they default as private
Oh
i write automatically the accessor
as none has to think about it
even if the default is set to this and that
K got it
code must be written clearly
Yeah, i rly should learn to write clean code
private static List<GameObject> _pipes = new List<GameObject>(); dont use static if you dont know what you are doing
Used to be a solo dev, so no one was checking my code except myself
if someone tells you "you can use static for storing stuff and holding a state" then the "someone" can fuck off
About this, i just got stuck with a RestartGame method
I just didn't know how to get the reference to it and etc
you put something in and you get somethign out
but not store stuff in statics
thats a rulke
Just as info container
nah
public static int Sum(int a, int b) => a + b;
here your static method doesnt store anything anywhere
you ptu something in
Oh
something gets out
only static "storage" is const
if someone else tells you something else you know that he doesnt know what he is doing
you will result in "spaghetti code"
which is bascally the death of your program
Ye
public static GameObject birb; dont static
but i think i evaluated the static point enough
foreach (var item in _pipes)
{
Destroy(item);
}
``` foreach on list will break your code
as the enumerator dies
you are not allowed to change a collection when an enumerator is iterating over it
foreach (var item in _pipes.ToArray())
{
Destroy(item);
}
that works
Ohhh
new Vector3(0f, 2f, 0.125f) again magic vector
no dude on earth will know why youve chosen those numbers
also no point in creating new vector each time you instantiate a birb
reuse the old vectors as their are consistent
but vector is astruct so you cant make it const which sucks
Oh right
but atleast you dont reinstantiate the magic vector all over again
GameObject pipe wtf is that
create a pipe script on your pipe which describes its behaviour
Oh
pipe.GetComponent<Rigidbody2D>().velocity you are searching each time you create a pipe the rigidbody
instead of creating a script
which references allready the rigidboy
???
Vector2.left * pipeSpeed here you avoided the magic numbers
😎
God damn
what if each pipe has its own time ?
I cant have magic numbers at all?
if you want clean code : no
Oh god
But
What's the meaning having so many premade variables
It would be like a huge fucking list
birb.IsDead = true; if you would have set it birb.IsDead = false; in your current code the same outcome would have happened
If ill Try to avoid magic numbers and strings
you will make them consts
so you want to search on a change each of the numbers in each script?
doubt
you will learn to programm more automated based
so the numbers set themselves
Question.
Naming the variable inside of method still counts as magic number/string?
birb != null as birb is a gameobject you can jsut simplyfy it to !birb
Didn't know
not necessarly
Uhhh
void Start()
{
}
``` ??????
if your code is clearly readable you can leave it as is
in your method
Oh k
Well uhh
I just realised that code wont run
Without a Start() thing
Yeah ik
yes
On collision enter
Ohh
Then im dumb
I thought script wont run without one of them
if you implement a unity message
this script will receive those
start is a message
update is amessage
on collision enter is a message
you can have 0 messages
yes
Oh cool
if you would have created a monobehaviour
and deleted all messages
you would have realized that on your own 😛
im bit harsh on that .. i know
i needed to help
lua script was the first program code i saw when i was 8 years old
and it was the reason why i wanted to code
so it was a duty to hlep lul
My reason was
Smth like
"Wow ppl making such a great games, why cant i do same"
Well, its too late for me, like 4AM and its my BDay tmrw.
Soo
Tysm for help