other class cant change variables in another class.
Moderator: Coders of Rage
other class cant change variables in another class.
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.
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
- programmerinprogress
- 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.
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?
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
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
Re: other class cant change variables in another class.
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.
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
- programmerinprogress
- 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.
I think your design sounds a little flawed.
I would do it a different way.
I would do something like this:
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;
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 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();
}
}
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
{
}
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
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
Re: other class cant change variables in another class.
okey, now im really confused........
i did like you said and checked for collision in my main function
something like this:
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...
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
- programmerinprogress
- 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.
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.
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
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
Re: other class cant change variables in another class.
Code: Select all
void player2::tagged()
{
isDead = true;
SDL_FreeSurface( PLAYER2 );
PLAYER2 = NULL;
PLAYER2 = load_image( "files\\pictures\\frozen.png" );
show();
}
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
- programmerinprogress
- 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.
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
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
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
Re: other class cant change variables in another class.
what??...
that´s exactly what im doing in the main function
that´s exactly what im doing in the main function
Code: Select all
if( check_collision(P1,P2) )
{
P2.tagged();
}
TorteXXX
- programmerinprogress
- 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.
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'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
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
- Ginto8
- 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.
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.
Re: other class cant change variables in another class.
well thanks anyways.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.
also i just wrote setTag() cause you wrote it in my program i use tagged(),
TorteXXX
- LeonBlade
- 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.
I smell a noob... lol...
It's cool, we all were noobs at one point...
It's cool, we all were noobs at one point...
There's no place like ~/
- programmerinprogress
- 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.
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
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
- LeonBlade
- 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.
Yeah he is...
If you want to change variables, use a Public Get/Set
If you want to change variables, use a Public Get/Set
There's no place like ~/