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

Author Topic: Unable to reference position in differen't script  (Read 4687 times)

0 Members and 2 Guests are viewing this topic.

ChikenMilk

  • Newbie
  • *
  • Posts: 3
    • View Profile
Unable to reference position in differen't script
« on: July 19, 2021, 10:13:44 pm »
I'm attempting to make Pong in SFML and trying to make a bot to play against. But when I try to reference for the balls Y position inside of the bot it's only giving me the initialized position and not updating for the current position. The position does update inside of the ball script itself but when taking it to the bot it just stays put.I'll include the two CPP files just in case it can help.
Bot
(click to show/hide)
Ball
(click to show/hide)

New to SFML so my code probably looks weird.



Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Unable to reference position in differen't script
« Reply #1 on: July 19, 2021, 11:16:23 pm »
well, if I understood correctly (hard to tell without the classes) this function inside Albert::Update doesn't seems to do anything:
this->ball.ballPos = this->ball.getBall().getPosition().y;

you are getting the position of the ball and setting it to the same position it already is?
Visit my game site (and hopefully help funding it? )
Website | IndieDB

ChikenMilk

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Unable to reference position in differen't script
« Reply #2 on: July 19, 2021, 11:18:49 pm »
That was me attempting to see if setting it inside of the bot would've actually set the position correctly, which it didn't.

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Unable to reference position in differen't script
« Reply #3 on: July 20, 2021, 02:53:04 am »
Without being able to see the class declarations I'm just guessing here, but it looks like there might be more than one ball.
Albert has a ball member which isn't a pointer, it looks like it might be a duplicate of the ball from the main game code. So the main ball is being moved around, but Albert is looking at a duplicate that isn't updated.

ChikenMilk

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Unable to reference position in differen't script
« Reply #4 on: July 20, 2021, 03:11:49 am »
These are the header files

Ball
(click to show/hide)

Bot
(click to show/hide)

Since it's not working then I'm guessing this is the wrong way to reference it to get stuff from it.

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Unable to reference position in differen't script
« Reply #5 on: July 21, 2021, 02:57:16 pm »
Ok, Albert contains a Ball member.
Does the main game contain a ball too, or is it directly accessing Albert's ball?

If the game has a ball, then the issue is that the game's ball is the one that's updating and Albert's ball isn't updated. There's two balls, one is moving and rendering and the other (not rendered or updated) is the one Albert is watching.

The easiest way to handle that case would be to change Albert's ball to a ball pointer:
Ball *ball;
Then get the game to set that pointer to the address of the game's ball.
//Something like:
albert.ball = &mainBall;
You'll also need to change all of Albert's code that accesses the ball from ball. to ball-> as it's now a pointer.

Here's an example of what I mean (not working code, just an outline):
// Ball.h
class Ball
{
   // Stuff
};

// Albert.h
class Albert
{
   // Stuff
   Ball *ball;
public:
   void setBall(Ball *b) { ball = b; }
};

//main.cpp
#include "Ball.h"
#include "Albert.h"

int main()
{
   Ball mainBall;
   Albert albert;
   albert.setBall(&mainBall);

   // Do all the game stuff

   return 0;
}
 

 

anything