When and how to properly use scripts?

9
Guitarmaniac24 #657 0 40

Hey everyone,

Very new programmer here. Just getting into learning how to make games with code (C# specifically) vs drag n drop engines.

So I just recently began to get a sense of what a script actually is, and how it can be used in game development.

My question is. What is the proper and best practice usage of a script in terms of ease of workflow and game performance?

For instance, (as a completely random example) if you created a player character and wanted that character to be able to run, shoot various weapons and have some kind of anti gravity effect applied to the player, what would be the best method for writing and assigning these scripts?


Should they all be assigned to one overall "Player" script? Or broken down into individual pieces focused specifically on that one aspect?

8
TheDoormouse #661 0 26

Well... really a game is all script. Most engines (I'm assuming you're referring to Unity?) have some basic things, like player controllers (movement, etc), created for you.

I would think of them less as scripts, per say, and more of functions. For example, say you found yourself constantly writing the same code all over your project just to move your player to the center of the map. Instead of writing this code all over the place, you could just create a reusable function to do it for you.

(This is in Javascript, but the concept remains the same)

Say you have a player object named MyPlayer. When your player is hit by an enemy, you want to center them on the map. Now, you could write this out in the logic that determines when your player is hit, but what if you also wanted to center the player at the start of the game? or if they reached the goal? You would need to create a function kinda like this..

function centerPlayer(payerID, targetPosition){

playerID.position.set(targetPosition);

};

Now, whenever you need to center your player from any other script, you could just call the function.

centerPlayer(MyPlayer, MapCenter);

You'll need to decide where and how you'll need bits of scripting depending on how many times its used. Do you have more than one moving character? Maybe you'll need a movement function. Find yourself needing to calculate the distance between two objects? Function it.

With C#, you'll also be using Models to define an object and it's settings. This will be for things like traits (CAN jump, CAN run, Color, size, move speed). You'll use these settings in your functions to determine how your object will react to the script.

To be honest, C# still confuses me sometimes and I use it in my day job. 

9
Guitarmaniac24 #657 0 40

Interesting...this makes sense.

So a function can basically act as a tool you can use in multiple places, or as a single instance for a certain game object?

And if I understand correctly, there's not really any one preferred method to sorting and splitting scripts? Say I had 1000 scripts, all handling different smaller in game things. Would this affect performance while running the game or would they all be consolidated either way and produce the same result as say 10 scripts?

8
TheDoormouse #661 0 26

Oh god. That would definitely affect performance lol. If your script can be simplified, it should be.

To answer your question, that depends on what you need it for. You can set it up either way depending on if you're only using it when that function is called, or if it's used frequently by a lot of functions. I haven't really used Unity, so I'm not sure how it's structured, but the concept is the same.

If you haven't already, I'd really suggest downloading Visual Studio and creating a few simple C# apps if you're interested in custom-coding your game. Once you get used to the language, I'd start moving into Unity from there. There's TONS of documentation online to get you started.

9
Guitarmaniac24 #657 0 40

Thanks for the tips! Yeah I'm very very new to coding so just trying to wrap my head around all the different types of input. Currently working my way through the unity basics tutorials. There really is a lot of content out there.

You mentioned simplifying scripts. Is there a specific way this should be done (combining related scripts, shortening code?) etc. or is there another aspect I should be thinking about?

8
TheDoormouse #661 0 26

Kinda all of the above? If there's a way to shorten/consolidate your code or make it reusable, it's always a good thing. Not only for the length/performance of your code, but also for your own sake and sanity All that typing adds up pretty quick, even if it's a few short lines. Once you get started, pay close attention to any of the best practices given and naming conventions especially with C#. There's a big difference when things are capitalized, or words are made plural in C#. For example, Item, item, Items, and items would all be read differently in C#, but there's a good reason for that.

Good luck! Once you tackle the general concepts, it gets a lot easier really quickly.

9
Guitarmaniac24 #657 0 40

Good to know! Thanks for the help!

Definitely going to try and get into good habits from the start. 

Showing 1 - 7 of 7