Created Object does not see any of its own member variables.

Working with the AGK game engine but I don't think the issue is related to it. it's more about the core code

I have an class of a button. It holds a sprite of a button and a text box which will act as a title of that button

subject_button.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 #pragma once
#include "template.h"

class subject_button
{

private:

public:


	// variables 
	float x = 10.0f;
	float y = 10.0f;

	float temp_y_blaaazzzz = 100.0f;
	uString temp_some_stuff = "blaaa blaa blaa";

	int sprite_id;
	int textbox;

	uString textbox_content = "";

	

	subject_button(int CloneSpriteSource);
	~subject_button();

	void update();
};


subject_button.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "subject_button.h"



subject_button::subject_button(int CloneSpriteSource)
{
	
	//textbox_content = folder_name;
	sprite_id = agk::LoadSprite("/media/sprites/spr_button.png");
	textbox = agk::CreateText("test");

	agk::SetSpriteScale(sprite_id, 1.0f, 1.0f);
	agk::SetSpriteVisible(sprite_id, 1);
	agk::SetSpriteScale(sprite_id, 2, 1);
	agk::SetSpriteColorAlpha(sprite_id, 90);

	agk::SetTextSize(textbox, 25);
	agk::SetTextMaxWidth(textbox, 360);
	agk::SetTextString(textbox, textbox_content);

	agk::SetSpritePosition(sprite_id, x, y);
	agk::SetTextPosition(textbox, x, y);

	agk::Print("ConstructorFired");

}


void subject_button::update() {


	//+= 0.05f;

	agk::SetTextString(textbox, textbox_content);
	agk::SetTextPosition(textbox, x + 100.0f, y + 100.0f);

	agk::SetSpritePosition(sprite_id, x, y);
	

	agk::PrintC("button text content: ");
	agk::Print(textbox_content);
	agk::PrintC("y: ");
	agk::Print(y);
	agk::Print("");
}

subject_button::~subject_button()
{
}


So first things to pay attention to.
In the cpp file, function update(); I'm printing the contents of the varuiable y using
 
agk::PrintC(y); 

Take a look at the results. I'm creating a few objects and every object has it's own update() function being run every cycle. So if I have 4 objects created, the Print would be executed 4 times. So you can tell that the objects exist and their function is indeed getting triggered. However the results of printing Y is some huge negative value, which makes me thing that it doesn't see the variable: https://imgur.com/a/IVrB9

As you can see, it's defined. I'm not doing anything else with that variable other than reading it and it returns that huge value. This happens with ANY variable I create in the header file.

Next thing to turn your attention to:

The functions of the engine
1
2
	sprite_id = agk::LoadSprite("/media/sprites/spr_button.png");
	textbox = agk::CreateText("test");

create the text box and a sprite and both return an ID of the created items to be stored and used to work with those particular items later on. When I'm actually trying to do some stuff with them in the update(); function, Visual Studio prints this in the Output

  > Text 3435973836 does not exist
   > Sprite 3435973836 does not exist


So the two factors, including the fact that I can create any new variable and printing it will result in printing nothing, makes me believe that the cpp file does not see any of the variables I created in the header file.
Last edited on
There's no such thing as an object not "seeing" its members. If the code compiles, it should work.
If you're getting garbage values that means some code modified that value between the time the constructor was called and subject_button::update() was called.

How are you calling the update() function? Is it possible you're doing something silly like
1
2
3
auto button = new subject_button(/*...*/);
delete button;
button->update();
or
1
2
3
4
5
6
7
subject_button *create_button(){
    subject_button button;
    return &button;
}

auto button = create_button();
button->update();
?
Last edited on
Hi, thanks for a quick reply.

I'm storing the pointers to the created objects into an vector so that I can fire the update script for every button object I create. I use vectors because at any point I can add or remove buttons so it made it easier than what you have to do with arrays.

The vector that stores the pointers looks like this:
 
std::vector<subject_button *> array_sprites_button_id;


when I create the object, I record it down like this:
1
2
subject_button inst_subject_button;
array_sprites_button_id.push_back(&inst_subject_button);


Now I know that the output errors about the Text and Sprite are specific to the engine but I also print a simple variable - the "y" float variable. When IT is being printed it returns some large negative float value: https://snag.gy/lYDHRI.jpg

That is something I usually see when you can't find the variable, as far as I know at least. So this is why I thought that id doesn't see the variables. BUT just keep in mind that the Print commands that are located in the update() function all fire so the print command works.

This is how I run the update function for every object of the subject_button class
1
2
3
4
5
6
7
// Run the update script for every button
for (int xx = 0; xx < array_sprites_button_id.size(); xx++) {

	// get the handle on the button object and run it's update script "loop" 
	array_sprites_button_id.at(xx)->update();

}


Now I KNOW that the update() function is being executed because inside of that function is a bunch of Print commands and when those show up on the screen for every object, that tells me that the update(); function has been executed. Should I comment out the update script, the Print disappear from the screen upon build.
Last edited on
when I create the object, I record it down like this:

Well, that's wrong. You're using a pointer to a local variable which will cease existing when the function returns which is basically the second option presented by Helios with a little indirection.
Wait so if you're creating an object, you HAVE to save the reference to it somehow so that you could address that object later on. What if you create 5 buttons? Also the game isn't made in the visual studio native main function. It's run inside of this sort of a loop (the engine comes with the template for new projects) So I don't know about it ever ending the main function since that would close the program.

1
2
3
4
int app::Loop(void) {	// MAIN GAME LOOP


}


Also IF the pointer really does get deleted then why am I still printing the stuff on the screen inside of the update() function? Shouldn't it throw an error if the pointer gets deleted once the function reaches the end?

How do you go about it correctly? I need to create the objects and save the pointers to those objects into a vector.
Last edited on
Oh Jesus IT WORKS!

I switched from pointers to smart pointers following this:
https://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one

1
2
3
4
5
// Need to create the object to achieve some goal
MyObject* ptr = new MyObject(); 
ptr->DoSomething(); // Use the object in some way
delete ptr; // Destroy the object. Done with it.
// Wait, what if DoSomething() raises an exception...? 
Thank you, both of you for explaining.
Topic archived. No new replies allowed.