Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 61 62 [63] 64 65 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 886749 times)

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #930 on: January 26, 2012, 10:17:16 pm »

Assign every enum a specific number.  You should be able to choose numbers such that the combinations you care about have sums that are unique.  Then switch on that sum.  Or use cascading ifs instead of a switch.

That's almost exactly what I already did.  I made a separate object that's just a giant ladder of if/else for reading the ConsoleKey, which is then passed back to the program as a byte the function then interprets in some way.  People were suggesting to me that I could eliminate the separate object and just make the switch statement work with the Key directly, which would be perfectly fine if a switch could read both a Key and a Modifier at the same time.

Anyway, I like my separate object, since I can just reference it from anywhere and save a few keystrokes when coding.
Logged
And here is where my beef pops up like a looming awkward boner.
Please amplify your relaxed states.
Quote from: PTTG??
The ancients built these quote pyramids to forever store vast quantities of rage.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #931 on: January 26, 2012, 10:27:52 pm »

Eh, if it forces people to do it right...
I sort of see where you are coming from, that the language is a tool, not a baby sitter, and if we want to fuck shit up that is our right, but C# was designed to be used to produce business applications, and fast. If your boss knew that there was one less thing you could get wrong, he would agree with it, and he often gets final choice of language.

I agree with the business side of it; in practicality, C# is a great language because it cares about producing working code quickly. But, as an initiated C programmer, I can't help but to prefer to take more time to learn how to do things properly and efficiently, rather than writing/testing a slop of code in a few hours and shipping it. Programmers are typically semi-perfectionists: they get something working, and then they spend their time making it perfect. Hence why I re-write large sections of my personal utility library every time a new C++ standard is released.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #932 on: January 26, 2012, 11:13:41 pm »

Gather round folks, it is time for lesson time.
So we can pass stuff into methods with parameters, and we know that when using primitives and enums, the value is passed, while with objects the reference is passed. This means that any change to a primitive inside a method will not affect that value for what ever called it.
But what if we want to affect it? What if we want to be able to change values with a method the same way we do with objects?

Lets start with some simple code.
Spoiler (click to show/hide)

Ok, so we take a value, and then we make sire it is between two other values. You would be surprised how useful that can be for games, but anyway. Wouldn't it be nice if we didn't have to reassign x after calling the method, if instead the method could just change x? Well we can do that with the 'ref' keyword! For example...

Spoiler (click to show/hide)
Now it will pass the reference to an int in, rather than copying its value over to the new method. But wait, the compiler is angry with us... Red lines over in the main! First and for most, we are still trying to assign x a value, even though the method returns void, and we don't need to any more, so we can fix that.
Spoiler (click to show/hide)
But something is still wrong. The problem is that we are trying to pass in an int, not the reference to an int, although the method now takes a reference, the thing calling it will still try to copy the old value over. To fix this, we add the ref keyword to the calling method.
Spoiler (click to show/hide)

There! Run that, and enjoy. Now you may be asking, what about objects? Does this ref keyword work for them too? And if so, how? Well yes it does, and in basically it can be used to swap objects around.
Let's bring back my favourite test class, the 'Person' class.
Spoiler (click to show/hide)
Now you know that thing from the matrix, where Agent Smith turns people into himself? That was awesome! Let's program that! First add this simple method.
Spoiler (click to show/hide)
And this to the main
Spoiler (click to show/hide)
Now because objects are always passed around by reference, it should replace Anderson with Smith, right?
Now run that and... DAMMIT! Foiled again! But this time, not because Neo went all chosen one, but because we didn't get our pointers right. See this is what happened...
Spoiler (click to show/hide)
The main was looking at Anderson, and told the method where to look by copying over the location of Anderson.
However, when it make Smith, it just forgot about Anderson, and didn't bother to do anything with the pointer in the main.
So we want to change what the main is looking at. To do that we need a pointer to a pointer!
Spoiler (click to show/hide)
So we do this with the 'ref' keyword, once again. Just like before, add it to the parameters.
Spoiler (click to show/hide)
Now when we do anything in the smithify method, we are making the variable in the main do something. Run that aaand...
Spoiler (click to show/hide)

Continues in part 2

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #933 on: January 26, 2012, 11:23:03 pm »

That was beautiful.  And informative!  Textbooks should use examples like that.
Logged
And here is where my beef pops up like a looming awkward boner.
Please amplify your relaxed states.
Quote from: PTTG??
The ancients built these quote pyramids to forever store vast quantities of rage.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #934 on: January 26, 2012, 11:44:10 pm »

Ah pointers. You know they make me long for my old flame, C++, now she was a wild one. Still, my new mistress Ms. Sharp might not be as much of a free spirit as old Plussles, but some might just call her cautious.

Anyway, if you have been playing around with what you can and can not get away with using ref, you might have noticed that you must assign a value to the parameter before you send it in. This is because when you use ref, you are making the assumption that the value has already been assigned to, so you don't need to worry about null pointers.
But sometimes you just want to get values out, without assigning them first. For this, we have another keyword. The 'out' keyword. Let clean out what we had, and start new with a new class, the 'Color' class.
Spoiler (click to show/hide)

Ok, now add this to the main!
Spoiler (click to show/hide)
See? Compiler hates it. You can not use unassigned variables with ref. You could assign a value to them before hand, but that would be stupid and pointless. Instead, we use the 'out' keyword.

Spoiler (click to show/hide)

Hey presto! We can now get multiple values out of a single function.
However, just as 'ref' requires you to assign a value to a variable before you can use it, the 'out' keyword requires you to assign something to the variable in the function you are calling, so for example
Spoiler (click to show/hide)
Is illegal, because we need to assign a value to 'b' before the end of the method.
So, ref is for when you to change something, and out is for when you want to get values out.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #935 on: January 27, 2012, 12:02:17 am »

That is a remarkable addition to the basic references that C++ has. I'm beginning to like C# more and more.

Also pointers are great.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #936 on: January 27, 2012, 12:38:42 am »

That was beautiful.  And informative!  Textbooks should use examples like that.
Oh god, it just hit me... Passing values is Roxy's favourite language feature, because it means she can always set the return type to void.
Horrible, horrible puns...

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #937 on: January 27, 2012, 01:03:09 am »

So, I downloaded NetBeans.

Now what?
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #938 on: January 27, 2012, 01:09:20 am »

Uninstall and get eclipse?
Seriously though, never used netbeans, so can't say for sure. You have the JDK right?

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #939 on: January 27, 2012, 01:10:33 am »

Indeedy, but I have no idea what I'm doing with it.
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #940 on: January 27, 2012, 01:13:27 am »

Well they should have both come with install files, have you tried running them?

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #941 on: January 27, 2012, 01:14:30 am »

Install ruby so I can get a turn with the bright-eyed young recruits. ;)
(not really, I mean, do if you want, but I don't have time for anything approaching the quality of Max's lessons)
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #942 on: January 27, 2012, 01:16:27 am »

Eh, post some of the basics some time, always interested in new languages.

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #943 on: January 27, 2012, 01:16:53 am »

eclipse is definitely the better IDE compared to netbeans.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #944 on: January 27, 2012, 01:23:32 am »

Install ruby so I can get a turn with the bright-eyed young recruits. ;)
(not really, I mean, do if you want, but I don't have time for anything approaching the quality of Max's lessons)

I'd be happy to learn it, but I'm in classes for Java and C++(2nd semester) right now. I'm afraid I'd get confused.
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting
Pages: 1 ... 61 62 [63] 64 65 ... 796