#Code feedback

1 messages · Page 1 of 1 (latest)

wooden sparrow
#

there

lofty kindle
#

This works too

turbid moss
#

whats this?

wooden sparrow
#

temporary thread

turbid moss
#

ooh

lofty kindle
#

You can post it in here

turbid moss
#

alr

lofty kindle
#

It really doesn't matter where you post it in the end I think

turbid moss
lofty kindle
#

So do you want a full code review or just the comments?

wooden sparrow
#

don't comment what something does, it should be self-evident.
always comment why something exists, if it's not self-evident.

turbid moss
#

just the labeling i think

#

it seems to work so far

lofty kindle
#

Because I have a lot of tips when it comes to other stuff

turbid moss
#

🤔

#

id rather see if the labeling is fine first i think

#

but i enjoy learning tips and tricks aswell

#

oh hey, theres more of em

keen furnace
#

Desperately need to use else and else if in a bunch of cases

subtle oasis
#

I'm not an expert but why are you using the legacy input manager?

keen furnace
#

the LaserSwitch checks, only 1 can be true so use a switch statement or else if

turbid moss
wooden sparrow
#

google Unity New Input System

subtle oasis
#

codeMonkey did a cool tutorial on it

keen furnace
#

The createLaser functions are indentical, they should just be one method with different parameters

wooden sparrow
#

saves you a lot of headache down the line

lofty kindle
#

You really need to create methods for common checks rather than putting labels down.
Stuff like this is also kind of unneeded to add, considering "gameOver" is pretty clear.

//Game Reset
if (gameOver == true)
{
  ...
#

Just methods in general are a good thing to start with

#

Your whole Update method is full with code that should probably be seperated for example

#

It's the kind of thing that adds self-documenting code and is generally more readable

turbid moss
keen furnace
#

Still, you could move the shared portion of them into a method

#

Code feedback

#

It's also weird that this is called Hero_Movement but does many other random things

lofty kindle
#

And maybe another tip: guard clauses!
You have methods like this

void createLaser()
    {
        // Base Laser code
        if (!gameOver)
        {
            ...
        }
    }

And you could add a guard clause and remove nesting from this, making it more readable.

void createLaser()
    {
        if (!gameOver) {
          return;
        }

        ...
    }

I like to write my code in a way where the "positive ending" to it is always at the bottom. This helps a lot with it.

turbid moss
wooden sparrow
#

that example gets better when you add more stuff though, fused

subtle oasis
turbid moss
keen furnace
#

Also, be consistent about capitalisation. The C# (and modern Unity standards) say public fields/properties should be PascalCase, you have a number of camelCase names.
Methods are also meant to be PascalCase.

wooden sparrow
#

return just ends the entire method

#

you don't need else

lofty kindle
turbid moss
lofty kindle
#

If your method returns void, you can do this

turbid moss
lofty kindle
#

If your method returns something, you obviously need to supply a return value

turbid moss
#

fair

#

google it is lol

lofty kindle
#

You can also return null in some cases, but that's specific to the return type. It means "no value". Not everything can use this.

turbid moss
#

so why does return get mad at me when i try to return specific stuff such as when i tried to return the laserswitch variable?

lofty kindle
#

I'm guessing the types are not matching? For example, it expects a string and you pass an integer?

turbid moss
#

said return cannot be used to do so and so

lofty kindle
#

If you share the code we can tell probably

turbid moss
#

i dont have that code anymore

#

this is the second version

lofty kindle
#

Sounds like the problem is fixed then 😛

wooden sparrow
#

that's a bit off-topic but i think it'd be good if you went through CS50

turbid moss
#

yeah, i removed return and used the 1 2 3 keydowns

turbid moss
#

or did i click wrong link

#

pulled up harvard😂

wooden sparrow
#

yeah, the free course

turbid moss
#

starts may 12?

wooden sparrow
#

no, just use the last recorded one

#

hold on..

turbid moss
#

ill look into it

wooden sparrow
#

i kinda agree that it's better to try stuff you learn immediately but so far i haven't seen anything in unity that does that in a well-structured way

turbid moss
#

ive been struggling on some of these

#

loops especially

wooden sparrow
#

godot has gdquest so if you don't have the patience to go through the course then it might be better

turbid moss
#

while i have ya, i do have a code question

#

did the version of my code have the r keydown reset?

wooden sparrow
#

?

turbid moss
#

it did, perfect

#

i am intending to make a system where once the player dies, they need to press r to restart, but it needs to set the player back at the start and reset everything

#

not quite sure how to go about it yet

#

any tips that i could try/start with?

wooden sparrow
#

if it's not procedural, probably easier to just reload scene

turbid moss
#

what makes procedural?

#

i can use the starting x position as the restarts translating position i think, ill just see what i can do with that for now ig

wooden sparrow
#

when spawning everything that can change through code

#

e.g. bioshock 1 had a system that used both kind of save, one is just fresh reload of everything from last save point, and another is just respawn player without changing enemies and loot

turbid moss
#

oh i have no idea how to mess with saves and stuff... Ive been struggling tjust to get what i have and still messed up spawning the enemy

wooden sparrow
#

procedural is like... vampire: survivors, for example. it just spawns enemies randomly so it doesn't need to reset them, game over would instead remove all enemies and just spawn them again when needed
even then, it'd probably be more appropriate to just reload scene anyway

turbid moss
#

currently, i can do most

wooden sparrow
#

is there anything that would make reloading scene a bad idea?

turbid moss
#

returning the character to the start and getting the spawn to work is having issues

#

reloading scene?

wooden sparrow
#

you can google that, it's essentially same thing as exiting and entering play mode

turbid moss
#

ya couldn't restart the game in game?

wooden sparrow
#

it's just simpler and more fail-proof

#

"DontDestroyOnLoad" for the stuff that persists between each death

turbid moss
#

only the enemies spawn so far, id share a demo for ya, but ive no clue how publishing works yet

#

ill learn publishing in a week or 2 maybe

wooden sparrow
#

take your time, itch makes it really easy though

#

especially if you use webGL and don't need save system

turbid moss
#

i dont

turbid moss
wooden sparrow
#

if it's a small game then i recommend switching early, you know that people are too lazy to download stuff

#

save system is annoying because it breaks between versions unless you use a workaround

turbid moss
turbid moss