Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
BizarroAzrael
Apr 6, 2006

"That must weigh heavily on your soul. Let me purge it for you."
Still finding my way around UDK and the UnrealScript source code, was wondering if there existed something to look at all the UnrealScript files and present them in a tree structure to show what each file extends from? I think something like that might be useful for me finding specific bits of code I need.

Also, is there an extension for Visual Studio or some text editor that will add predictive text for UnrealScript? I've been working a lot in Visual C# and have gotten quite used to it.

Adbot
ADBOT LOVES YOU

wasabimilkshake
Aug 21, 2007

North Carolina votes yes.
You want nFringe:
http://wiki.pixelminegames.com/index.php?title=Tools:nFringe

It's a Visual Studio plugin that adds IntelliSense support for UnrealScript, among other very helpful features. The way it's set up, the root of your Visual Studio project is UDK/Development/Src/, so all the stock classes are part of your project and can be effortlessly browsed with right-click -> "Go to Definition" or what have you. There's some sort of licensing deal that you're subject to if you make money, but it's free for non-commercial use.

You could also use UnCodeX, which allows you to browse the class tree pretty easily, but it's a standalone application that doesn't have any editing capability.

haveblue
Aug 15, 2005



Toilet Rascal

Luigi Thirty posted:

I thought of this but dismissed it as the cheap way out.

Just because it involves less typing and math doesn't mean it's the cheap way. The engine's built-in transformation system is probably faster than doing it yourself in the high-level language, and involving the full GameObject hierarchy means you can now fire projectiles from any relative location you can express with that system, rather than hard-coding angles and offsets. Plus, you open the door for adjusting the position in the future without having to touch the code that instantiates and configures the projectile, which would come in handy if you have someone else working with you, especially an artist who can't necessarily code.

Not that any of that necessarily applies to you or this particular project, but it's good to get into the habit of thinking about implications.

BizarroAzrael
Apr 6, 2006

"That must weigh heavily on your soul. Let me purge it for you."

wasabimilkshake posted:

You want nFringe:
http://wiki.pixelminegames.com/index.php?title=Tools:nFringe

It's a Visual Studio plugin that adds IntelliSense support for UnrealScript, among other very helpful features. The way it's set up, the root of your Visual Studio project is UDK/Development/Src/, so all the stock classes are part of your project and can be effortlessly browsed with right-click -> "Go to Definition" or what have you. There's some sort of licensing deal that you're subject to if you make money, but it's free for non-commercial use.

Must have done something wrong, I've writen a few .ucs in it already, and never saw any IntelliSense prompts. Didn't realize Goto Definition worked across files.

PDP-1
Oct 12, 2004

It's a beautiful day in the neighborhood.
Anyone know of a (free) graphics program that will let you draw directly on the alpha layer of an image?

I'd like to take an image and apply a smooth alpha gradient to it so it fades from solid in the center to transparent on the edges, but both Gimp and Paint.net seem to derive the alpha channel from the RGB layers instead of letting you manipulate it directly.






VVV That worked! Thanks!

PDP-1 fucked around with this message at 22:12 on Jun 17, 2010

The1ManMoshPit
Apr 17, 2005

In GIMP, right-click on the layer, select "add layer mask," it allows you to edit the alpha channel as a greyscale image. Right-click again and select "apply layer mask" when you're done editing.

Luigi Thirty
Apr 30, 2006

Emergency confection port.

I'm playing with the network stuff in Unity. When the server initializes, it creates a pyramid of cubes. When I connect to the server with another instance of my game, it creates a tank for the player, gets the state of the cubes and puts them in the level.

On the client, if I shoot the cube pyramid with my tank's cannon, the cubes move for a few frames then freeze in place and start freaking out moving back and forth and very slowly going back to their original position. On the server instance of the game, the pyramid has fallen over. If I keep shooting the still-standing pyramid on the client, it will fall over and look like the server. :mad:

All objects I'm working with are network-instanced. I've been looking at it for a while and am probably missing something simple.

BizarroAzrael
Apr 6, 2006

"That must weigh heavily on your soul. Let me purge it for you."

wasabimilkshake posted:

You want nFringe:
http://wiki.pixelminegames.com/index.php?title=Tools:nFringe

It's a Visual Studio plugin that adds IntelliSense support for UnrealScript, among other very helpful features. The way it's set up, the root of your Visual Studio project is UDK/Development/Src/, so all the stock classes are part of your project and can be effortlessly browsed with right-click -> "Go to Definition" or what have you. There's some sort of licensing deal that you're subject to if you make money, but it's free for non-commercial use.

You could also use UnCodeX, which allows you to browse the class tree pretty easily, but it's a standalone application that doesn't have any editing capability.

Ok, it seems I need a script from Epic to make intellisense work, but nFringe's wiki doesn't link it and I can't find it googling, can you help?

lynerd
Mar 31, 2004
I am working on learning Xna and C# for fun. My current hurdle is messing with Xml documents to load sprite data. I have an object class that all sprites would derive from. Below I have listed the data that is in each class. I would like to have a "object.xml" that I can load and have it fill in all the data for me. Anyone have any good tutorials or someplace to start? I am currently Googling Xmlserializer and some other terms and getting some okay stuff. I just can't seem to wrap my head around this though.

If I am in way over my head with Xml I wouldn't even mind loading this from a text file or something.

code:

class Object
{
        public string Name = "none";     // Would load from xml file

        public Vector2 Position = Vector2.Zero;     // Would load from xml file

        protected float Velocity = 1.0f;     // Would load from xml file

        protected Direction Direction = Direction.South;     // Would load from xml file
        protected Action Action = Action.Idle;     // Would load from xml file

        public SpriteSheet spriteSheet;     // Would load from xml file
        //I guess this would be a bunch of nodes for each animation in my dictionary
}


class SpriteSheet
{
        Texture2D texture;
        string fileName = "";

        Dictionary<string, SpriteAnimation> Animations = new Dictionary<string, SpriteAnimation>();

        public string Key = "default";     //which animation is currently shown
        public string Old = "default";     //the last animation that was shown

        float timer = 0.0f;
}


class SpriteAnimation
{
        // Miliseconds that each frame should be shown
        public float interval = 0.0f;
        public bool animate = true;

        int width = 0;
        int height = 0;
        int space = 0;

        int x = 0;
        int y = 0;

        public int yOffset = 0;
        public int xOffset = 0;

        int numberFrames = 0;
        int currentFrame = 0;
}

wasabimilkshake
Aug 21, 2007

North Carolina votes yes.

BizarroAzrael posted:

Ok, it seems I need a script from Epic to make intellisense work, but nFringe's wiki doesn't link it and I can't find it googling, can you help?
Sorry, Intellisense worked for me without any setup. It may be that I have Visual Assist X installed, and its enhanced Intellisense features make it smart enough to digest/setup hinting for UnrealScript on its own.

BizarroAzrael posted:

Ok, it seems I need a script from Epic to make intellisense work, but nFringe's wiki doesn't link it and I can't find it googling, can you help?

1. I'm not sure if the code you posted is an actual snippet or just a quick example you typed up, but do you mean for the Object class to represent a visible, moving object in the game world? You probably don't want to call it Object, since most people will assume that a class named Object is the base class from which all others are ultimately derived. I'd go with something like Sprite or Actor or Entity.

2. There are many different conventions for naming member variables, but "Direction Direction;" will always look retarded to me. I wasn't even aware that you could do that in C#. Feel free to use whatever convention suits your style, but make sure it's readable.

I've been working on loading data from XML this very day, but I can't be of much assistance to you since I'm not familiar with the C# way of doing it. If it's any help, though, you can take a look at my code for loading animation data from an XML file (the LoadTexture function at top is unrelated). I'm using TinyXML, which afaik is only available for C++. Also, don't be confused by the fact that I've named my class ContentManager -- I'm just ripping off XNA's naming conventions for my own convenience.

http://pastebin.com/m1mLBeTM
http://pastebin.com/eesxfGjK

In my case, I call ContentManager.LoadAnimations() at startup, and it parses animations.xml, creates an Animation object for each one, and stores it in a map (C#: dictionary) with its name/id as the key. When I want the Animation called geomancer_walk, I just call ContentManager.GetAnimation("geomancer_walk") and it reaches into the map and hands me a pointer to that animation.

wasabimilkshake fucked around with this message at 06:15 on Jun 18, 2010

seregrail7
Nov 3, 2006

lynerd posted:

I am working on learning Xna and C# for fun. My current hurdle is messing with Xml documents to load sprite data. I have an object class that all sprites would derive from. Below I have listed the data that is in each class. I would like to have a "object.xml" that I can load and have it fill in all the data for me. Anyone have any good tutorials or someplace to start? I am currently Googling Xmlserializer and some other terms and getting some okay stuff. I just can't seem to wrap my head around this though.
Shawn Hargreaves' blog is worth reading, he's one of the XNA guys.

Here's one on serialization: http://blogs.msdn.com/b/shawnhar/archive/2008/08/12/everything-you-ever-wanted-to-know-about-intermediateserializer.aspx

As a quick sample, below is the Data class I use for saving my sprites, but since they're saved in a LevelData file I don't write any XML. You can also set what will be saved in the actual class, but I find that messy. If you're going to be saving custom types(Direction, Action) as properties the classes for those will have to be serializable like my Sprite class. Then whatever you want to save in it's own file will have to have some way to write the data.

code:
[Serializable]
public class EditorSpriteData
{
    public String SpriteSource { get; set; }
    public Vector2 Position { get; set; }
    public int Layer { get; set; }
    public float Rotation { get; set; }
    public Color Tint { get; set; }
    public float Scale { get; set; }
    public Boolean Flip { get; set; }

    public EditorSpriteData() { }

    public EditorSpriteData(Sprite _sprite)
    {
        SpriteSource = _sprite.Name;
        Position = _sprite.Position;
        Layer = _sprite.Layer;
        Rotation = _sprite.Rotation;
        Tint = _sprite.Tint;
        Scale = _sprite.Scale.X;
        Flip = _sprite.FlipHorizontal;
    }
}

[Serializable]
public class LevelData
{
    public LevelData() { }

    public List<EditorLayerData> LayerData { get; set; }
    public List<EditorEmitterData> EmitterData { get; set; }
    public List<EditorSpriteData> SpriteData { get; set; }
    public List<EditorAnimationData> AnimationData { get; set; }
    public List<EditorEntityData> EntityData { get; set; }

    public void WriteData(String filename)
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;

        using (XmlWriter writer = XmlWriter.Create(filename + ".level", settings))
        {
            IntermediateSerializer.Serialize(writer, this, null);
        }
    }
}

seregrail7 fucked around with this message at 23:39 on Jun 19, 2010

lynerd
Mar 31, 2004

seregrail7 posted:


Xml information and awesome link...


Thanks for this info and link. It was very useful in fixing my Xml questions.


(As a forward to this question I am wanting to make a Link to the Past type of game)
Now I am having another issue I can't seem to solve. I'm trying to design how my sprites should fit onto a tilemap. I can't figure out how to handle sprites of different sizes. Sprites don't really fit to the tile size in a game anymore. You don't have to have 32 pixel wide sprites to fit onto 32 pixels tiles.

Since sprites can be all kinds of odd shapes and sizes I can't figure out how to place them in the world and switch between them for different animations or handle collision with tiles. A character's action animation may be quite a a lot wider or taller than the idle or walking animation. Even different characters may be completely different sizes. Like if you had your character and a boss character there might be hundreds of pixels of difference.

I was thinking I could store bounding box information on a per frame basis for each sprite. Also, I would have a Point stored for each frame that would be the part I stick in the sprite's tile position in the world.

As an example here is a sprite from Mother 3 showing what I was thinking for storing the bounding boxes on a per frame basis. Then the point I would store to place him on the correct tile would be somewhere around his shoes in the center.

Click here for the full 709x318 image.


I may just be making my sprite class way too generic and need to settle on how big a sprite can be and hardcode it.

seregrail7
Nov 3, 2006
This is my personal opinion, but I think a lot of people making 2D games over complicate things for themselves by looking at past games. In the 90s developers were working with very limited power and memory, so they had to come up with ways to squeeze in a lot of sprites. Personaly the way I deal with it is I just have a set sprite size that's big enough to incorporate space on the left/right for sword swipes and so on, so I don't have to check for differing sprite sizes. Since it's just some empty space in a sprite sheet, and any PC or console running your game will have plenty of memory for a 2D game.

If you really want to go with varying sprite sizes, you could have a base sprite size, then check the size of the current sprite against it and the direction you are facing and offset it. Something like this might work(not sure if the if statement is in the right order):

code:
Vector2 defaultSize = Vector2.One * 24;
Vector2 currentSize = Vector2.One * 32;
...
if(currentSize.X > defaultSize.X)
    if(facingLeft)
        drawPos.X -= (currentSize.X - defaultSize.X);
    else
        drawPos.X += (currentSize.X - defaultSize.X);
For collision, since drawing is somewhat seperate to the actual position of things you should just keep a bounding box that fits over the area of the persons body and just update it's location as you move, the drawing offset will make it all look ok when it's being drawn.

seregrail7 fucked around with this message at 21:59 on Jun 20, 2010

angel opportunity
Sep 7, 2004

Total Eclipse of the Heart
Relating to your answer and to lynerd's question:

I'm really new to programming but am working on a game. I'm good at making sprites and that's what I did first, I have sprite sheets for several characters finished and I'm learning to code as I go. The game will be turn-based with isometric tiles so I won't be worrying about collision. For each character I have like... 40 different frames of animation. The largest frames (73 x 76 pixels) are the sword swipe frames like you said, a regular walking frame is only 35 x 77. Then the frames where the character is laying on the ground are around 73 pixels wide like the sword swipe frames, but probably only 35 pixels tall.

On the sprite sheet should I just put every frame into a 73 x 76 pixel box so that I can set one texture size and call everything up from the position on the sprite sheet? If the texture size of my tiles are smaller than that largest sword swipe frame will it cause trouble down the line, would doing varying sprite sizes be advisable in this situation? I'm developing for the ipad so there is only 256 MB of RAM, but the game will only have a max of like eight sprites on the board at once.

POKEMAN SAM
Jul 8, 2004

systran posted:

On the sprite sheet should I just put every frame into a 73 x 76 pixel box so that I can set one texture size and call everything up from the position on the sprite sheet?

Just do it this way (i.e. the simplest way) until/unless it's a problem. For those of you who want to pack frames in without "wasting" space, I recommend checking this out: http://www.blackpawn.com/texts/lightmaps/default.html

Vinlaen
Feb 19, 2008

Are there any browser game engines (ie. Flash or plug-in based engines like Unity3D, etc) that are primarily 2D and support UDP?

Basically I'd like to create a multiplayer 2D game that can be played in a browser...

ChadyG
Aug 2, 2004
egga what?

Vinlaen posted:

Are there any browser game engines (ie. Flash or plug-in based engines like Unity3D, etc) that are primarily 2D and support UDP?

Basically I'd like to create a multiplayer 2D game that can be played in a browser...

Aves Engine
Does exactly what you want (except without plugins).
http://www.dextrose.com/en/projects/aves-engine

Also video!
http://www.youtube.com/watch?v=Ol3qQ4CEUTo

edit: except for the part where it will only be available to companies with a sizable budget in 2011

ChadyG fucked around with this message at 16:05 on Jun 23, 2010

brian
Sep 11, 2001
I obtained this title through beard tax.

Unity has built in networking, I believe it's built on Raknet but not i'm entirely sure, I believe it uses UDP as it's very capable of multiplayer action games from the tests i've done. I guess it's also worth noting that it was used for Cartoon Network's Fusion Fall MMO although i'm not sure how much of that was using the built-in networking. I believe it's possible to use System.IO.Sockets too if you want to do it manually.

POKEMAN SAM
Jul 8, 2004

Vinlaen posted:

Are there any browser game engines (ie. Flash or plug-in based engines like Unity3D, etc) that are primarily 2D and support UDP?

Basically I'd like to create a multiplayer 2D game that can be played in a browser...

If you don't care about making people install a plugin, go with Unity, IMO.

Vinlaen
Feb 19, 2008

Aves Engine looks nice... too bad it's not out yet and won't be available to indie developers.

Unity3D looks nice. Unless you pay the $1000 (or is it more?) price for the professional version you don't have direct access to sockets but instead but use their built-in networking layer. I'm not sure how well it works though. Also, how well does it handle 2D? It seems like it's built for 3D (hence the "3D" in the title, heh).

POKEMAN SAM
Jul 8, 2004

Vinlaen posted:

Aves Engine looks nice... too bad it's not out yet and won't be available to indie developers.

Unity3D looks nice. Unless you pay the $1000 (or is it more?) price for the professional version you don't have direct access to sockets but instead but use their built-in networking layer. I'm not sure how well it works though. Also, how well does it handle 2D? It seems like it's built for 3D (hence the "3D" in the title, heh).

At the very least you can just write a thin wrapper for 2D that just billboards stuff.

Vinlaen
Feb 19, 2008

Seems like a really big hack though :(

(and would there be things like collision detect, response, physics, etc?)

CodeJanitor
Mar 30, 2005
I still can't think of anything to say.

Vinlaen posted:

Seems like a really big hack though :(

(and would there be things like collision detect, response, physics, etc?)

Check out Alec Holowka's videos - http://www.youtube.com/user/infiniteammoinc. He has a video that explains how to use Unity to make a 2d scroller, in this case a side-scrolling spaceship shooter. He's done quite a few games, like Aquaria, and has a good understanding of the tools. Its a bit slap-dash, but gives a good idea of what you can do with it.

Edit:
On the Unity note, does anyone have information on how to go about loading levels on the fly as a player moves around the world? For example, having a large world made of a grid of heightmaps (like World of Warcraft handles its ADTs) and loading adjacent/visible map entries as the player moves around the world. I haven't seen a direct tutorial or sample code on how to go about this.

CodeJanitor fucked around with this message at 19:04 on Jun 23, 2010

Evil Trout
Nov 16, 2004

The evilest trout of them all
Hey guys, a couple of days ago I released a small flash game I wrote in ActionScript 3 called Information Overload. The basic premise is a shooter where you shoot news from live sites on the net.



The game hasn't been extremely popular which probably has a lot to do with how little time I spent tuning it, so I figured I'd be nice and release the source code. If anyone here is looking to get into Actionscript coding it might help you out.

If not there's some decent pixel art in there you can use as placeholders in your 2D game projects :)

Play The Game

View The Source, enjoy!

monsterland
Nov 11, 2003

EDIT: oops, wrong thread

monsterland fucked around with this message at 19:05 on Jun 25, 2010

MagneticWombats
Aug 19, 2004
JUMP!
I'm attempting a clone of Pong (on Windows 7 if it matters) with libtcod and C++. I want to be able to have both players hold down their respective up/down keys at the same time and have my game be able to move both.

Presently, with the sample code found in the tutorial and the Mandlebrot program in the libtcod cookbook, really, only one player can play at a time. If both players hold down keys, only one key is registered and so that player can effectively "block" the other from moving. Here's code showing what I have so far: http://codepad.org/zHx6fcJN .

You'll notice in the switch statement some commented out code- that was a failed work around. Perhaps the problem is that a keyboard (Windows at least) can only register so many keys at the same time? I even tried having player 2 use TCODK_UP and _DOWN (with key.vk of course!) but I ran into essentially the same problem.

EDIT: http://codepad.org/ndHxAJ6r works but as you can see, Home and End are not very awesome controls!

MagneticWombats fucked around with this message at 04:39 on Jun 29, 2010

Vinlaen
Feb 19, 2008

What options are available for creating browser-based games?

It seems like Flash is by far the most popular followed by perhaps Unity3D. I know that Silverlight also exists but I'm not too interested in that at the moment.

Also, does anybody have any comments/opinions on browser-based games versus standalone games? I'd like to create some simple games and it seems like requiring the user to download and install software is a considerably large road block.

For example, I recently tried Transformice (and love it), but I wouldn't have tried it if I was required to download it first...

Bastard
Jul 13, 2001

We are each responsible for our own destiny.

Vinlaen posted:

What options are available for creating browser-based games?

It seems like Flash is by far the most popular followed by perhaps Unity3D. I know that Silverlight also exists but I'm not too interested in that at the moment.

Flash, Unity3D, Silverlight, Torque2d (whenever the next major version is released, don't hold your breath), Processing, HTML5's canvas + javascript.

haveblue
Aug 15, 2005



Toilet Rascal
HTML5/JS is the "best" solution in that it seems set to become the preferred method in the future, but there are no slick, friendly dev environments for it yet the way there are for all the others. If you just want to whip up a quick game, as a learning experience, use Flash, Unity, or Silverlight.

Valtis
Sep 21, 2009
I have been bored lately and I decided to start coding roguelike game with C++, and at the moment I'm struggling with the inventory system. I thought that it would be easiest to have one base class for the items that has all the generic attributes such as weight and item name, and then have derived classes with item spesific attributes and methods. Items would be stored into vector as pointers to base class (vector<BaseClass *> items) so that I can have single container for all items. This however causes me some headache when I actually have to do something with the items, as the base class won't have all the methods etc. so I cannot use virtual methods. Currently I have to cast the pointer back to whatever derived item class I need to use which is rather undesirable for the following reason:

For example, when I want to show item attributes in the inventory, I need to cast all the weapon items back to weapons so I can get damage etc. values from the class, all the armor items back to armors to get defence values and so on for each item type, which makes the item list code huge stack of if - else if - commands:
code:
if (itemPointer->itemType == weapon)
	(Weapon *)(itemPointer)->Foo();
else if (itemPointer->itemType == armor)
	(Armor *)(itemPointer)->Bar(); 
...
So, is there any better way to create inventory as this approach has proven to be pretty terrible to me.

FrantzX
Jan 28, 2007
Have a 'DisplayForInventory' abstract function in the item base class. That way you can avoid the casts.

Valtis
Sep 21, 2009

FrantzX posted:

Have a 'DisplayForInventory' abstract function in the item base class. That way you can avoid the casts.

Hmh, I wonder why I didn't think of that :downs:. Sometimes I'm blind to the most obvious solutions.

Hubis
May 18, 2003

Boy, I wish we had one of those doomsday machines...

Valtis posted:

I have been bored lately and I decided to start coding roguelike game with C++, and at the moment I'm struggling with the inventory system. I thought that it would be easiest to have one base class for the items that has all the generic attributes such as weight and item name, and then have derived classes with item spesific attributes and methods. Items would be stored into vector as pointers to base class (vector<BaseClass *> items) so that I can have single container for all items. This however causes me some headache when I actually have to do something with the items, as the base class won't have all the methods etc. so I cannot use virtual methods. Currently I have to cast the pointer back to whatever derived item class I need to use which is rather undesirable for the following reason:

For example, when I want to show item attributes in the inventory, I need to cast all the weapon items back to weapons so I can get damage etc. values from the class, all the armor items back to armors to get defence values and so on for each item type, which makes the item list code huge stack of if - else if - commands:
code:
if (itemPointer->itemType == weapon)
	(Weapon *)(itemPointer)->Foo();
else if (itemPointer->itemType == armor)
	(Armor *)(itemPointer)->Bar(); 
...
So, is there any better way to create inventory as this approach has proven to be pretty terrible to me.

What's wrong with RTTI?

Valtis
Sep 21, 2009

Hubis posted:

What's wrong with RTTI?

Nothing probably, I just hadn't heard of it before; I don't have any official training or anything, so my knowledge is mostly limited to 10 year old books and web tutorials.

POKEMAN SAM
Jul 8, 2004

Hubis posted:

What's wrong with RTTI?

There are better solutions most of the time that don't involve bloating the hell out of your executable ;)

Hubis
May 18, 2003

Boy, I wish we had one of those doomsday machines...

Ugg boots posted:

There are better solutions most of the time that don't involve bloating the hell out of your executable ;)

Well yeah, maybe not actual rtti, but including a "type" flag and casting like he's doing at the least. It may seem "hackish" in C++, but that's pretty run-of-the-mill for higher level languages.

Having the most generic methods you can get away with ("virtual std::string Item::GetInventoryDesc()" for example) reduces how often you have to do that sort of thing and is probably a good idea, but at some point when you try to equip the item in slot 5 to your right hand, you're going to have to decide if it's a weapon or not, and if so what it's stats are. Of course then again, being able to wield a banana in your mainhand and attacking with it is very Nethack-ish, so...

Null Pointer
May 20, 2004

Oh no!
The best solution would be the visitor pattern. That way you avoid casting, and you can still keep your inventory display logic separate from your inventory item.

Paniolo
Oct 9, 2007

Heads will roll.

Valtis posted:

I have been bored lately and I decided to start coding roguelike game with C++, and at the moment I'm struggling with the inventory system. I thought that it would be easiest to have one base class for the items that has all the generic attributes such as weight and item name, and then have derived classes with item spesific attributes and methods. Items would be stored into vector as pointers to base class (vector<BaseClass *> items) so that I can have single container for all items. This however causes me some headache when I actually have to do something with the items, as the base class won't have all the methods etc. so I cannot use virtual methods. Currently I have to cast the pointer back to whatever derived item class I need to use which is rather undesirable for the following reason:

For example, when I want to show item attributes in the inventory, I need to cast all the weapon items back to weapons so I can get damage etc. values from the class, all the armor items back to armors to get defence values and so on for each item type, which makes the item list code huge stack of if - else if - commands:
code:
if (itemPointer->itemType == weapon)
	(Weapon *)(itemPointer)->Foo();
else if (itemPointer->itemType == armor)
	(Armor *)(itemPointer)->Bar(); 
...
So, is there any better way to create inventory as this approach has proven to be pretty terrible to me.

Seems like you're misunderstanding how to use polymorphism here. If you have some sort of operation which needs to know the subtype of a class but only has a pointer to the base class, 90% of the time the operation is being performed in the wrong place. The sort of thing you're doing there is a really strong code smell.

For example, you could do something like this for a fairly versatile system:

code:
class ToolTip {
public:
   virtual std::string GetString() = 0;
};

class WeaponToolTip {
public:
   WeaponToolTip(const Weapon* item);
   std::string GetString();
};

class ArmorToolTip {
public:
   ArmorToolTip(const Armor* item);
   std::string GetString();
};

class ItemBase {
public:
   virtual const ToolTip& GetToolTip() = 0;
};

class ItemWeapon {
public:
   ItemWeapon()
   : mToolTip(this)
   {
   }

   const ToolTip& GetToolTip() 
   {
      return mToolTip;
   }

private:
   WeaponToolTip mToolTip;
};

class ItemArmor {
public:
   ItemArmor()
   : mToolTip(this)
   {
   }

   const ToolTip& GetToolTip()
   {
      return mToolTip;
   }

private:
   ArmorToolTip mToolTip;
};

Gyshall
Feb 24, 2009

Had a couple of drinks.
Saw a couple of things.
I'm interested in making my own side scroller game just as a hobby. I don't know any programming languages, but I have dabbled in Python before and I can do web design/CSS, if that helps. I used to mess around with RPG Maker and could get around that fairly easily, so I'm wondering if there is a free or sub $150 game engine designed around making side scrolling beat em' ups like Comix Zone or Streets of Rage et al. Any suggestions on where to start?

Adbot
ADBOT LOVES YOU

Hubis
May 18, 2003

Boy, I wish we had one of those doomsday machines...

Gyshall posted:

I'm interested in making my own side scroller game just as a hobby. I don't know any programming languages, but I have dabbled in Python before and I can do web design/CSS, if that helps. I used to mess around with RPG Maker and could get around that fairly easily, so I'm wondering if there is a free or sub $150 game engine designed around making side scrolling beat em' ups like Comix Zone or Streets of Rage et al. Any suggestions on where to start?

For what it's worth, a friend of mine works on a 2D prototype engine called Angel. Lua/Python integration, cross-platform, built-in modules for a bunch of basic stuff like AI and pathing.

Hubis fucked around with this message at 21:58 on Jul 6, 2010

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply