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

Author Topic: How to pass member variables of type sfml as parameters other classes  (Read 2927 times)

0 Members and 1 Guest are viewing this topic.

Mykal

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
I am working on a game and am planning on creating a text management class that will basically place text anywhere on the screen. I am trying to use a class member variable of sfml type as a parameter and I can figure out how to make it work. below is a few snippets of code.

text manager class function

void TextManager::SetUpText(sf::Text text, sf::FloatRect text_rect, sf::Color color, std::string word, int size, sf::Font, float width, float height)
{
   text.setString(word);
   text.setFillColor(color);
   text.setCharacterSize(size);
   text_rect = text.getLocalBounds();
   text.setOrigin(text_rect.left + text_rect.width / 2.0f,
      text_rect.top + text_rect.height / 2.0f);
   text.setPosition(sf::Vector2f(width / 2.0f, height / 4.0f));
}

I'm trying to use this function in one of my title screen class functions, but the parameters I'm entering aren't working.

these are the title screen member variables I'm trying to use

   sf::Font start_Font;
   sf::Text start_Title_Text;
   sf::FloatRect start_Title_TextRect;

I'm trying to use them in the function from the text manager class

here is the function in the title screen class

void StartScreen::SetUpScreen(int width, int height)
{

   start_BACKGROUND.loadFromFile("Background.png");
   start_Font.loadFromFile("Title.ttf");
   start_Title_Text.setFont(start_Font);

   SetUpText(start_Title_Text, start_Title_TextRect, sf::Color::White, "Title", 72, start_Font, width, height);
}

but the start_Title_Text, start_Title_TextRect and start_Font are all saying
"no suitable user-defined conversion from sf::type" to "TitleScreen" exists"

JayhawkZombie

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: How to pass member variables of type sfml as parameters other classes
« Reply #1 on: February 08, 2017, 08:53:11 pm »
Wouldn't you need to do
TextManager::SetUpText
instead? Is the TextManager class method static or does it need an object to be used?

If there is no actual TextManager object, you can just place that method in a namespace and have the function available globally.

You say your class is called "TitleScreen"(?) but you have "StartScreen::SetUpScreen".

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to pass member variables of type sfml as parameters other classes
« Reply #2 on: February 08, 2017, 09:06:28 pm »
An instance that you intend to modify in a function must be passed by reference.
Laurent Gomila - SFML developer

Mykal

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: How to pass member variables of type sfml as parameters other classes
« Reply #3 on: February 09, 2017, 06:38:38 am »
Thanks! that worked Laurent!.. hmm that's an interesting idea with the namespaces, maybe ill have to try that method out jayhawk and see which way works better

 

anything