Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Adding a "Tag" to a sprite: Possible  (Read 5967 times)

0 Members and 1 Guest are viewing this topic.

eyeliner

  • Newbie
  • *
  • Posts: 19
    • View Profile
Adding a "Tag" to a sprite: Possible
« on: August 12, 2014, 05:22:48 pm »
First, I'll explain what I want to do:

I'm currently using lists to store my sprites, so that I use a foreach loop and draw all the items in a List<Sprite>.
It works great. For drawing, of course.

Thing is, if I want to do anything more to them, say like test if the mouse is over one specific sprite.
It would be great if there's a way to add a tag (or something, for that matter), that we could use to differenciate the sprites, using a list would be even more great.

In my case, I'm specifying the index of a sprite on the list to shutdown my app. Ok, I can live with it.
But when the app grows (and I intend on doing something very much mouse based), it would be great if I could do something like:

foreach (Sprite s in sprites)
    if (s.GetGlobalBounds().Contains(mouseX, mouseY)&&s.Tag =="whatever")
        {
                //Do something
        }
 
I'm really not sure about the difficulty in implementing such a thing and if a thing exists, I apologise. If it is something that can be added by anyone less enlightned (like myself), I surely wouldn't mind doing it.

Can anyone tell me? Thanks.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Adding a "Tag" to a sprite: Possible
« Reply #1 on: August 12, 2014, 06:01:54 pm »
That's a really limited point of view, and such things will never make it into SFML anyway. SFML sprites should not, and will never, help you implement your high-level collision logic.

Instead, you should think about how you could implement such a system on top of SFML sprites. Not in them.

For example, you could create a class derived from Sprite that checks its collisions itself and triggers an event when it collides. Then you can attach a collision handler when you create the sprite.
Laurent Gomila - SFML developer

eyeliner

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Adding a "Tag" to a sprite: Possible
« Reply #2 on: August 12, 2014, 09:58:16 pm »
Understood. I don't think I'm quite there yet to pull off such a thing, but I got the point you are trying to get across.

Thanks Laurent.
EDIT:
Thanks muchly for the point in the right direction! Made it work by making a class that derives from sprite. I added a Tag.

Now to make a collision checker. :)
« Last Edit: August 12, 2014, 10:32:26 pm by eyeliner »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Adding a "Tag" to a sprite: Possible
« Reply #3 on: August 12, 2014, 10:46:59 pm »
I would even recommend to build a high-level logic class around Sprite, not derived from it. That is, one that contains the sprite as a member and encapsulates it. The sprite is just a visual representation of a game entity and not the entity itself.

This may sound like more work in the beginning, but the separation of graphics and game logic pays off as soon as the project grows. There have been a lot of design discussions about this point, you could search for older threads if you're interested.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Adding a "Tag" to a sprite: Possible
« Reply #4 on: August 12, 2014, 10:51:40 pm »
eyeliner, try this:
class TaggedSprite : public sf::Sprite
{
public:
        std::string tag;
};
Then use TaggedSprite instead of sf::Sprite. You can then access an internal string called tag for any use that you like.

Here's an complete (but simple) example:
(click to show/hide)

I read Nexus' reply just as I was about to post this and realised that I think he's right. I'm still going to post this example as it may help you see how easy/useful inheritance can be :)

EDIT: Just saw your own modified post that stated that you've figured it out already. Go you! :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eyeliner

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Adding a "Tag" to a sprite: Possible
« Reply #5 on: August 12, 2014, 10:55:32 pm »
I sure am! I just need a bit of time to deal with and search for with the particulars. Adding a tag ended up being way easier than I first thought.
Of course, I need to create a sprite in a bit different way, but it's okay (for 3 lines of code, at least).

My main hardships are the names of what to look for. :p

Thanks, Hapax, I'll have to translate that to C#. :) And yeah, I figured how to do it, but I can't create a sprite like:
Code: [Select]
nSprite spr = new nSprite(new Texture("image.png");

I have to resort to this:
Code: [Select]
nSprite.Texture = new Texture("image.png");
nSprite.Tag = "myTag";

But it's not too bad.
« Last Edit: August 12, 2014, 10:59:18 pm by eyeliner »

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Adding a "Tag" to a sprite: Possible
« Reply #6 on: August 12, 2014, 11:39:11 pm »
I'm really sorry; I didn't even notice this was in the DotNet forum  :-[

I found the same problem. You can see in my code that I also set the texture after creation of the "tagged sprite".

You should probably, as Nexus suggested earlier, create a class that has an sf::Sprite as a member and include the tag inside your new class. That way, you can add more things to it that aren't directly related the image. The initialisation should work as normal then too.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eyeliner

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Adding a "Tag" to a sprite: Possible
« Reply #7 on: August 13, 2014, 12:21:40 pm »
That's OK. When trying to help, sometimes overlooks happen.

Now, regarding Nexus' advice (and yours), I'm trying to find out how to do such a thing. :P
And having a hard time to find good examples. But I'm stubborn. :)

Ztormi

  • Jr. Member
  • **
  • Posts: 71
  • Web developer by day. Game developer by night.
    • View Profile
Re: Adding a "Tag" to a sprite: Possible
« Reply #8 on: August 13, 2014, 02:04:46 pm »
He is referring to splitting your class into logical components as in Component pattern.

The simplest practical example would be:

 
public class MySprite
    {
        // this is the sfml sprite
        public Sprite Sprite { get; set; }

        public string Tag {get; set;}

        public MySprite()
        {
        }
        public MySprite(Texture t)
        {
            Sprite = new Sprite(t);
        }
    }

 
MySprite spr = new MySprite(new Texture("..."));
« Last Edit: August 13, 2014, 02:13:33 pm by Ztormi »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Adding a "Tag" to a sprite: Possible
« Reply #9 on: August 13, 2014, 03:28:39 pm »
If you really don't want to use the component pattern and really just want a tag added to a sprite you could exploit object inheritance + extension methods. However, as Nexus said, you should really just prefer a high level composition class around SFML objects.
(click to show/hide)


This would also allow your original code to work like the following.

(click to show/hide)
« Last Edit: August 13, 2014, 03:36:53 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

eyeliner

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Adding a "Tag" to a sprite: Possible
« Reply #10 on: August 13, 2014, 04:48:33 pm »
This is all levels of amazing! Thanks! After reading your code (Ztormi, and zsbzsb), I think I now understand. I was actually hacking away with some positive results.

I wouldn't go where you guys are so fast (there's the difference between those who know and those who would like to know), but you guys helped me immensely for my future. This will prove quite handy if I ever get such a need in other classes.

Thanks, all.

 

anything