Tuesday, March 10, 2009

C# Delegates

I posted about C++'s function pointers a few posts back. They're pretty nifty, even though my experience with them is pretty limited. I recently learned about something called delegates in C#. They're sort of like C++'s function pointers, but more "safe". They allow you to (like C++'s function pointers) to pass a method to another method for later execution.

It pretty much works like this: You first declare your delegate for use. How you declare it depends on the type of methods you want it to be able to handle. If you wanted it to handle methods with the return type of void and that take one int, you would do this:

public delegate void MyDel(int val);

This creates a delegate that accepts methods of that same signature. After this, you're going to need a method declared somewhere that meets these parameters for it to use. Here's a short example:

public static void DelMethod(int i)
{
System.Console.WriteLine(i);
}

To use your delegate, you first have to assign the method you want it to hold, then call it somewhere.

MyDel handler = DelMethod;
handler(42);

The above code creates a custom delegate using the signature we declared above and sets it equal to the method we created. Then the next line uses the delegate to run the method stored in it and passes the number 42. The output to a console app would just be "42".

Another nice thing about delegates is that they can be added together. If I have three delegates (two with methods stored in them and one new one), I could set the third one equal to the first two added together and when I used the third delegate, it would execute both methods now stored in that delegate in the order they were stored in it.

I'm just starting to learn about this subject, but so far C++'s function pointers make more sense to me. I didn't have to scratch my head as much when learning them. That's not to say delegates aren't useful...they definitely are. It's just taking me longer to understand them fully than it did to learn C++'s function pointers. There are definitely many uses for delegates, but that'll have to be for another post. I just wanted to post about this while it was still fresh in my mind.

Thursday, February 12, 2009

C# and Such

So I'm taking a C# class this semester since most of my programming experience is C++. I thought it would be a good idea to take a formal class on it (as a compliment to learning a bunch on my own because of my internship). Well, the class has turned out to be kind of a joke. I've been learning, but what I've learned has been outside of class on my own time or through discovery with problems out of the book. The teacher isn't the best I've had at UAT. I'm hoping it'll get better as the semester goes along, but at this point, I'm not so sure. It's a 200 level class and the first two things we learned in class were "what is the internet?" and "what is a computer?". It's week 5 and we're learning what comments are and that there are two types of comments in C#.

Oh well. I'm learning more through real world experience than I am in the class, but it would have been nice to have a worth while class. My only problem with C# is that it keeps you too safe sometimes. I find myself falling back into C++ habits and getting frustrated when I get compiler errors for things that worked without so much as a warning in C++. I guess that's part of learning a new language, but right now, I don't feel like I can write in C# as efficiently as I should be able to. There are some features that aren't in C++ in C# that are time savers and all around interesting features. I guess that's more of a plus for the .NET framework, not C#, but the point still remains.

One feature in C# that I'm not 100% sure on yet is Properties. For those who don't know, they're used in classes when you have private member variables. In C++, you'd have to create two separate functions to access private data (one setter and one getter). In C#, you can do that, or you can create a property. It's declared like so:

public int SometimeSimilarToYourVariableName
{
get
{
return _varName;
}

set
{
_varName = value;
}
}

The property takes care of the set and get for you. You can declare both, none, or just one for your private data. Then you use the property like it was a member variable. Code might be something like int foo = object.SometimeSimilarToYourVariableName;. This automatically calls the get portion of the property. It's pretty smart, honestly. At the same time, it's more that it does automatically for you and while it's a really clever feature, I'm not sure about it yet. I can see it being confusing occasionally because for it to make sense, you want the property name to be similar to your private data member's name. In C++ with my dedicated functions, I would never mistake what I was doing because it was an explicit function call, not treating something like it IS the private data.

I came across an interesting article earlier today about this very feature. Someone ported it over to C++. I didn't read through the whole thing, but it's really cool that someone was able to get this kind of C# feature into C++. Regardless of whether or not it's used, it's nice to have the choice, I think.

Monday, November 17, 2008

Realmware

So I know it's been a while since my last entry, but I've been rather busy. I'm still working on some C++ work, but the most of my time has been going to learning C#. The project I'm working on switched engines again. Instead of using Unreal now, we're using Visual3D.NET. It's a new game engine being developed by a company called Realmware. Realmware visited UAT a couple weeks ago for our Tech Forum event. As it turns out, Realmware's engine was one of the engines our team was seriously considering using after we had to drop CryEngine2 because of German laws. Turns out they were coming to speak at our school. We were able to set up a meeting with their CTO, Brian Knox through Nate.

So as it turns out, they were impressed with our team and concept and we proposed a deal with them to work on their engine and pretty much partner with them so they have something (our game) to show off what their engine can do. We benefit by not having to pay for commercial licenses for the engine. Since the engine was written in C# and as a lead, I'm going to be working on engine code, I've been spending time learning more C#. Unfortunately, most of my programming skill is in C++. I've taken 2 classes where I've had to use C# (Managed DirectX, and XNA), but never a formal class on it. I am taking C# I next semester, but I need to get a jump start anyway.

With all of this going on, it's been pretty busy. It's only going to get much worse from here on out, though. Once me and the other programmers get into full swing, we should be programming around 50 hours per week. We're hoping to have most of the engine stuff we need to work on to get our game to work with the engine done by Jan 1st. While 3 of us work on that, the other 3 programmers on the team will be working with the rest of the group on a smaller project that can be finished by the time we're done with our engine stuff. By then, we can get started on our main project - which is the reason we started this in the first place. The good news out of this is that I've got an internship through Realmware now. So after next semester, I'm officially done at UAT. That's why I'm trying to get as much practical experience programming as I can now.

That's about all I have time to post about for now. Hopefully my next post won't wait as long as this one did. Until next time...

Friday, October 31, 2008

Lots of Stuff

So I got Fallout 3 the other day and it's pretty awesome. I highly recommend it to anyone looking for something new to play. I played for 6 hours today and completely lost track of time. It's been a while since I've played a game that did that to me. There are plenty of reviews out there for the game, so I'm not going in depth, but I will say that it's an amazing game and it should be played at least once. I'm about 8 hours into the game and I haven't even finished the second main quest yet. I've been busy doing a bunch of side quests and just exploring the game world (which is absolutely huge).

For our MOD project, we switched engines again. This time, over to Unreal 3. Vincent and a few other members of our team met with some of my school's alumni who worked on the COR MOD project. They gave us a bunch of tips on how to organize the team to succeed and they were the ones who said we should be going with a different engine. The advantages of U3 are that it's a more active community for modding. Not only that, but you have a bigger audience once the game is done, too. Anyway, I'm going to have to start learning UnrealScript sooner rather than later now. Not really looking forward to that, but once I have it down, the MOD should start rolling along fairly well (on the programming side, anyway).

As for my other C++ programming, it's coming along pretty well. I still want to do my own programming projects instead of these assignments, but I'm as motivated as I can be while having to work on this stuff. I want to write another entry soon on some cool stuff I learned in my class recently, but I'm probably going to change my blog theme first so I can actually use black text. Getting code samples to show up in a blogger post isn't all that easy. So look for that post coming up soon.

Puff is going fairly well, too. We had our weekly meeting last night. Unfortunately, we didn't get as good of a turn out as we have been this semester so far, but it was still pretty fun. In the next few weeks, I want to do some kind of event. Josh brought up an interesting idea to have a cook out at Galleria one day and all sit outside, smoke, and eat some BBQ. Sounds like a good time to me. If we don't go with that one, I'm still all for going to Cafe Istanbul's new hookah lounge upstairs. I haven't smoked there yet and it looks pretty nice. Not only that, I think it'd be good for a lot of the club members to go to an actual hookah bar to smoke.

Well, that's about all I have for tonight. I'm pretty damn tired. All this programming lately and trying to meet a deadline has taken its toll on me. I haven't slept much in the past few days. It's not a huge deal...I'm pretty used to all nighters and long days of work like this, but now that I can relax a bit, I'm definitely going to take advantage of it. I'll post again soon.

Happy Halloween!

Thursday, October 23, 2008

Programming, and more Programming

I'm glad that I've been getting back into the programming mood lately.  I still find it hard to get any work done at the house, but I don't really see that as a lack of drive as much as there are just too many distractions around here.  I managed to solve a couple problems with some of my code for C++ and progress a bit with my work, which has made me a bit happier, but I'm still stressed since my Oct. 31st deadline is fast approaching.

Last night, I was reading up on the STL (Standard Template Library) for C++.  That's one area I haven't had much experience yet.  Using vectors isn't very hard, but learning the meaning behind them and how to use them properly is a good thing to know.  Today in my C++ class, we went over some of that same stuff.  We got a nice little crash course in iterators, vectors, and some of the algorithm header functions from the STL.  It was a great class.  One thing I found particuarly interesting was function pointers.  You can pass a function to another function as an argument and use it within that function.  An example that uses this is "algorithm"'s for_each function.  To use this function, you pass it two iterators and a function to execute.  So if I had a function called "printIt" that is implemented as so:


void printIt
(int i)
{
    cout
<< i << " ";
}


All this does is print the value passed to it with a space after it. Once you had this implemented, you need to create a vector object and pass the values you want to iterate through to the for_each function:


vector
<int> vec;
for(int i = 0; i < 10; i++)
{
    v.push_back
(i);
}

for_each
(v.begin(), v.end(), printIt);

This would print out all of the values in the vector using my printIt function.  This works because functions are really just pointers to machine instructions for that function.  By passing "printIt" to the for_each function, you're telling it where in memory the instructions for that function are so it can use the function.  If you want to see the implementation for the for_each function, you can look it up online.  It's actually pretty simple, but you need to know templates for it.  We actually made our own implementation of it in class both with templates and without.

Anyway, I have to get back to some more programming.  I hope this was insightful for someone.  I thought it was really interesting.  Once I get more done, I'll make another post and share some more.


EDIT: Sorry about the formatting. This was my first try at putting source code in a blog post and I had to edit HTML to do so. In the process, I accidently deleted some of the post. Let me know if something doesn't make sense and I'll clarify!

Tuesday, October 14, 2008

Baseball and Programming

So today I missed my programming class due to a few things, but I've really got an itch to do some more progamming.  Hopefully I can get a project done in the next day or two and get this incomplete from last semester out of the way so I can work on the MOD project as my main priority.  

Speaking of the MOD project, it's going fairly well.  I'm going to be working on the technical document for the project in the next few days and then get together with Cullen to get the programming doc done so we can get down to business.  Vincent has been doing most of the design doc and other documentation himself as the project lead.  Things seem to be coming along quite nicely - even if we've lost a couple members already.

Game 4 of the ALCS is today.  Hopefully my Red Sox will make a comeback and tie the series at 2-2.  They need some wins if their postseason hopes are to remain alive.  I love the post season in baseball.  It's always a fun time.  I've been watching all season long and it's fun to see everything play out in the final games of the year.  I'm a huge Red Sox fan, but really as long as they do better than the Yankees by the end of the season, I'm pretty happy.

I know this was another fairly short post.  Hopefully I'll have a bit to say later today or tomorrow once I dive into some programming again.  I'm really looking forward to it.  The project I'm working on now is a custom time/date class (without using the ones built into C++).  After that, I have a vector class to write.  Fun times ahead!

Monday, October 13, 2008

New Day

So it has been a while since I've kept a blog.  The last one kind of died off after only a couple posts because I made it too focused, I think.  I tried to keep it on video game news and such and I realized that's not really what I want to do anymore.  I figured a place to keep my thoughts would help as of late because I've been more stressed lately between make up programming work, auditing a class, looking for an internship, Puff,  and working as the lead programming for the Asylum MOD project.

With Nicole back in Alabama for a couple weeks and not being sick anymore (I just got over strep throat this past week), I've had some time to do more programming.  I had planned on doing some last week, but the strep throat just took all my energy and concentration out of me.  
 
Besides all of this, I've been thinking of some fun events to do for Puff.  There haven't been many lately, and I think it would be nice to meet up and do something fun with the club outside the regular club meetings once a week.  It's actually one of the main things I plan to bring up at the next Puff meeting this Wed. night (be there!).  We've had a pretty good turn out, though.  I've always felt Puff was a great place to relax and unwind from the stress of classes, work, and project and I hope others feel that way when they attend, too.

I know this was a pretty short post, but I just wanted to get something up while I had some things fresh on my mind for the first blog post.  Once I get back into the swing of things with all this work, I should have quite a bit more to say.