Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 6 7 [8] 9 10 ... 72

Author Topic: The Roguelike Development Megathread  (Read 245842 times)

Mephisto

  • Bay Watcher
    • View Profile
Re: The Roguelike Development Megathread
« Reply #105 on: January 29, 2009, 03:25:46 pm »

I'm having a wee bit of difficulty. I read somewhere that global stuff is bad, so I have a Game class that holds my map. The trouble is, I need to store my vector of objects in said class, but then how do I loop through them to draw them? Should I just make the class do that itself, or should I perhaps have a function that returns a copy of the vector?

This is all assuming C++. If you're using a different language, ignore.

Code: [Select]
// change the contents of an object in
// a function by passing a pointer
#include <iostream>
using namespace std;

class student
{
      public:
             int semesterhours;
             float gpa;
};

void somefn(student* ps)
{
     ps->semesterhours = 10;
     ps->gpa = 3.0;
     cout << "The value of ps->gpa = "
          << ps->gpa << "\n";
}

int main()
{
    student s;
    s.gpa = 0.0;
   
    // display the value of s.gpa before calling somefn()
    cout << "The value of s.gpa = " << s.gpa << "\n";
   
    // pass the address of the existing object
    cout << "Calling somefn(student*)\n";
    somefn(&s);
    cout << "Returned from somefn(student*)\n";
   
    // the value of s.gpa is now 3.0
    cout << "The value of s.gpa = " << s.gpa << "\n";
   
    cin.get();
    return 0;
}
This is one of the examples from a book I just read. You could set up your classes in an include file (not shown here) and pass a pointer to an object of that class to the function, where it modifies the object through the pointer. You don't have to mess with making a copy and you don't have to mess with copying that copy back to the original. The function doesn't even have to return anything.
Logged

Sowelu

  • Bay Watcher
  • I am offishially a penguin.
    • View Profile
Re: The Roguelike Development Megathread
« Reply #106 on: January 29, 2009, 03:34:06 pm »

I'm having a wee bit of difficulty. I read somewhere that global stuff is bad, so I have a Game class that holds my map. The trouble is, I need to store my vector of objects in said class, but then how do I loop through them to draw them? Should I just make the class do that itself, or should I perhaps have a function that returns a copy of the vector?

Global variables aren't -entirely- evil, but if you're comfortable with C++ classes, doing it with classes and stuff is better.

One thing to keep in mind is how you want to handle multiple levels.  Do you want to keep track of stuff between levels?  Can you go back to an earlier one?  If the answer is "yes", then you might want to have a "Map" class, instead of just a "Game" class.  That way you can have multiple "Map" instances floating around, keeping the data from earlier levels when you leave.

You should consider putting a function on your "Map" or "Game" that draws the whole map, and then draws all the creatures.  I mean, if your map holds lists of creatures and items, it's quite sensible that it should draw them too!  (or at least call them to draw themselves, perhaps)  Taking a copy of the vector out and then using it somewhere else just seems a little roundabout to me.
« Last Edit: January 29, 2009, 03:39:49 pm by Sowelu »
Logged
Some things were made for one thing, for me / that one thing is the sea~
His servers are going to be powered by goat blood and moonlight.
Oh, a biomass/24 hour solar facility. How green!

Rhodan

  • Bay Watcher
    • View Profile
Re: The Roguelike Development Megathread
« Reply #107 on: January 29, 2009, 03:36:59 pm »

I remember picking a roguelike to be my final project in programming at high school. Twice.
It never really worked out, even though I got good marks on both.

The first one had a little hand-crafted village with some clunky NPC-interaction.  Combat was just melee+luck, levelling was mostly based on getting new equipment from corpses and eating said corpses.  It had a little sign saying "unfinished" behind the entrance to the goblin caves.  The framerate was horrid for some reason. (I used Visual Basic.net)

The second one had actual level generation featuring a random starter village with 100 random levels all the way down.  It had a neat event queue system so a really quick rat could hit you twice, or a critical miss would leave your opponent more time to run away.
This one never got an actual inventory system, but it had a lot of random treasure and gems to collect.  I spent a lot of time on a message system that listed objects in a natural language format.

I guess the sad part is that the first attempt got better grades just because it was shinier, while the 2nd attempt had much better and cleaner code.

I might consider a new attempt once I finally get libtcod working.  I'm thinking a Stranded roguelike would be very fun.
Logged

Fenrir

  • Bay Watcher
  • The Monstrous Wolf
    • View Profile
Re: The Roguelike Development Megathread
« Reply #108 on: January 29, 2009, 03:44:41 pm »

You should consider putting a function on your "Map" or "Game" that draws the whole map, and then draws all the creatures.  I mean, if your map holds lists of creatures and items, it's quite sensible that it should draw them too!  (or at least call them to draw themselves, perhaps)  Taking a copy of the vector out and then using it somewhere else just seems a little roundabout to me.
All right, thanks!

I'm only having one level for right now, since the Einherjar don't really go anywhere. To be perfectly honest, I don't really know where I'm going with this. Einherjar (the name of my game in this context; also Odin's chosen warriors) is an idea I had a long time ago. There was much discussion and little action. Initially I had planned that there be quests, but, as Armok said, that's not what the Einherjar do. My vague plan for right now is to make combat and NPC interaction as deep an entertaining as po... er... nevermind. Every time I start blathering about what I plan to do, I stop actually doing. I'll just keep doing whatever and see what comes of it.
Logged

Granite26

  • Bay Watcher
    • View Profile
Re: The Roguelike Development Megathread
« Reply #109 on: January 29, 2009, 03:51:42 pm »

I don't even use switch statements, or even for loops...

Switch + Enums = Ultra Readable Code

As far as globals go, remeber that you can cheat and use a namespace to hide your 'globals' from the outside world.  It's a good first step to completely object oriented.

A second option is static members of classes.  I use this for organizing all my constants (GameConstants.Whatever, ImageConstants.Somethingelse)

For the map:  Couldn't you code a 'get display grid' function that calculated and returned the display character for every square in a simple array(x)(y) for the graphics engine?  Especially if it just returned the characters it needed to draw.  Bonus points for an additional 'get changes' vector that included the tiles that had changed to minimize congestion

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: The Roguelike Development Megathread
« Reply #110 on: January 29, 2009, 03:56:38 pm »

This might be too big a question, more of a theoretical inquiry...

I hear in every class and tutorial and advisement that it's "better" and "safer" to differentiate data and parts of objects or classes and such into Public and Private functions.

What I wonder is, why?  A program can only do what it's told to do.  It's not like this anything to do with marauding hordes assaulting your data, it's just a matter of allowing or disallowing certain parts of program being accessed by other parts of the program.  But I can't think of any reason why I wouldn't want every part of program accessible from anywhere.  What is the good reason not to make every part of a class/object Public?
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.

Granite26

  • Bay Watcher
    • View Profile
Re: The Roguelike Development Megathread
« Reply #111 on: January 29, 2009, 04:04:31 pm »

Because when you've got 30000 lines of code, 300 classes, and 30 programers, you don't always know what a class is supposed to do.  Making most of the code/functions/variables private means that you don't have to worry about people (or you 3 weeks from now) using it incorrectly.

For example, you should have a generic character  He should be able to run, skip, jump, get experience and whatnot.  For the XP system, all you need to expose is 'getxp'.  How that xp is stored, when the character levels up, what happens when he levels up, what skills he gets shouldn't matter.  All you need to do is feed him XP.  If you expose the 'gain skill' function, there's the possibility that someone could call it at the wrong time.  Worse, if you expose the actual XP value, there's the possibility that you could add XP that way, skip the GetXP code that knows how to level up the character appropriately.  GetXP means that it's guaranteed that the same block of code gets called every time you getxp.

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: The Roguelike Development Megathread
« Reply #112 on: January 29, 2009, 04:10:05 pm »

I'm still not seeing how that specifically necessitates controlling access to data, but I think I get the point.  The problem is accidentally calling a function piece of data you're not supposed to with identically named tags or something?  I just keep coming back to the fact that the program will never interact with anything you don't actually tell it to in the first place.

Anyway, that's all irrelevant to my work.  Now to find a C++ dictionary...
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.

Mondark

  • Bay Watcher
    • View Profile
Re: The Roguelike Development Megathread
« Reply #113 on: January 29, 2009, 04:12:57 pm »

This might be too big a question, more of a theoretical inquiry...

I hear in every class and tutorial and advisement that it's "better" and "safer" to differentiate data and parts of objects or classes and such into Public and Private functions.

What I wonder is, why?  A program can only do what it's told to do.  It's not like this anything to do with marauding hordes assaulting your data, it's just a matter of allowing or disallowing certain parts of program being accessed by other parts of the program.  But I can't think of any reason why I wouldn't want every part of program accessible from anywhere.  What is the good reason not to make every part of a class/object Public?

Not knowing C++, etc. only Java, I shall use Java to provide a (very) contrived example.

Say you've got a program in multiple classes, each of which is in its own file. 
You distribute this program to some malevolent, too-smart-for-his-own-good, pimply teenager, like myself. 
  This hypothetical person, using whatever means, figures out what methods are being used, then writes and compiles a replacement for one of the class files.
 The replacement gets loaded like a regular class, and using all the public methods wreaks general havoc in the system, or whatever.
  If the methods and variables are private, however, he'd need to replace a class file with important functionality, which would just mess everything up, instead of providing him with a usable vulnerability.

At least, that's how I understand it, anyone who knows better feel free to correct me.

Now that I think about it, someone making a plugin is a much better example.

Eh, Granite beat me to it, and a better explanation too boot.  Oh well.
Logged
Fefeshnelmargalip

Granite26

  • Bay Watcher
    • View Profile
Re: The Roguelike Development Megathread
« Reply #114 on: January 29, 2009, 04:15:15 pm »

Good program design is about reducing the chance that you'll do something stupid while increasing the ability of the compiler to figure out when you have.

Data encapsulation does both.

It's something I never really got until the first time I looked at a 3 month old program and had to figure out how to make a change to it

Sowelu

  • Bay Watcher
  • I am offishially a penguin.
    • View Profile
Re: The Roguelike Development Megathread
« Reply #115 on: January 29, 2009, 04:17:21 pm »

Public and Private:  Let's say that your creatures have a "hp" field.  You always want to check to see if they die when they lose hit points.  So you want 'hp' private so you can't access it without going through those checks by accident.
Logged
Some things were made for one thing, for me / that one thing is the sea~
His servers are going to be powered by goat blood and moonlight.
Oh, a biomass/24 hour solar facility. How green!

Fenrir

  • Bay Watcher
  • The Monstrous Wolf
    • View Profile
Re: The Roguelike Development Megathread
« Reply #116 on: January 29, 2009, 04:32:33 pm »

Perhaps data encapsulation doesn't matter in the project you're doing now, but you should be learning to do it for the bigger projects down the road that do need it. That's the way I see it, at least.
Logged

Andir

  • Bay Watcher
    • View Profile
Re: The Roguelike Development Megathread
« Reply #117 on: January 29, 2009, 04:41:40 pm »

For the map:  Couldn't you code a 'get display grid' function that calculated and returned the display character for every square in a simple array(x)(y) for the graphics engine?  Especially if it just returned the characters it needed to draw.  Bonus points for an additional 'get changes' vector that included the tiles that had changed to minimize congestion
Double bonus points for making that function a class and calling it a camera or viewport, sending it your world array reference on construction, making it variable (dimension, position) then being able to move it along with your character... or is that asking too much at this stage? :p

I'm all about logical layers in a program design.  If you can abstract an item out as a functional view item, it makes touching it less confusing.  viewport.setCamera(x, y, z);  viewport.setWidth(50), etc ... graphicsEngine.render(viewport.getView())
Logged
"Having faith" that the bridge will not fall, implies that the bridge itself isn't that trustworthy. It's not that different from "I pray that the bridge will hold my weight."

penguinofhonor

  • Bay Watcher
  • Minister of Love
    • View Profile
Re: The Roguelike Development Megathread
« Reply #118 on: January 29, 2009, 05:00:13 pm »

Out of curiosity, what language are you using, Penguin?

Good question, man. Any suggestions?
Logged

Fenrir

  • Bay Watcher
  • The Monstrous Wolf
    • View Profile
Re: The Roguelike Development Megathread
« Reply #119 on: January 29, 2009, 05:20:31 pm »

Double bonus points for making that function a class and calling it a camera or viewport, sending it your world array reference on construction, making it variable (dimension, position) then being able to move it along with your character... or is that asking too much at this stage? :p
I'll try...

EDIT:
What about creatures and objects? Should I do something like this:
Code: [Select]
if(viewport.isInRange(object.getX(),object.getY()))
{
   //draw the object
}
...or should I pass the vector of objects to the viewport and have the viewport determine what should be displayed?
« Last Edit: January 29, 2009, 05:27:18 pm by Fenrir »
Logged
Pages: 1 ... 6 7 [8] 9 10 ... 72