other class cant change variables in another class.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
Vortex
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Sat Nov 15, 2008 1:00 pm

other class cant change variables in another class.

Post by Vortex »

hello i have a problem,
lets say my player1 class needs to execute a function in the player2 class, that works fine, the function will change 1 boolean and then it will change the sprite of a specific surface.
however it appears that it dosent change the boolean in the player2 class. ive tryed to write out if the boolean is changed or not and it sais it is not so how do i make it change it??



also sorry for making so many topics with my problems, game programming is harder then i tough.
TorteXXX
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: other class cant change variables in another class.

Post by programmerinprogress »

Could you elaborate, I don't quite understand what you're asking, sorry...

EDIT: I don't want to misunderstand your question and tell you the wrong answer, could you explain what you're trying to achieve overall?
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Vortex
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Sat Nov 15, 2008 1:00 pm

Re: other class cant change variables in another class.

Post by Vortex »

okay ill explain a bit more,
you see ive got a player1 class that in one of its functions checks for collision with a player2 class alright?
if it found an collision it will run the P2.tagged() function ( P2 is a player2 class and .tagged() is a function that changes a boolean variable to true and change the sprite of the PLAYER2 surface alright? )
in the "game" when player1 collide with player2 the PLAYER2 surface changes sprite so i assume that the function was executed
however if the boolean is changed to true the player2 class shouldent be able to move and this works cause when player2 collide with player1 the variable is changed via the tagged() function in the player2 class. and player2 wont be able to move. so my problem is that player2 still can move and that it wont change the variable.

if u want ill post the classes or parts of them.
TorteXXX
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: other class cant change variables in another class.

Post by programmerinprogress »

I think your design sounds a little flawed.

I would do it a different way.

I would do something like this:

Code: Select all

class PlayerOne
{
    public: 
        void setTag();
    private: 
        bool tagged; 

};

void PlayerOne::setTag()
{
 Tag = true; // or Tag = !Tag if you want to reverse the action each time you collide
}

class PlayerTwo
{
    public: 
        setTag(); // this is of course if you want to set tags for the other player, you would just do the same thing as above 
    private:
        bool tagged;  

};


// some separate function (for now) 
bool Check_Collision(PlayerOne P1, PlayerTwo P2)
{
 // collision stuff , return a true or false 
}


int main(int argc char* argv[])
{
     PlayerOne P1; 
     PlayerTwo P2; 
    if(Check_Collision(P1,P2))
    {
       P1.setTag(); 
    }
}
Or Something to that effect...

Basically, Accessors are used to change the tagged value to true, if there's a collison.
I mean you could write a checkCollision function, which is a method of PlayerOne I guess, but you would have to be sure to either declare + define the Player to collide with first, or simply declare that there will be a class to collide with, like this;

Code: Select all

class PlayerTwo; 

class PlayerOne
{
// class crap 
bool Check_Collision(PlayerOne P1, PlayerTwo P2); 
}

class PlayerTwo
{
}
another alternative is to perhaps come up with a common ' player' class and derive two different types of player from that base class...
if nothing really differs between a playerTwo class and PlayerOne, then just declare two Objects, and test them against eachother.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Vortex
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Sat Nov 15, 2008 1:00 pm

Re: other class cant change variables in another class.

Post by Vortex »

okey, now im really confused........
i did like you said and checked for collision in my main function
something like this:

Code: Select all

int main(int argc char* argv[])
{
     PlayerOne P1;
     PlayerTwo P2;



    while(1)
{

    P1.handle_input() // handles input from both players.
    P2.handle_input()

   if(P1.alive())
{
  P1.move();
}
  if(P2.alive())
{
  P2.move();
}
    if(Check_Collision(P1,P2))
    {
       P1.setTag();
    }


more nonrelated stuff.


}
}

now when they collide nothing happens ( before atleast it changed the sprite. ) exept from them stopping to move wich i check in the move() function...

it might be cause im really tired or something is really wrong here...
TorteXXX
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: other class cant change variables in another class.

Post by programmerinprogress »

did you program it so that the sprite changes based on the value of the tag member(or whatever you called it)?

clearly you tested movement based on that, but you also have to tell the thing to use another picture when they collide or something to that effect.
Last edited by programmerinprogress on Sat Jan 24, 2009 5:09 pm, edited 1 time in total.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Vortex
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Sat Nov 15, 2008 1:00 pm

Re: other class cant change variables in another class.

Post by Vortex »

Code: Select all

void player2::tagged()
{
	isDead = true;
	SDL_FreeSurface( PLAYER2 );
	PLAYER2 = NULL;
	PLAYER2 = load_image( "files\\pictures\\frozen.png" );	
	show();
}
sorry dont really understand what you mean, heres the function that changes the sprite however.


but i dont think the function is executed cause when i checked for collision inside player1 it atleast changed the sprite, and now it should atleast also change the sprite cause i didint change anything exept from checking for collision in the main function....
im really confused..
TorteXXX
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: other class cant change variables in another class.

Post by programmerinprogress »

My example was deliberately abstract, to show you how YOU could implement a similar solution (it was more about the algorithm than the exact meaning)

however, you could solve your problem by doing this

Code: Select all

if(Check_Collision(P1,P2))
    {
       P2.tagged(); // this is YOUR version, this should change the sprite and such, my example was using a hypothetical scenario
    }


---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Vortex
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Sat Nov 15, 2008 1:00 pm

Re: other class cant change variables in another class.

Post by Vortex »

what??...
that´s exactly what im doing in the main function

Code: Select all

if( check_collision(P1,P2) )
{
 P2.tagged();
}
TorteXXX
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: other class cant change variables in another class.

Post by programmerinprogress »

in the code snippet you showed me it calling P1.setTag(); not P2.tagged();

I've obviously misguided you,I think it would require me to write out a shedload of code to explain this one, and you know it's too late, so trying figure it out, I tried my best to assist you.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: other class cant change variables in another class.

Post by Ginto8 »

If this is a game of tag, you could do something like this:

Code: Select all

if( collides( P1, P2 ) )
{
    if( P1.is_it() )
       P2.tagged();
    else if( P2.is_it() )
       P1.tagged();
}
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
Vortex
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Sat Nov 15, 2008 1:00 pm

Re: other class cant change variables in another class.

Post by Vortex »

programmerinprogress wrote:in the code snippet you showed me it calling P1.setTag(); not P2.tagged();

I've obviously misguided you,I think it would require me to write out a shedload of code to explain this one, and you know it's too late, so trying figure it out, I tried my best to assist you.
well thanks anyways.

also i just wrote setTag() cause you wrote it in my program i use tagged(),
TorteXXX
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: other class cant change variables in another class.

Post by LeonBlade »

I smell a noob... lol...

It's cool, we all were noobs at one point...
There's no place like ~/
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: other class cant change variables in another class.

Post by programmerinprogress »

Vortex is cool, he just needs to pick a few things up (and I need to explain things better) ;)
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: other class cant change variables in another class.

Post by LeonBlade »

Yeah he is...

If you want to change variables, use a Public Get/Set
There's no place like ~/
Post Reply