SOLUTION: It seems I was being really rigid in my idea of how to declare and define the objects I was working with. I was declaring and defining the objects generally in the same scope. The solution I found was to declare the objects outside the scope of the function that I wanted to change the objects' value and then make calls to that objects member functions (thus defining them, I think) inside the function in question. I hope that explains it.
What it looks like:
overworldTime() previously looked like this:
class gameTime {
void overworldTime() {
overworldLocations cityA; //objects are declared //
overworldLocations cityB;
cityB.overworldCity({(float)windowSizeX/8, (float)windowSizeY/8}, "My Second City");
cityA.overworldCity({(float)windowSizeX/3, (float)windowSizeY/3}, "My First City");
// ..and objects are defined right away //
// do stuff //
}; // end gameTime
now is looks like this:
overworldLocations cityA; //objects are declared outside of gameTime //
overworldLocations cityB;
class gameTime {
void overworldTime() {
cityB.overworldCity({(float)windowSizeX/8, (float)windowSizeY/8}, "My Second City");
cityA.overworldCity({(float)windowSizeX/3, (float)windowSizeY/3}, "My First City");
// ..and they are still defined in the same place //
// do stuff //
}; // end gameTime
/editThis is more of a general C++ question I believe, but because I'm using SFML to work on this project I figured I'd ask around for help here before going elsewhere.
I have a set of classes and I'd like for their variables to be shared. Easy enough, however I have a function within one of the classes that is meant to incriment the value of the shared variable. In my logger I note that the incriment occurs once and then reverts to the initial value, and this process just repeats itself.
The goal is to get
cityFood which is defined in class
overworldLocations to be incrimented in the loop in
overworldTime() which is a function of class
gameTime.
Here are the two classes ( I'm sure there are errors everywhere, but everthing works aside from this one problem) overworldLocations:
#include "text_function.hpp"
#include <memory>
unsigned int cityPosX{(unsigned int)windowSizeX/3}, cityPosY{(unsigned int)windowSizeY/3};
sf::Font font, cityFont;
class overworldLocations {
public:
static int cityFood, cityIndustry, cityPopulation;
friend class gameTime;
int resourceSetter(int a, int b, int c) {
cityFood = a;
cityIndustry = b;
cityPopulation = c;
}
int resourceGetter() {
return cityFood, cityIndustry, cityPopulation;
}
void overworldCity(sf::Vector2f cityPos, std::string cityName) {
float citySizeX{100}, citySizeY{100};
sf::RectangleShape cityShape(sf::Vector2f(citySizeX, citySizeY));
cityShape.setPosition(cityPos);
window.draw(cityShape);
sf::Vector2f cityPosition{cityShape.getPosition()};
float x = cityPosition.x;
float y = cityPosition.y;
sf::Vector2f cityPosVector{cityPosition.x + 25, cityPosition.y + 80};
// *foodPointer = cityFood;
std::string foodString = std::to_string(cityFood), industryString = std::to_string(cityIndustry), populationString = std::to_string(cityPopulation);
text_function textObject(cityName, cityFont, cityPosVector, sf::Color::Black, 10);
text_function cityTextObject1("F", cityFont, {cityPosition.x, cityPosition.y - 15}, sf::Color::Green, 12);
text_function cityResourceText1(foodString, cityFont, {cityPosition.x + 7, cityPosition.y - 15}, sf::Color::Green, 12);
text_function cityTextObject2("I", cityFont, {cityPosition.x + 40, cityPosition.y - 15}, sf::Color::Red, 12);
text_function cityResourceText2(industryString, cityFont, {cityPosition.x + 47, cityPosition.y - 15}, sf::Color::Red, 12);
text_function cityTextObject3("P", cityFont, {cityPosition.x + 80, cityPosition.y - 15}, sf::Color::Yellow, 12);
text_function cityResourceText3(populationString, cityFont, {cityPosition.x + 87, cityPosition.y - 15}, sf::Color::Yellow, 12);
};
};
int overworldLocations::cityFood{10}, overworldLocations::cityIndustry{10}, overworldLocations::cityPopulation{10};
and gameTime:
#include "overworldLocations.hpp"
#include "drawable_objects.hpp"
sf::Font overworldFont;
bool timePaused = true;
sf::Clock overworldClock;
sf::Text overworldText, overworldCurrentDay;
float overworldObjectPosX{(float)windowSizeX/windowSizeX}, overworldObjectPosY{(float)windowSizeY - 30};
sf::Vector2f overworldCurrentDayObjectPos{(float)windowSizeX/windowSizeX + 50, (float)windowSizeY - 30};
class gameTime : public overworldLocations {
public:
static int currentDay;
friend class overworldLocations;
void overworldTime(int& reference) {
sf::Time wholeDay = sf::seconds(1.0f);
sf::Time timePassed = overworldClock.getElapsedTime();
if(timePassed > wholeDay) {
std::cout
<< "OVERWORLD TIME "
<< timePassed.asSeconds()
<< std::endl;
overworldClock.restart();
if(!timePaused) {
if(currentDay >= 7) {
currentDay = 0;
overworldLocations::cityFood += 1;
std::cout
<< "OL CF is "
<< overworldLocations::cityFood
<< std::endl;
std::cout
<< "GT CF is "
<< cityFood
<< std::endl;
std::cout
<< "reference to cityFood is "
<< reference
<< std::endl;
}
// std::cout
// << "shared_ptr & is "
// << &sharedfoodPointer
// << std::endl;
// std::cout
// << "shared_ptr * is "
// << *sharedfoodPointer
// << std::endl;
// std::cout
// << "shared_ptr is "
// << sharedfoodPointer
// << std::endl;
}
currentDay ++;
std::cout
<< "Day "
<< currentDay
<< std::endl;
}
overworldLocations::cityFood += 1;
std::string currentDayString = std::to_string(currentDay);
text_function overworldTextObject("Day:", overworldFont, {overworldObjectPosX, overworldObjectPosY}, sf::Color::Red, 24);
text_function overworldTextCurrentDay(currentDayString, overworldFont, overworldCurrentDayObjectPos, sf::Color::Red, 24);
};
};
int gameTime::currentDay{1};
and the main.cpp:
#include <SFML/System.hpp>
#include <SFML/WIndow.hpp>
#include <SFML/Graphics.hpp>
#include "include/window.hpp"
#include <iostream>
#include <thread>
#include <string>
#include "include/eventPoll.hpp"
int main() {
if (!cityFont.loadFromFile("impact.ttf")) {
std::cout
<< "error loading font";
}
if (!overworldFont.loadFromFile("impact.ttf")) {
std::cout
<< "error loading font";
}
if (!font.loadFromFile("impact.ttf")) {
std::cout
<< "error loading font";
}
while(true) {
window.clear(black);
gameTime gameClock;
gameClock.overworldTime(overworldLocations::cityFood);
character();
drawable_objects();
eventPoll();
window.display();
if(isClosed == true) {
break;
}
}
return 0;
} //main()
There is more I can put here if its neccesary but I believe my problem lies within these bits of code.
I can achieve the desired effect by making
cityFood global - but alot of information suggests that is bad practice, and theres obviously something fundemental I'm missing so I'd like to learn how to get the results I want without using globals. The last thing I tried was passing by reference, as you can see in the
overworldTime() function but I did not notice any change.
Any help would be appreciated, I hope I provided enough information.
EDIT: I forgot to add the code where I make instances of the overworldLocations class. I have those objects in a function called
drawable_objects which is called in
main :
void drawable_objects() {
overworldLocations cityA, cityB;
cityA.resourceSetter(10,10,10);
cityB.resourceSetter(10,10,10);
cityB.overworldCity({(float)windowSizeX/8, (float)windowSizeY/8}, "My Second City");
cityA.overworldCity({(float)windowSizeX/3, (float)windowSizeY/3}, "My First City");
}