Coding and repositories

2021-09-23T21:00:00Z

Hi everyone :v: :sunglasses:,
I hope that I am sharing in the right place. I doing Complete C# Unity Game Developer 2D course. and I would like to share the importance of having a repository either local repository or a GitHub repository.

Repositories:
Repos or repositories are such a containers or boxes that you store something in it. but when we talk about repos in the technology field the repos are containers that stores your code and it’s history.

Local Repositories:
You may wonder why would you have local repo while the actual file of the code saved in your computer. as I have mentioned repositories stores the history of our code. Imagine with me, that you are adding a new feature or functionality to your game, then you started having a lot of code going on then you faced a lot of bugs, and you tried to back to the code when were everything is working. isn’t that frustrating, and it might sometimes take few days to return back to that point, where your game was working. and you might can’t return back.

with local repos you will be able to make checkpoint such in games to return to them back whenever you want, as might something wouldn’t work or you just want to return back. For example, you have made 3 checkpoints 1, 2, and 3. and when you were working some issues raise up then you can in few seconds return back to checkpoint 2 and have everything working.

GitHub Repositories:
GitHub repos are the same but the difference that you have backed up your code in the cloud, so, it could be accessible from anywhere. Also, you can collaborate with other to share you code or even work in one project and merge the code.

Branching:
also, a great thing that repos has is that you can work on a feature without making changes to the main work. Let me demonstrate, you have your game 100% working and you wanted to develop a new AI system or adding a feature but without missing out with the game which might result that your game might not work. So, you can make a branch! what is the branch? from the word branch itself, you make a separated environment where you can code your new AI or feature without touching your core game code. in a result of that your game remain working for your audience if you have launched it, while bringing up a new feature.

In Conclusion:
The process of doing that called Version Control. You don’t have to be tech guru to learn how to make repos, it is not a rocket science. It is a good thing to use repos either local in your machine or in the cloud. and the important thing is practicing and get use to use repos. I might not have covered everything about the repositories. and pardon me if I had any grammar mistakes or did not elaborate in easy way to understand easily. and for my lengthy Topic.

  • This was useful :innocent:
  • Sort of :thinking:
  • Naah :face_with_monocle:

0 voters

While they are most prominent, github.com is not the only provider for storing git repositories “in the cloud”, there are others as well, for example gitlab.com and bitbucket.org (and there were others before github, for example SourceForge).

On important thing to mention (especially with Unity) is the option to put a filter list into your git repository (it’s a text file called .gitignore) that you should add before you do your first commit of the project (a “commit” is what the top post calls a “checkpoint”). You really don’t want to keep your “Library” folder (and a few more things) stored inside git.

  • “Library” is quite large, “Logs”, “obj”, and “Temp” have no real use for keeping
  • Everything inside can be rebuild by Unity
  • Its contents get changed independently of what you commit into your project (Unity caches temporary data for which there is no need to add them into git)

Also, commits are cheap. Do them often in small increments. If you always work inside a branch that you later merge back into the main (or “master”) branch, the commit history will tell when you finished working on a feature and started doing the next one.

Using version control is essential especially if you’re not working alone. It’s how you share the project with other in order to work concurrently on it.

One more thing to be aware of: Merging changes in the code parts of the project where two (or more) sides worked in is relatively easy, even if both sides changed the same script, git can usually merge the changes and if there’s a conflict it doesn’t resolve, you can read the code and determine what to keep.

With the other files this is much harder. Images are binary files where git can only keep the old and the updated file, but not really have a difference between them - giving concise descriptions on what was changed in the image for the commit message is therefore important. Unity’s own files (like the scenes) are stored in a textual format, but changes within are more difficult to follow (and really hard to resolve if merge conflicts occur).
Therefore, if you’re working in a team, don’t work inside one scene at the same time.
Better have a test scene for each member of the team, that won’t be added to the published builds.

1 Like

@heckert, You mean (.gitignore) file where I specify the files that git will be ignoring when I make a commit, rigfht?

and yes you are totally right as I would not keep other folders and files into git. it will not be useful to back them up there, cause unity would not figure out the differences of image files.

and what do you mean by,

In fact when I was using git and within assets folder I have made a couple of Filename.txt files to test it out. but I found unity made new other files as in Filename.txt.meta and I think that is the cached files made by unity. but is there a way to stop this or should l leave it as it would be useful in future.

and thank you @heckert for adding this information it was so useful.

What I meant is that Unity will rebuild the Library folder of the project, which is why nothing inside of it needs to be committed into the git repository.

As for the meta files, that’s a different story. Among other things they have some internal ID that Unity assigns to files that are created or imported. Inside the Assets folder every file has a corresponding meta file attached. It’s important to commit the meta files into git!

  • Connections between objects are made using those IDs, so if you have an image file for a sprite and add it to a game object but forget to include the meta file with the commit, what will happen on another computer (or if you cloned a copy into another folder on your machine, so you only have what you got from your git repository), then Unity will generate a new meta file with a new ID. As a consequence, all links made using the original ID now point to nowhere.
  • Also, for some binary file type you can make configurations in the Inspector and those are saved into the meta file. Again vital data that would get lost if you did not add the meta file to git.
1 Like

One more thing to be aware of is how to handle renames and re-organizations of your project data.

If you rename an asset within Unity, the Unity Editor will ensure the associated .meta file will be kept at its side or renamed along with it. If you’re not careful, it’s possible that git will lose track of the file’s commit history and think there’s one (pair of) file(s) that got deleted and new ones were added.

If you rename the file using git’s command set ("git mv") then you can be sure the commit history will stay intact. But since git knows nothing about a file and its meta file being “an item”, you have to be careful to handle the meta file as well. Otherwise, Unity will generate a new one and treat the existing meta file as an orphan.

The best workflows I have found, so far:

  • Rename in Unity
    • Commit changes, so your work copy is in a clean state.
    • Rename files from within Unity
    • Commit just these changes, using “git add” first and checking if git correctly detected a file rename
  • Rename with git
    • Use “git mv” on both, the asset file and the associated meta file
    • Switch back into Unity and let it re-detect the asset under its new name / in its new place
      • Since the asset file has a meta file already, Unity will use it, and thus keep the reference ID intact

One of these days I should make myself a shell alias or write a bash script to do the renaming for me…

1 Like

Thank you again @heckert, I really appreciate what you have shared and of course others will learn too.

regarding the shell alias and scripts, I am using Powershell. I have made variables that lead to different directories to ease the reaching and it is faster than the usual using of double click. Also, I have made some functions that automate simple tasks and it worth it.

For the alias, they are great to be used when you make your own functions or variables name. what I would say here, that making scripts with powerhsell is easy. and always try to make it simple to reach your task.

here is some example:

  1. Declaring a variable:
    $VariableName

  2. creaing a funtion:
    one of the guides when you are naming your function you may name it as verb-noun

funtion Print-Welcome(){
Write-Host "Hello World!"
}

if the funtion name was lengthy you might set alias to that funtion.
Structure:

New-Alias -Name "AliasName" Funtion-Name

Example:

New-Alias -Name "Welcome" Print-Welcome

and I would share a function that search on google whatever you type in it. then it opens the default browser and search on google what you have typed.

	#This function used to search in google
		function Google-Search{
			Param($Search)
			start "https://www.google.com/search?q=$Search"
			Write-Host ""
			Write-Host "The link is opened successfully." -ForegroundColor Green
			Write-Host ""
		}

The function called Google-Search and it has a parameter which represent the variable that you will pass to it. The variable will be used in the search link of google. you can try it by yourself.
open browser > search in google anything > then check the link up and you will find something similar to https://www.google.com/search?q=(What you have searched for)

Sorry I might be drifted away from what is the topic I have create about “Coding and repositories”.

I can’t comment on “PowerShell”. It’s nothing that exists on my system nor would I voluntarily install it. (I am aware it exists, but I really see no use for it.)

1 Like

I would not agree or disagree with you. as it depends on the person who is using it. Either PowerShell or any other CLI.

While command aliases and shell functions may be a new thing in the Windows world, UNIX had them ages ago already :wink:

1 Like