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

Author Topic: check if variable is equal to certain sprite or null  (Read 1735 times)

0 Members and 1 Guest are viewing this topic.

forestlapointe

  • Newbie
  • *
  • Posts: 2
    • View Profile
check if variable is equal to certain sprite or null
« on: September 18, 2019, 01:34:26 pm »
I need to create a public function that runs through an array of sprites and draws them if they are not null, however if you create an array of sprites I have no clue what null is. So I figured I'll just make a blank sprite, name it null, populate the array with that, and use it as null. However when I try to compare the value in a point of the array to (well anything really, but in this case) the null sprite, it tells me "no match for 'operator==' (operand types are 'sf::Sprite' and 'sf::Sprite')" which I assume means that because I'm not comparing a typical data type the operators mean nadda.
This is my code so far, it's at the top of the page(after all the #include's)
(click to show/hide)

If you didn't already guess, I'm making a game where the sprites will overlap, and I would like those with a lower y value to be drawn first, so that those that are closer to the "camera" will be drawn over those "farther away". I tried googling how to do this, but the only thing I found was a lot of people asking for a z axis to be implemented in sfml, and others responding that that would be inefficient and to do this instead. Problem is, everyone's so quick to tell people off but nobody actually wants to actually help or give any idea how to do it.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: check if variable is equal to certain sprite or null
« Reply #1 on: September 18, 2019, 04:53:33 pm »
I need to create a public function that runs through an array of sprites and draws them if they are not null
Why do you "need" this, if you don't actually need these "null" sprites?

I tried googling how to do this, but the only thing I found was a lot of people asking for a z axis to be implemented in sfml, and others responding that that would be inefficient and to do this instead. Problem is, everyone's so quick to tell people off but nobody actually wants to actually help or give any idea how to do it
As with most of these claims, when I end up searching for such things myself, I pretty much will always find useful answers. ;)

It's not complicated to implement a z ordering yourself and it allows you to customize the sorting to your own setup. For example a lot of games work more with layers (background, tiles, entities, player).

struct Entity {
        Entity(std::string name, int order)
        : Name{name}
        , Order{order}
        {}
       
        std::string Name;
        int Order;
};
std::vector<Entity> entities;
entities.emplace_back("Test1", 2);
entities.emplace_back("Test2", 3);
entities.emplace_back("Test3", 1);

std::sort(entities.begin(), entities.end(), [](Entity& a, Entity& b) {
    return a.Order < b.Order;
});

Working example: https://ideone.com/uOCung
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

forestlapointe

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: check if variable is equal to certain sprite or null
« Reply #2 on: September 19, 2019, 01:13:51 pm »
It seams there is a lot about C++ that I've yet to learn, haha. Thank you this was very helpful and brought a lot of functions and techniques to my attention that I would not otherwise have thought to use.

 

anything