Generic game programming / C# thread

Anything else
User avatar
Korban3
Posts: 4146
Joined: Tue May 31, 2011 9:14 pm
Location: 42nd St E, Hell

Re: Generic game programming / C# thread

Post by Korban3 » Wed Sep 14, 2011 9:13 pm

Wait. Why would I want the 8 neighbouring tiles' states to change the center tile's state? Are we making a flashing checker board? That's what my mind generated at least. :lol:
EDIT: Typos

User avatar
Endoperez
Posts: 5668
Joined: Sun Jan 11, 2009 7:41 am
Location: cold and dark and lovely Finland

Re: Generic game programming / C# thread

Post by Endoperez » Thu Sep 15, 2011 1:45 am

Korban3 wrote:Wait. Why would I want the 8 neighbouring tiles' states to change the center tile's state? Are we making a flashing checker board? That's what my mind generated at least. :lol:
EDIT: Typos
Check the link. They simulate life. A lone specimen dies out without reproducing, and if there's too many creatures around there's not enough resources left and population plummets. Also, the numbers chosen result in interesting stuff, like repeating patterns, moving patterns, and sometimes complex moving patterns that spawn less complex moving patterns.

User avatar
Endoperez
Posts: 5668
Joined: Sun Jan 11, 2009 7:41 am
Location: cold and dark and lovely Finland

Re: Generic game programming / C# thread

Post by Endoperez » Thu Sep 15, 2011 1:35 pm

I'm slightly proud of myself. I wrote the dialogue handlers:

PlayerLine class stores a single line the player says, and a keyword that tells the game how the AI should react to this reply.

DialogueLine class stores a keyword, compared to the ones in PlayerLine to find how the AI reacts to the player's reply. It also stores whatever the NPC's is going to say, and which PlayerLine's the player can choose from.

So a DialogueLine 'start' might welcome the player to a bar and ask if he wants beer. The choice 'Yes' and 'No' would be PlayerLines. 'Yes' would have keyword 'get_beer', and would result in the AI responding with the 'get_beer' DialogueLine.

All the DialogueLines of a single NPC are stored in a Dialogue class, which has a List containing all the separate DialogueLines and methods for handling them.


I did all that without testing them out. So next, I wrote a filereader to read dialogue from a file and convert it into objects of the previously mentioned classes. Here's an example of the format I chose:

Code: Select all

#keyword
A keyword marks the start of what should be stored as a new DialogueLine.
Anything without one of the three symbols # : > in the beginning 
is interpreted as random NPC babble, including newlines.

So it's easy to format AI speech.
:This is player's reply
>next keyword
: This is also a reply
> another keyword
All this took me perhaps two or three hours. Then came the time to test it, for the first time. After I realized that my file was named "test.txt.txt" (thanks, Windows... ) and got the code to find the file... it worked on the first run. Well, it didn't know how to read Scandinavian letters and it missed one of the DialogueLines, but still, I didn't actually expect it to work that painlessly!



TL;DR: I wrote a semi-complex piece of code that reads info from a file and converts it into objects, and it worked on the first try.

User avatar
Korban3
Posts: 4146
Joined: Tue May 31, 2011 9:14 pm
Location: 42nd St E, Hell

Re: Generic game programming / C# thread

Post by Korban3 » Thu Sep 15, 2011 4:45 pm

Oh, haha! I thought you were talking about the board game. That sounds much more fun to make.
That dialogue thing is sweet. Congrats on it working the first time you ran it.
I'm going to have to do something for dialogue soon as well, but I've still got a couple details to work out. I'm going to begin the code today and maybe try to write a quicky theme too. I still have to learn to do collisions eventually. Basic stuff first.

User avatar
Korban3
Posts: 4146
Joined: Tue May 31, 2011 9:14 pm
Location: 42nd St E, Hell

Re: Generic game programming / C# thread

Post by Korban3 » Thu Sep 15, 2011 8:11 pm

Hmm, got a class set up for characters. Working pretty good and it's helping to keep my empty main.cpp source code clean :lol: I'm also setting upa class for items to be derived into subclasses for each item type, but I haven't learned about data structures yet, so that's what I'm learning about now. Although my main.cpp is mostly empty, I feel like these classes are pulling this a little ways forward. Most of the variables now defined in my Character class would have been defined at the top of my code and look all gnarly. Hopefully, I can get this data structure thing (They're fun so far :D) put together tonight and start getting it set up to handle my entities. Also, I'm jamming to some rad 80's tunes right now. Make this so much fun. *Pops and locks way back to code*

User avatar
Korban3
Posts: 4146
Joined: Tue May 31, 2011 9:14 pm
Location: 42nd St E, Hell

Re: Generic game programming / C# thread

Post by Korban3 » Fri Sep 16, 2011 11:40 pm

I'm banned from the Wolfire IRC (???) so this question is going to live here for right now. Anyone know how I can access all of the instances of a certain class at one time? I'm trying to write an update function that updates positions by adding velocity to each axis every time it's called, but I don't know how to access each instance of a class.
I can create a character called Fred via my class and work with him after that, but I want this update function to automatically do this for all instances of Item, Character etc. Am I just being a total dork and missing something?

User avatar
Endoperez
Posts: 5668
Joined: Sun Jan 11, 2009 7:41 am
Location: cold and dark and lovely Finland

Re: Generic game programming / C# thread

Post by Endoperez » Sat Sep 17, 2011 2:26 am

Korban3 wrote:I'm banned from the Wolfire IRC (???) so this question is going to live here for right now. Anyone know how I can access all of the instances of a certain class at one time? I'm trying to write an update function that updates positions by adding velocity to each axis every time it's called, but I don't know how to access each instance of a class.
I can create a character called Fred via my class and work with him after that, but I want this update function to automatically do this for all instances of Item, Character etc. Am I just being a total dork and missing something?
The only way I know is to call Update() for each instance specifically.

One way to do it would be to collect all items, characters etc into lists, and update those. They could be in either the class itself (works great for e.g. Particles), or in your level (Level.Update() includes character.Update(), items[j].Update() etc). The latter is better if you have different levels, and so the generic list would go through unnecessary stuff. Particles can be handled in a static way since you can just delete all the old ones when you go to a new level.

User avatar
Korban3
Posts: 4146
Joined: Tue May 31, 2011 9:14 pm
Location: 42nd St E, Hell

Re: Generic game programming / C# thread

Post by Korban3 » Sat Sep 17, 2011 9:18 pm

In the level sounds useful, especially once I have a level structure made. I just don't know how to create that list of instances and add or remove instances on the fly. What's the syntax for accessing a specific entity of a class? Does creating the list go under int main() and updating it go under the constructors and destructors of my classes? That's what my first impression is of the idea.
I hope I'm not too sick to work on this tonight. I haven't eaten much and I was out all day on my bike visiting my cousin. My stomach is all kinds of funny right now.

User avatar
Endoperez
Posts: 5668
Joined: Sun Jan 11, 2009 7:41 am
Location: cold and dark and lovely Finland

Re: Generic game programming / C# thread

Post by Endoperez » Sun Sep 18, 2011 4:01 am

You create an empty list first. You should only create new particles by Add()ing them to the list.

I can't really explain it well, so here's what I've been doing so far.

Code: Select all

public class Particle
    {
        float life;

        // the list that holds particles
        public static List<Particle> particles = new List<Particle>();

        // constructor for a single particle
        public Particle(float startingLife)
        { }

        // this function, or something similar, is called 
        // to add particles to the list
        public void AddParticle(float startingLife)
        {
              particles.Add( new Particle(startingLife) );
        }

        // cycle through list and update all particles
        // dt is delta time, amount of time
        // that passed since the last Update() call
        public static void UpdateParticles(float dt)
        {
           // note that this starts from end and cycles towards 0 - this way, 
           // removing particles doesn't affect the index of particles not handled yet
            for (int i = particles.Count - 1; i >= 0; i--)
            {
                particles[i].Update(dt);

                if (particles[i].life < 0)
                 {
                    particles.RemoveAt(i);
                 }

            }
        } // end UpdateParticles()

        // update a single particle
        private void Update(float dt)
        {
            life -= dt;
            // update position, rotation, stuff
        }

        private void DrawParticles(SpriteBatch sb)
        {
            for(int i = 0; i < particles.Count; i++)
            {
                particles[i].Draw(sb);
            }
        }

        private void Draw(SpriteBatch sb)
        {
            // draw single particle
        }
In C# and XNA Environment, delta time is calculated like this:
float dt = gameTime.ElapsedGameTime.Milliseconds / 1000.0f;

User avatar
Korban3
Posts: 4146
Joined: Tue May 31, 2011 9:14 pm
Location: 42nd St E, Hell

Re: Generic game programming / C# thread

Post by Korban3 » Sun Sep 18, 2011 9:34 pm

Ok, so List<className> is what I want to use to catalog them? I haven't heard of those yet. That solves that part. I was trying to think of a system using an array of pointers to each instance.
Hmm, looks like List<> isn't a template in C++. Is there a file I should be including for it? I'll start working on my array setup, and maybe figure out something better later.

User avatar
Endoperez
Posts: 5668
Joined: Sun Jan 11, 2009 7:41 am
Location: cold and dark and lovely Finland

Re: Generic game programming / C# thread

Post by Endoperez » Mon Sep 19, 2011 1:17 am

Korban3 wrote:Ok, so List<className> is what I want to use to catalog them? I haven't heard of those yet. That solves that part. I was trying to think of a system using an array of pointers to each instance.
Hmm, looks like List<> isn't a template in C++. Is there a file I should be including for it? I'll start working on my array setup, and maybe figure out something better later.
That was C#! I don't know is C++ does Lists in the same way.

Array is a data structure with pre-set length. C++ might have ways of changing the length on the fly, but it's quite complicated in C# at least.

List is a data structure into which data can be added at any time, and it has no pre-set length. That's why I used lists instead of arrays. Look up, or ask, about lists in C++.

User avatar
Korban3
Posts: 4146
Joined: Tue May 31, 2011 9:14 pm
Location: 42nd St E, Hell

Re: Generic game programming / C# thread

Post by Korban3 » Mon Sep 19, 2011 5:15 pm

Hmm, yeah. I just got the idea fleshed out today. There is some kind of list thing in C++, but I'm not familiar with it and dynamically assigning and unassigning memory to arrays isn't too complex. I'm pretty close to getting my array set up for characters and then it's just rinse and repeat for items. I plan on using this array to update positions as well as call drawing, so one cool side effect of using an array full of pointers is that if I have a character Firefly at array element 3, and I create a new character instance of horse in that element space the firefly will be omitted from the next draw and update calls and be replaced with a horse. I'd still have to destroy the instance of Firefly itself, but this at least leaves that part behind the scenes. For now, I have a max number of characters set, and if try to go past that I can stop the character from being assigned to the array, or I can cycle back through the array replacing old ones. That should be useful for debris effects. Next step is getting the update function constructed, and then drawing things on the screen which should be a little easier than the last few things. Blah, blah, yap, yap, dribble, dribble, dribble.
How's your game coming along Endo? Still adventury?

User avatar
Endoperez
Posts: 5668
Joined: Sun Jan 11, 2009 7:41 am
Location: cold and dark and lovely Finland

Re: Generic game programming / C# thread

Post by Endoperez » Tue Sep 20, 2011 12:09 am

Still working on it, not sure if it's going strong.

I have done:
Scenes, with simple navigation meshes, where you can walk and click on items. When you click an item that can be activated, the Activate() function is properly called. NPCs can talk, dialogues are read from a text file that the writer-guy can easily edit, and clicking on certain objects moves you to another scene.

That's all the basic building blocks of an adventure game, there.

Unfortunately, all the complex building blocks (like this one barrel we need for the game, or that dog, or the meat that you feed to the dog) are still missing, and there's less than a week left to implement them all.

I'll meet the rest of the team in about an hour and we'll see what that graphical stuff looks like.

User avatar
Korban3
Posts: 4146
Joined: Tue May 31, 2011 9:14 pm
Location: 42nd St E, Hell

Re: Generic game programming / C# thread

Post by Korban3 » Tue Sep 20, 2011 4:55 pm

Hmm, farther along than I am ^_^ Sweet though. I hope it goes well. Why are you such a tight schedule? Is it for a competition?
I actually just got a single sprite in my window, first non-behind the scenes thing. But I'm going to begin the setup for loading and drawing all the sprites for characters. The hardest part of this will be putting a function together that cycles through every character and item in order to move, apply basic physics, collide, draw, and animate them. A lot of that will happen there, so I'm still very deep in under-the-hood work. Lovin' it.
I hope whatever your project is all about goes really well. It sounds like it's going to be super awesome once it's done.

User avatar
Endoperez
Posts: 5668
Joined: Sun Jan 11, 2009 7:41 am
Location: cold and dark and lovely Finland

Re: Generic game programming / C# thread

Post by Endoperez » Wed Sep 21, 2011 1:19 am

Korban3 wrote:Hmm, farther along than I am ^_^ Sweet though. I hope it goes well. Why are you such a tight schedule? Is it for a competition?
It's for a competition, but that's not why we're in a hurry.

Basically, people in my new school who started with me have to decide between game studies, and doing data structures and IT hardware and networks and that kind of stuff. The latter isn't anywhere near as glorious as games, so the teachers try to make people understand that making game isn't all sunshine, lollipops and rainbows. So not only are we taking part in Microsoft's Imagine Cup, but we have to do two practice projects (few weeks each) before starting the actual Imagine Cup competition project.

It's quite clever, in my opinion. There are people who just started here and haven't programmed at all, people who haven't ever worked in any sort of a game, etc. They get a chance to learn the basics in a project that isn't very important. Everyone learns that there's never enough time. Some people realize making games is tough, and try the IT side of the competition instead, and might decide to focus on that instead. Oh, and since we have to change groups between each project, we learn something about how well other people keep up under pressure, how good they're at deadlines, and if it's fun working with them.



Wow, another wall of text there. Oh well, that's why I made a new thread! :D



Any way, about your project.
That's interesting, you're doing it a bit differently than I've been doing. While I would call most of the stuff you mentioned from item's or character's Update(), in the examples I've seen Draw() functions were separate from Update()s. It's because XNA offers a SpriteBatch class that draws sprites, and has to be SpriteBatch.Open()'ed before all calls and SpriteBatch.Close()'ed after they're drawn - all the draw functions have to be called between those two. Well, you can Open and Close several times, but I think it's much slower that way. I have no idea how C++ draws stuff, so you're on your own there, I have no idea how that way is better or worse.

Post Reply