Hi, I have a question and i’d like to get different opinions on the matter. Basically my question is this: How do you think like a programmer? What i mean is, when u get an idea what helps u turn the idea into code? how do you think? I ask because this has always been a problem with me, I never had trouble learning how to program but i had trouble using what i know effectively, I had this trouble when I first started trying to use PHP while i can use it well enough now i still cant quite grasp the right mindframe when it comes to programming. I was wondering what others do, and what advice others can give me to use my knowledge of programming to produce the ideas that are in my mind
Some people just naturally think that way, some don’t. If you don’t, the only thing I think you can do is practice, look at other peoples code that’s really efficient to maybe learn new ways of doing things, and just lots of experience.
what they teach us at school: programm in blocks. and dont just rush off, write whole code, and find out that its not working.
I find it helps to always try to think of the concept as it’s component parts. You know what you want to accomplish, but how would you explain it to something without any knowledge of the required concepts. You need to be very descriptive to yourself and to others. Once you’ve broken it down into simple, descriptive parts it’s much easier to write logic for it. Creating a proper 3D platformer character controller can be complex, but making him move forward, turn, jump, have momentum are all MUCH simpler to accomplish individually.
But that’s kinda how I see it, but others probably have different methods. After you’ve got breaking things down into small parts, the next practice is to be able to still properly keep in mind the whole concept and how all the small parts will fit together in end.
Experience and practice can also help as you will gain familiarity over common patterns and concepts, as well as more unique tricks and caveats.
I’ve always looked at it as a simple question of systems management. You say “What items do I need to hold the data, what regular interactions with the data will I have, and what does this data need to react to in-game inputs?” From there, you create all the variables you foresee yourself needing, create all the common functions you believe you’ll need, and then use that as your core for the game.
Seems to work quite well, but only if you have a thorough understanding of what the game will need to do.
Thanks for the tips but most of this stuff i already do, I also am having another issue… I haven’t programmed in about 8 months , and i find it’s hard to start writing code again, for instance I wrote a decent health script awhile back, and im trying to rewrite the same script but it seems i’m getting compiler errors. I have tried the many things i can think of that may fix them but it seems I am having no success, so another question i have to ask is this normal? i’ve never had this problem before its like i forgot…
oh, I forgot to mention my previous script worked flawlessly
Focus on the goal, not the tool. Define the problem you are trying to solve in your spoken language. From there, again in your spoken language, list the elements you think you’ll need to get it done. Then define the tasks those elements will need to perform. This will invariably lead to sub-elements with actions and tasks of there own. As elamre says, work in blocks.
You may want to google “use case” And “functional design document”. You don’t necessarily need to do all that, but pay attention to the approach. The main thing is defining what you want and then breaking it into it’s constituent parts.
Thanks for the advice guys, it appears i’m getting my mind to work right again here is a script i just wrote
Now it might be a little rough, but it works and i was just fooling about all it does is rotate a cube in a circle while spinning it
another variation me again just practicing and having some fun not really useful to anyone
For me, the first thing to do is step away from the computer and grab a notepad, start writing in pseudo code (this is a description of what is happening, and the steps you need to take, and is not code at all). For example, if I want to have a function that enables me to click on one enemy at a time and highlight them:
Is my mouse over an enemy object?
If it is…
Cycle through all enemy objects and check if any are selected.
If another enemy is, take off the highlight.
If no enemy’s are now selected, highlight the one I’ve clicked on.
Then, you can break down the steps even further, so that last statement will become:
Set some property on the enemy to indicate it is selected
Highlight the ground around the enemy as red
And then you can keep breaking down, so the last line might become:
Add a new directional light to the enemy
Set it’s colour as red
Rotate the light to face the ground
Turn the light on
This is a very good way of figuring out big problems. It s very important though that you consciously steer clear of using any syntax or coding at this point. The idea is that once you’ve broken your problem down in to logical stes, you can then go and tackle each step on its own. Hope that helps!
pseudo code Has always helped me in web programing and i find it useful here, thanks for the suggestion…
natural problem solving and experience. You just have to keep solving problems. Most of your problems will actually be solved with a good design of code, if statements, the occasional loop, and thinking about what is required with variables. The best example is for you to create a program mentally for brushing your teeth:
you need variables for the toothbrush, how long to brush for, if there is remaining toothpaste etc and so on. This is the grunt work of programming, it is basically a well-oiled machine that will deal with the issue at hand.
I solidly look at the result and what tasks are needed to accomplish it through code. I then theorize what I have to do, which breaks it down into smaller steps that can be accomplished. I will then think of it in pseudo code and work on it from that angle. In most cases I will break down methods within the pseudo code that could be repeatable and make functions out of them. Sometimes it works out other times it does not, but having a simple function to do something means that you don’t have to weed through a thousand lines of spaghetti code to get your solution.
Overall problem:
I want my CharacterController controlled AI character to move to and attack a player.
Breakdown:
I need for my CharacterController to face my player.
I need him to move to my player
I need him to attack my player when he is within range.
Because we are working in “time” not all of this can be accomplished at the same time.
Pseudo code:
Face My Player
Set a direction variable (target - transform)
set the move to the normalized direction
if we are close enough to the player
set the move to zero
if time < last attack + attack time
attack
set last attack to now
apply gravity
do CharacterController Move
Each line should be 1 to 2 lines of code and that is it at this point.
Other things can be added to this, like if the player is too far away, a return code to return the creature back to the spot he was if the player dies or gets too far away from his the monsters origin point.
Thanks guys, now I have another question Over time is it easier to Figure out what approach to take to implement the features u want? for instance say I wasnt to shoot a bullet, but off the top of my head I cant think of what the best functions/Classes to make that happen, I know the steps but I have to look at others code Use the scripting reference or think harder than I should. Will it get easier for me to get the right thought process the more i learn about each function, each class ect… I know enough to put it together but not enough to say, “hmm ok i need to do this and that, there we go I’m finished” without putting too much thought n time into it.
If you are a programmer and want to impeove go and read Code Complete and Writing Solid Code. You won’t regret it. These two books should be required reading on all college courses and for all programmer interview candidates.
Yes. It gets easier. Or harder in a way because you start solving much bigger problem. You build up a roadmap in your head of what each API does and where code sits. A lot of software development has very little to do with writing pure code. ![]()
It gets easier. Learning the “3d” math will make your life easier. Remembering certain methods will help too.
All 3d is relative to 1d.
direction is always “target - position” (yes, 20 - 10 is 10… thus you move forward, 10 - 20 is -10… thus you move backward)
local 3d is not global 3d. (transform.forward is almost never vector3.forward)
“OK, I need to do this and that” is “Pseudo code”
“There we go, I’m finished” is ALWAYS preceded by “OK, tested it and got this error, and this error and now it doesn’t work at all?!!!?!!”
Patience is a virtue, not a talent.
All depends on how you approach it. Once you’ve finished a project that has your first bullet, go in to the code, pull out the bits related to the bullet and rewrite it so you can ‘drop’ it in to any other projects that need the same thing. Make sure the code acts very generic, takes all the info it needs and returns stuff thats useable. Over time you’ll end up building your own library of code, little snippets, and you’ll start to constantly rework them as you get on (working on lowering the memory they consume, etc).
This goes back to writing functions that do stuff. As you progress in concept and execution, you will learn what takes less and more time to compute. If you write things out as simple functions to start, when you replace those functions with a newer one that operates cleaner, you can easily substitute an old function for a new one and have no “fixing” in the actual code.
If it was all written in spaghetti code, then you would have to go find instances of that type of function and replace it in the code, much harder to work with at that point.
I hear a sticky on pseudo code a’coming…
Heh, it would be nice, but pseudo code is not programming, its theory. It would be hard to write a serious thread about something everyone does differently. ![]()
What my biggest set back is right now, is that I dont know nearly enough to be effecient or effective, for example let say in theory I know what i want and I know the steps i need to take to get to where I want, However I dont know what to do. What i mean is I dont know what built in functions do what, what classes that might be useful and so forth. I know the basics and what a function is what arrays are how to use arrays, variables ect… for instance here is a script i wrote following the burgzerg tutorials but I wrote it in Unity Script not C#
var targets : List.<Transform>;
var go : GameObject[];
var selectedTarget : Transform;
private var myTransform : Transform;
function Start(){
targets = new List.<Transform>();
selectedTarget = null;
myTransform = transform;
AddAllEnemies();
}
public function AddAllEnemies(){
go = GameObject.FindGameObjectsWithTag("Enemy");
for(var enemy : GameObject in go)
AddTargets(enemy.transform);
}
public function AddTargets(enemy){
targets.Add(enemy);
}
function SortTargetsByDistance(){
//Supposed to target nearest enemy but I couldn't figure out how to write it
}
function TargetEnemy(){
if(selectedTarget == null){
selectedTarget = targets[0];
}
else{
var index : int = targets.IndexOf(selectedTarget);
if(index < targets.Count -1)
{
index++;
}
else{
index = 0;
}
DeselectTarget();
selectedTarget = targets[index];
}
SelectTarget();
}
function SelectTarget(){
selectedTarget.renderer.material.color = Color.red;
var pa : PlayerAttack = GetComponent("PlayerAttack");
pa.target = selectedTarget.gameObject;
}
function DeselectTarget(){
selectedTarget.renderer.material.color = Color.gray;
selectedTarget = null;
}
function Update () {
if(Input.GetKeyDown(KeyCode.Tab)){
TargetEnemy();
}
}
It fully working but I know I went about doing some things wrong especially with the arrays… It took me about 3 hours using google and the scripting reference to get it fully working. I learned some things and it was good experience but like i said it wasn’t easy since i’m learning on my own and I don’t fully understand the API