.com.unity Forums

.com.unity Forums (http://forum.shrapnelgames.com/index.php)
-   Space Empires: IV & V (http://forum.shrapnelgames.com/forumdisplay.php?f=20)
-   -   OT: Stupid c++ (Roguelike development) (http://forum.shrapnelgames.com/showthread.php?t=29767)

narf poit chez BOOM August 18th, 2006 06:11 PM

Re: OT: Stupid c++
 
Apparently, the compiler tries to treat every .cpp file as a seperate program, so multiple .cpp files do not work. I presume it works with other compilers.

PvK August 18th, 2006 06:31 PM

Re: OT: Stupid c++
 
C++ is trying to help you to help yourself to write nice code. Why do you want a class member function to have side effects on a global vector defined outside the class?

A nicer way to do it would be to have the constructor take a parameter which tells the object about the global vector (pass it a pointer or reference), store that pointer/reference and use that.

PvK

PvK August 18th, 2006 06:33 PM

Re: OT: Stupid c++
 
No, every compiler has limited file scope, to keep globals slightly under control. You can get to objects in other files by declaring them "extern", but relying on globalness tends to get ugly.

narf poit chez BOOM August 18th, 2006 06:41 PM

Re: OT: Stupid c++
 
AngleWyrm, what's that code for?
No, just a slight precedence error in 'characterToGraveyard'. I fixed it, but thanks anyway.

@Pvk: Wouldn't that take a lot of memory?

Ah, thanks. The advice makes more sense now.

Update, call it: Simple Roguelike v0.1.4.5

Not a single pointy in the code.

What kind of file is 'main.o'? It's rather big - Can I delete it?

PvK August 18th, 2006 08:07 PM

Re: OT: Stupid c++
 
It'll use all of 4 bytes if you make it a class static data member, in which case there is only one per class, not per object:

static (STL::vector<cheese>)* pGlobalCheeseVector;

However I guess I'm harping object-oriented design when you are working in a good-ol' C hack design paradigm, which may actually be less confusing for you at this point. If it works and you understand why, it's good code. http://forum.shrapnelgames.com/images/smilies/wink.gif

main.o is probably an object file. That is, an intermediate file before building the exe. If you delete it, it just means it the compiler will need to recreate it before building the exe, even if you hadn't changed any code in main.cpp before you rebuilt. Generally you can ignore main.o, and you don't need to distribute it to players.

PvK

narf poit chez BOOM August 18th, 2006 08:20 PM

Re: OT: Stupid c++
 
Ah, thanks.

Object-oriented design is what I'm trying to do. Any difficulties come because I'm self-taught (Aside from what random people on the internet have taught me).

Er, I've tried to make a pointer to a vector, but it didn't work.

Death to globals!

PvK August 18th, 2006 08:33 PM

Re: OT: Stupid c++
 
Hmm, a pointer to vector should work - what syntax are you using?

narf poit chez BOOM August 18th, 2006 09:09 PM

Re: OT: Stupid c++
 
I dunno. Whatever syntax Dev c/c++ uses. I found a syntax page, but it doesn't list the type.

I'll try it again, soon as I finish getting rid of the current global.

(Er, why did you type STL::vector? I thought it was std?)

This seems to work: 'static std::vector<Character> * CVector;'

So I guess I just put my &Character list vector into CVector, then access that from inside the class?

PvK August 18th, 2006 10:05 PM

Re: OT: Stupid c++
 
Oh, sorry STL->std (bad human memory).

By "what syntax", I just meant what does your code look like. Yes, the line you wrote is the syntax I was trying to remember and suggest to you. Good job. http://forum.shrapnelgames.com/images/smilies/wink.gif

Yes, so the syntax for that would be something like:
<font class="small">Code:</font><hr /><pre>
// In the class definition file e.g. NarfClass.h
class NarfClass
{
// Constructor:
NarfClass( std::vector&lt;Character&gt; * pCVectorToUse );

// Method that does something with CVector:
void DoStuffWithCVector();

// The static storage of the Character Vector:
static std::vector&lt;Character&gt; * pCVector;
};</pre><hr />

<font class="small">Code:</font><hr /><pre>// In the class definition file e.g. NarfClass.cpp
#include "NarfClass.h"

NarfClass::NarfClass( std::vector&lt;Character&gt; * pCVectorToUse )
{
pCVector = pCVectorToUse;
}

void NarfClass::DoStuffWithCVector()
{
int NumberOfCharactersInGame = pCVector-&gt;size();
// etc...
}</pre><hr />

<font class="small">Code:</font><hr /><pre>// In the class that stores the whole gamestate:
#include "NarfClass.h"

// The actual vector:
static std::vector&lt;Character&gt; CVector;

// The creation of a NarfClass object:
NarfClass NarfsNarf( &amp;CVector );

// Now NarfsNarf is an object that can do stuff with CVector.
NarfsNarf.DoStuffWithCVector();</pre><hr />
I may have a typo or be missing something, but that's about how I'd do it.

PvK

narf poit chez BOOM August 18th, 2006 10:15 PM

Re: OT: Stupid c++
 
Thanks again.

For syntax, I tend to go with the needs of the moment.

So, you mean make a handler class for the Character class?

How do you declare something 'extern'?

PvK August 18th, 2006 10:24 PM

Re: OT: Stupid c++
 
I don't really know what you're doing, so NarfClass was just an example. NarfClass would be any class that wants to be able to use some external object, without requiring the external object to actually be defined as a global with a certain name in the class definition.

Actually, the classic approach to doing what I think you probably really want to do is to make the Character Vector into a singleton class - a class designed to ensure there is only one of something, and provide access to it to other classes.

I don't really know what your class design is like - that is, how you've decided to divide up the things in your game into classes.

extern is a keyword for referring to objects or variables defined in other files, which would be how you can refer to globals created in other files. The syntax is, for example:

extern int X9B;

This lets you use X9B, but in one and only one other file you need to have the actual declaration:

int X9B;

So you can see that can make your code hard to read and use the more you do that, because it means you have to remember where that global is actually defined, etc.

PvK

narf poit chez BOOM August 18th, 2006 10:33 PM

Re: OT: Stupid c++
 
If you want, I can send you the source code. Right now, the character stuff is mostly in one character class, with a vector for the list of characters and some external functions that deal with that vector class.

Yeah, I think I'll stay away from multiple cpp files if it means externs. Thanks.

PvK August 18th, 2006 11:05 PM

Re: OT: Stupid c++
 
Multiple cpp files means externs if you use globals to share info between them.

But for an object-oriented design, you have a class definition file (Foo.h) and a class implementation file (Foo.cpp) for each class, and then you use them in the context of other classes. This way, almost all the code you write solves problems for general cases, and the specific application implementation (e.g. the actual Main function of an application) can then be something more general, simple and high-level.

So Character defines a character object in so general a way, that its .h and .cpp files don't need to refer to the instance of them at all - they refer only to the definition of other related classes, but never to a specific object (or else they'd be usable only with that specific object).

So your list of classes might be something like:

GameState
Level
Character
Item
Potion
Weapon
Armor

Each would have two files, a .h and a .cpp. All would be generic implementations.

Then you'd have a Main.cpp file where the entry point and specific objects are declared. In it you'd have maybe just one GameState object, and just a few calls to its methods, like:

GameState.CreateNewGame();
GameState.Load( SavedGame );
GameState.Play();

GameState itself might have a protected Initialize() method which would be called by both CreateNewGame() and Load(), and GameState would have the only Vectors storing all the objects representing stuff that exists in the game universe.

Etc.

PvK

narf poit chez BOOM August 18th, 2006 11:18 PM

Re: OT: Stupid c++
 
Huh. Thanks.

That is a radical departure in thinking. I'll have to think about it.

Right now, I'm wanting to do some visible content. Too much code maintenance makes Narf wonder if he's making progress.

PvK August 18th, 2006 11:36 PM

Re: OT: Stupid c++
 
Ya. You might want to spend more time having fun making things work, and think about a more O.O. architecture for your next project.

narf poit chez BOOM August 19th, 2006 12:34 AM

Re: OT: Stupid c++
 
Heh. I added armor. Now I'm making it work. Just have to fix the load routine (Again).

Everythings fixed, and armor is now available. Just check the floor in your nearest dungeon.

narf poit chez BOOM August 19th, 2006 12:50 AM

Re: OT: Stupid c++
 
Anyone know how to turn off the generation of debugging information in Dev c++? I think it's padding my program.

narf poit chez BOOM August 19th, 2006 02:23 AM

Re: OT: Stupid c++
 
Updated. A couple of weapons with actual names. Savegames should still work.

On the downside, characters aren't properly re-located after descending stairs. Not a game-breaker.

narf poit chez BOOM August 22nd, 2006 06:24 PM

Re: OT: Stupid c++
 
Update: Simple Roguelike v0.1.5.0

Breaks savegames. A few different item properties. Items are now more of a trade-off.

narf poit chez BOOM September 4th, 2006 08:41 PM

Re: OT: Stupid c++
 
Dev C++ doesn't seem to like one of the include files that came with the DirectX SDK.

It's one of the ones used in:
'(SDK root)\Samples\C++\Direct3D\Tutorials\Tut01_CreateD evice'

I think I have the Directx include and lib folders linked in the right place, but just to check, can somone tell me how to link the Dx SDK into Dev C++?

Thanks.

narf poit chez BOOM September 6th, 2006 05:48 PM

Re: OT: Stupid c++
 
The latest

dogscoff September 7th, 2006 06:59 AM

Re: OT: Stupid c++
 
Just downloaded and had a little play.

Damn Narf, that is so cool! I really envy you the skills to produce something like this. I used to do a little coding (nothing on this level of complexity) and I'm just now remembering that feeling you get from creating something from nothing.

Have you got a name for your roguelike yet? I suggest you call it "Zort".

narf poit chez BOOM September 7th, 2006 12:50 PM

Re: OT: Stupid c++
 
Thanks. Responces like that make my day. http://forum.shrapnelgames.com/images/smilies/happy.gif

"Zort"? Why Zort?

dogscoff September 7th, 2006 01:09 PM

Re: OT: Stupid c++
 
Well, Zort is one of Pinky's many exclamations (along with "Narf", "Poit", "Zounds", "Egads" and others) and Zork was a classic text-based adventure game lovingly referenced in roguelikes such as nethack (Ever wonder why the currency of the Dungeons of Doom is 'zorkmids'?)

Kind of a pun, I guess. Not a very good one though.

(I just love that you can plug "Zort" into the wikipedia and get redirected to PatB!)

narf poit chez BOOM September 7th, 2006 01:23 PM

Re: OT: Stupid c++
 
Hmm, thanks.

Ed Kolis September 7th, 2006 01:58 PM

Re: OT: Stupid c++
 
Hey Narf, does your roguelike have magic yet? I've become rather enamored of the runecraft system in the Nintendo DS RPG/RTS hybrid "Lost Magic"... it's quite original in that you draw runes on the screen to cast spells, and after a certain point in the game you can combine multiple runes to cast more powerful spells. Obviously you can't draw on the screen in a PC roguelike, but I thought the combining runes system would be really cool... ToME (Tales/Troubles of Middle Earth; http://www.t-o-m-e.net) uses a similar system for one of its mage classes, and if you're interested you could use it in your roguelike too! http://forum.shrapnelgames.com/images/smilies/laugh.gif In fact the idea got me so excited I was thinking of starting work on yet another roguelike project that will probably fail - why I never pick up work on the existing ones I've started I don't know, but it might be fun http://forum.shrapnelgames.com/images/smilies/wink.gif
The idea so far is that you could have a variety of runes which your character can learn, and spells are cast not from a list but by typing the rune keys - keys on the numpad, or letter keys, or whatever - I kinda like the numpad because you can have "opposed" runes (fire and water for instance) located opposite each other and some sort of "neutral" Balance or Cosmos rune in the middle. Anyway, so you type the "cast a spell" command (say "m", like in Angband), then you type 2 runes, and after you type the second rune you can target the spell. If you combine fire and fire you might get a big fireball, for instance; air and water for maybe a freeze spell; light and air for a haste spell. The more runes your character learns, the more spells he can cast - a starting character choosing the fire-mage template might only be able to cast a fireball, but once he learns a second rune (say, earth) he could now cast four spells: fireball (fire/fire), stoneskin (earth/earth), magma bolt (fire/earth), and earthquake (earth/fire)! http://forum.shrapnelgames.com/images/smilies/laugh.gif So if you had nine runes (say fire, water, earth, air, cosmos, chaos, light, dark, and balance) you could have 81 spells in your game. That might sound like a lot of spells to come up with, but Lost Magic has something like 400 spells and 18 runes, and to come up with those spells they used patterns - all the fire+X spells are beam attacks, all the water+X spells are rapid-fire attacks, all the dark+X spells are monster traps, etc.
Of course if you don't like this idea, don't use it, but I thought I'd just bring it to your attention! http://forum.shrapnelgames.com/images/smilies/wink.gif

Ed Kolis September 7th, 2006 04:48 PM

Re: OT: Stupid c++
 
Here's another spellcasting idea, borrowed this time from UnAngband: Don't just use "intelligence" as the be-all and end-all of spellcasting stats! Split it up amongst several stats! Let intelligence determine how many spells you can memorize, because you have to be smart to remember them all. Let wisdom determine how powerful your spells are, because only wise men have true magical power. Let dexterity determine how likely your spells are to actually work, because if you do the hand-waving wrong it will really screw up your spell! http://forum.shrapnelgames.com/images/smilies/wink.gif You can even combine this with the runecraft idea: don't let the player use all of his myriad spells at once, but instead give him a limited number of memory slots, dependent on intelligence, in which he can place spells; in order to use a spell which is not in memory, he must have the two runes used to create it available to study from and either "copy" them (casting the spell immediately but perhaps using more game ticks since the character must concentrate both on reading the runes and moving his hands, or perhaps even destroying the inscribed runes which he is carrying) or memorize them (deleting one of his existing spells from memory if there are no more free slots).

Caduceus September 7th, 2006 10:09 PM

Re: OT: Stupid c++
 
Played around with Relentless. Good job early on... I can't tell if I am equipping items I pick up though...

narf poit chez BOOM September 8th, 2006 12:26 AM

Re: OT: Stupid c++
 
For magic, I'm thinking something simple, that will allow the player to build spells.

The readme has been updated with a sentence on equiping.

narf poit chez BOOM September 8th, 2006 03:46 PM

Re: OT: Stupid c++
 
The latest
Bugfixes, updates and name change.

Now that I'm coherent, instead of thematic runes, maybe purpose-based runes. Supposing you have the runes for Fire, Summon, Ray and Monster. You could make an explosion spell (Fire with no control rune), but it would damage your character. You could make a Ray of Fire, Summon Fire (Explosion at a square within your LOS), Summon a Monster, Summon a Monster where a Ray hits something solid, Summon a Fire-based Monster, Summon a Ray (Which would fire a Ray from a square you choose at a target you choose, but wouldn't do anything usefull), or Summon a Ray of Fire (Much more usefull.)

Simple, versitile, quick - What do you think?

Ed Kolis September 8th, 2006 07:33 PM

Re: OT: Stupid c++
 
Cool http://forum.shrapnelgames.com/images/smilies/laugh.gif

Ed Kolis September 11th, 2006 01:00 PM

Re: OT: Stupid c++
 
1 Attachment(s)
Hey Narf, I made this attached pic for a modelling challenge over at the Wings forum, but I thought you might like to use it as an intro screen for your roguelike; besides, I just need a place to upload it to show it off at the Wings forum since I'm not at home to put it on my server http://forum.shrapnelgames.com/images/smilies/wink.gif

narf poit chez BOOM September 11th, 2006 08:56 PM

Re: OT: Stupid c++
 
Thanks, soon as I figure out how to put a picture on the screen.

Ed Kolis September 12th, 2006 09:15 AM

Re: OT: Stupid c++
 
You could always get one of those handy bitmap-to-ASCII programs and make it doubly old-school http://forum.shrapnelgames.com/images/smilies/wink.gif

narf poit chez BOOM September 13th, 2006 12:49 AM

Re: OT: Stupid c++
 
...I didn't even know they existed. I thought that was done by hand.

Ed Kolis September 13th, 2006 09:08 AM

Re: OT: Stupid c++
 
Some ASCII art is done by hand, but the really complex stuff is often done with a program http://forum.shrapnelgames.com/images/smilies/wink.gif

narf poit chez BOOM September 13th, 2006 05:07 PM

Re: OT: Stupid c++
 
Huh, thanks.

Ed Kolis September 13th, 2006 05:24 PM

Re: OT: Stupid c++
 
BTW, this pic will also be appearing in the following comic series as part of a guest comic next week!
http://angband.calamarain.net/

narf poit chez BOOM September 18th, 2006 06:46 PM

Re: OT: Stupid c++
 
Why would the 'new' operator re-assign the same pointers?

Seriously, if you can answer that question, I can probably fix the current problem.

narf poit chez BOOM September 19th, 2006 12:18 PM

Re: OT: Stupid c++
 
And now it isn't even getting past re-sizing the character array.

C++ is stupid.

narf poit chez BOOM September 19th, 2006 12:28 PM

Re: OT: Stupid c++
 
Anybody want to volunteer to look at the code?

Ed Kolis September 19th, 2006 04:30 PM

Re: OT: Stupid c++
 
Sure, though I'm not that great with C++, I might be able to spot something...

PvK September 19th, 2006 05:23 PM

Re: OT: Stupid c++ (Roguelike development)
 
Why would new re-assign what same pointers?

Fyron September 19th, 2006 05:49 PM

Re: OT: Stupid c++
 
And now it isn't even getting past re-sizing the character array.

Why are you using character arrays at all? There is no reason to ever use them over string. Any time you need to pass a char* to a function, just use the .c_str() member on your string (aka: crazy_function(mystring.c_str()).

C++ is stupid.

C++ is neither stupid nor smart. It all depends on how you use it.

Why would the 'new' operator re-assign the same pointers?

I don't understand the question. New doesn't assign any pointers; it creates an object in the dynamic memory space (heap), and returns a pointer to it.

narf poit chez BOOM September 19th, 2006 06:04 PM

Re: OT: Stupid c++
 
By character array, I mean an array of NPC's and PC's. And technically I should have said 'vector'.

C++ needs more comprehensive error checking.

Tomato, tomato.

Anyone who wants to see the source code, PM me, thanks.

Fyron September 19th, 2006 06:39 PM

Re: OT: Stupid c++
 
C++ compilers have more error checking than most other languages (esp. g++ with things like -pedantic flags). And anyways, you can always add more by throwing and catching exceptions.

narf poit chez BOOM September 19th, 2006 07:53 PM

Re: OT: Stupid c++
 
It doesn't tell me why the resize function wasn't working on the character vector! Obviously, it's not fully-featured! http://forum.shrapnelgames.com/images/smilies/laugh.gif

Fyron September 19th, 2006 08:22 PM

Re: OT: Stupid c++
 
Why are you using a resize function anyways? push_back() works well.

narf poit chez BOOM September 19th, 2006 08:25 PM

Re: OT: Stupid c++
 
Because, with resize you don't have to push_back something.

Fyron September 19th, 2006 08:36 PM

Re: OT: Stupid c++
 
Why would you increase the size of a vector without having something to add to it?


All times are GMT -4. The time now is 07:36 PM.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©1999 - 2025, Shrapnel Games, Inc. - All Rights Reserved.