r/gamedev • • Feb 15 '20

Tutorial How To Separate your Player Code into Different Script components ►

https://www.youtube.com/watch?v=_vj1GASSO9U
234 Upvotes

17 comments sorted by

33

u/Orava @dashrava Feb 15 '20 edited Feb 15 '20

Good practice, but the execution could use some further polish.

The reference clutter in the inspector and manual fiddling to set it all up could be automated to make it foolproof and a lot cleaner.

Using [RequireComponent] would make it so it can't be set up wrong, and make it quite a bit simpler to set up further characters, since you could simply drop any of the scripts on a GO and the others will be added automatically.

Manually messing with the execution order can end up getting really bloated as the project grows. It's nice to see things visually in the execution order, but it's further manual and error-prone work you might as well skip. The main script could be initializing all the modules ensuring that things exist in the order you need them.

2

u/MegaStegz Feb 15 '20

Some good ideas and feedback, I appreciate it.

7

u/velocipenis Feb 15 '20

You definitely don't want to rely on Unity for Awake/Start execution order. Like Orava says you should have only one script on the "object" do all script setup for you. Even then using Awake or Start for that is questionable depending on everything else that needs to get setup in a scene before the player object becomes "active".

You also likely don't want any of these sub scripts having any update/fixed update/etc methods either. You'll want to have full control on the order those take place in for each of the sub scripts. Not only to get the execution order right but also to easily handle enabling/disabling them correctly for things like pausing the game.

Another thing to keep in mind is being very careful when exposing things like the rigidbody. It's best to only have it in one script and that one script is the only thing that affects it. One big category of bug I've struggled with in Unity is "who is messing with this" due to having things exposed. Where your code is right in the one script and something should be moving (or not) but doesn't because there is another script that is touching it in Update() too for some other reason. If you can't see everything interacting with an exposed property by Find All References in VS then you're in for a bad time.

4

u/HowlingHowl Feb 15 '20

Do you have anything I could read about those things you mentioned?

11

u/Raiden95 //TODO Feb 15 '20

RequireComponent from the script reference

and then you could use GetComponent<OtherScript>() on Start/Awake/OnEnable to get/cache the references to other scripts

3

u/Mikal_ Feb 17 '20

Wish there were more tutorials about this kind of things honestly. Thousands of tutorials about how to set up a character, or a SFX, or whatever, but when it comes to integrate everything together it's "left as an exercise to the reader"

2

u/BanD1t Feb 17 '20

Jason Weimann covers it pretty well. (Albeit a bit long in some cases, but you get a full explanation instead of just the best case scenario)

1

u/Gonzako Feb 15 '20

Tbh, I put most of my initialization code on Start, If something needs to be setup before that (which is normally something like a singleton pattern which is probably bad) it goes to onEnabke/awake. You don't really need much more than that

6

u/MegaStegz Feb 15 '20 edited Feb 15 '20

edited: The audio sync has self corrected. some youtube glitch. Thanks to those who let me know.

7

u/Avahe Feb 15 '20

If this is Unity specific, could you put Unity in the title next time around? And thanks for your post

5

u/MegaStegz Feb 15 '20

A fine idea! will do this next time.

3

u/Jaedong9 Feb 15 '20

Well I'm using Godot and the same concepts applies, separting your code is important regardless of the platform =)

2

u/Gonzako Feb 15 '20

I always do this! I mostly do it so the AI moves more or less the same way as the player and they feel "in the same world". Also, interface everything and you got an Iterators dream

0

u/runslikewind Feb 16 '20

Got excited because this is what i'm struggling on. until i saw it was unity.

1

u/Bmandk Feb 16 '20

If you have a similar engine that also uses components, then it's still applicable.

Generally, he's pretty close to a ECS system, which is starting to become more popular with the bigger devs. It's something you might want to look into.

2

u/runslikewind Feb 16 '20

I will thanks.

-4

u/AutoModerator Feb 15 '20

This post appears to be a direct link to a video.

As a reminder, please note that posting footage of a game in a standalone thread to request feedback or show off your work is against the rules of /r/gamedev. That content would be more appropriate as a comment in the next Screenshot Saturday (or a more fitting weekly thread), where you'll have the opportunity to share 2-way feedback with others.

/r/gamedev puts an emphasis on knowledge sharing. If you want to make a standalone post about your game, make sure it's informative and geared specifically towards other developers.

Please check out the following resources for more information:

Weekly Threads 101: Making Good Use of /r/gamedev

Posting about your projects on /r/gamedev (Guide)

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.