#Code Review

1 messages · Page 1 of 1 (latest)

steel finch
#

aye

uneven musk
#

simple mistakes

steel finch
#

ofc

#

im a newbie

uneven musk
#

rb2D = gameObject.GetComponent<Rigidbody2D>();

#

why are you doing that in start?

#

if the rigidbody is not allready on the component the game will break

steel finch
#

so you are suggesting use the Inspector?

uneven musk
#

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???

steel finch
#

Uh?

#

Honestly I didn't tested bool, it just works

uneven musk
#
            if(_isDead = value){

            Destroy(this.gameObject);
            GameManager.RestartGame();
}
#

would make more sense

steel finch
#

Oh

uneven musk
#

i hope you understand what i mean

steel finch
#

Ye

uneven musk
#

if not ask about it in the channel

steel finch
#

No, i understand

uneven musk
#

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

steel finch
#

Oh

uneven musk
#

private const int FLY_SPEED = 250;

#

and use in code

#

Vector2.up * FLY_SPEED

#

everyone would know what it is

steel finch
#

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

uneven musk
#

also if you write 2 times somehwere 250

#

and change one to 300

steel finch
uneven musk
#

you would forget the other one

steel finch
#

So i should rely on variables more?

uneven musk
#

for "magic numbers" and "magic strings"

#

rely on consts yes

#

consts are very valuable for replacing the magics

steel finch
#

I got the meaning of the magic numbers

#

What about stirngs

#

Just same scenario ?

uneven musk
#

if(tag ="bob")

#

imagine you compare in 3 scripts with "bob"

steel finch
#

Yeah, i got it

uneven musk
#

but now you want "birb"

#

you will forget scripts

#

// bool canJump = true; zombie code sucks ass

steel finch
#

zombie code?

uneven musk
#

zombie code is code

#

which is actually not having any effect

#

like

steel finch
#

Oh

uneven musk
#

int x = 3 and you dont do soemthing with it afterwards

#

or commented out code

steel finch
#

Got it

uneven musk
#

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

steel finch
#

Makes sense

uneven musk
#
   float lastTime;
    bool _isDead;

where is the accessor?

steel finch
#

?

uneven musk
#

i dont believe you meant it to be internal

steel finch
#

Oh

uneven musk
#

and if you meant internal

#

WRITE IT

#

protect your values from your own stupidity

steel finch
#

Isnt they default as private

uneven musk
#

thats basically what accessors are for

#

internal is the default

#

afaik

steel finch
#

Oh

uneven musk
#

i write automatically the accessor

#

as none has to think about it

#

even if the default is set to this and that

steel finch
#

K got it

uneven musk
#

code must be written clearly

steel finch
#

Yeah, i rly should learn to write clean code

uneven musk
#

private static List<GameObject> _pipes = new List<GameObject>(); dont use static if you dont know what you are doing

steel finch
#

Used to be a solo dev, so no one was checking my code except myself

uneven musk
#

if someone tells you "you can use static for storing stuff and holding a state" then the "someone" can fuck off

steel finch
uneven musk
#

static should always be stateless

#

which means

steel finch
#

I just didn't know how to get the reference to it and etc

uneven musk
#

you put something in and you get somethign out

#

but not store stuff in statics

#

thats a rulke

steel finch
uneven musk
#

nah

#

public static int Sum(int a, int b) => a + b;

#

here your static method doesnt store anything anywhere

#

you ptu something in

steel finch
#

Oh

uneven musk
#

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

steel finch
#

Ye

uneven musk
#

public static GameObject birb; dont static

#

but i think i evaluated the static point enough

steel finch
#

Yes

#

Im also interested in the RestartGame method

uneven musk
#
        foreach (var item in _pipes)
        {
            Destroy(item);
        }
``` foreach on list will break your code
#

as the enumerator dies

steel finch
#

How

#

What should i use instead

uneven musk
#

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

steel finch
#

Ohhh

uneven musk
#

or you are iterating over it backwards

#

collection order or size

steel finch
#

Got it

#

Ig

uneven musk
#

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

steel finch
#

Oh right

uneven musk
#

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

steel finch
#

Oh

uneven musk
#

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

steel finch
#

😎

uneven musk
#

thats funny 😄

#

new WaitForSeconds(5f);

#

another magic number

steel finch
#

God damn

uneven musk
#

what if each pipe has its own time ?

steel finch
#

I cant have magic numbers at all?

uneven musk
#

if you want clean code : no

steel finch
#

Oh god

#

But

#

What's the meaning having so many premade variables

#

It would be like a huge fucking list

uneven musk
#

birb.IsDead = true; if you would have set it birb.IsDead = false; in your current code the same outcome would have happened

steel finch
#

If ill Try to avoid magic numbers and strings

uneven musk
#

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

steel finch
#

Question.
Naming the variable inside of method still counts as magic number/string?

uneven musk
#

birb != null as birb is a gameobject you can jsut simplyfy it to !birb

steel finch
uneven musk
#
    void Start()
    {
        
    }

``` ??????
#

if your code is clearly readable you can leave it as is

#

in your method

steel finch
#

Oh k

steel finch
#

I just realised that code wont run

uneven musk
#

it will run

#

once

steel finch
#

Without a Start() thing

uneven musk
#

as its a unity message

#

unity will call it

#

and it will fucking do nothing

steel finch
#

Yeah ik

uneven musk
#

as no fucking code is in it

#

thats a good example of zombie code

steel finch
#

Can the script run without Update and Start

#

And having only

#

For example

uneven musk
#

yes

steel finch
#

On collision enter

#

Ohh

#

Then im dumb

#

I thought script wont run without one of them

uneven musk
#

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

steel finch
#

Can i have 0 message and make smth like extensions

#

For editor or smth other

uneven musk
#

yes

steel finch
#

Oh cool

uneven musk
#

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

steel finch
#

Its ok

#

I appreciate any kind of help

uneven musk
#

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

steel finch
#

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

uneven musk
#

i made a mod for the settlers which used lua

#

i changed at max 3 lines when i was 8 years old

#

but it fascinated me

steel finch
#

My first time coding was

#

Editing script for the game that runs them

#

Like changing color of some object or music