0
\$\begingroup\$

I have a space shooter game and I am trying to create a shield power for the player ship. My mechanic for shield is working. When I press the shield power button(P), the shield power decreases from 100 to 0 at a certain pace.

Problem is that I am trying to draw this shield power value on screen but it's just showing the starting shield power value(which is 100) and not showing the decrease in shield power value when I press the shield button.

What am I missing?

Here's the relevant code: Game.cpp

if (keyboard->IsKeyPressed('P'))
{
    if (player_->shield == true)
        player_->shield = false;
    else
        player_->shield = true;
}

if (player_->shield && player_->shieldPower > 0)
{
    if (keyboard->IsKeyPressed(VK_SPACE))
    {
        XMVECTOR playerForward = player_->GetForwardVector();
        XMVECTOR bulletPosition = player_->GetPosition() + playerForward * 10.0f;
        SpawnBullet(bulletPosition, playerForward);
    }

    player_->shieldActive = true;
    if (GetTickCount() - player_->time > 100)
    {
        player_->time = GetTickCount();
        player_->shieldPower--;
        if (player_->shieldPower <= 40)
        {
            player_->shieldPowerLow = true;
        }
        else
        {
            player_->shieldPowerLow = false;
        }
    }
}
else
    player_->shieldActive = false;

TextClass.cpp

void TextClass::OnRender(System *system)
{
Graphics *graphics = system->GetGraphics();
FontEngine *fontEngine = graphics->GetFontEngine();
Player *player = new Player();

Game *game = system->GetGame();
game->RenderEverything(system->GetGraphics()); 

std::stringstream buffer;
buffer << player->shieldPower;

fontEngine->DrawText(buffer.str().c_str(), 50, 100, 0xffffffff, 
FontEngine::FONT_TYPE_SMALL);
}

Player.h

class Player
{
public:

int shootDelay;
int time;
int shieldPower;
bool shield;
bool shieldActive;
bool fullShield;
bool shieldPowerLow;
bool shoot;
}

Player.cpp

Player::Player() :
shootDelay(500),
shieldPower(100),
fullShield(true),
shieldPowerLow(false),
shield(false),
shieldActive(false),
shoot(false)
{
}
\$\endgroup\$
3
  • \$\begingroup\$ void PlayingState::OnRender(System *system) { //...// Player *player = new Player(); ...am I reading correctly that you're constructing a new Player instance every time TextClass::OnRender is called? \$\endgroup\$
    – DMGregory
    May 24, 2019 at 23:27
  • \$\begingroup\$ Yeah. You are reading that right. That is the problem, isn't it? How can I go about using the "existing" player then? \$\endgroup\$
    – AaySquare
    May 24, 2019 at 23:31
  • \$\begingroup\$ Player *player = new Player(); - this creates another player and asks what that player's shield power is. So now you have two players (one of which isn't used except to get its shield power). (And since it never gets deleted, after 100 ticks you have 101 players) \$\endgroup\$
    – user253751
    Aug 23, 2019 at 0:35

1 Answer 1

1
\$\begingroup\$

Your OnRender method needs to get access to several dependencies:

  • Graphics
  • Font Engine
  • Player
  • Game

And for three of the four you use one perfectly sensible pattern: a getter method accessible from the System you've been passed as an argument, or something get-able from it.

void TextClass::OnRender(System *system)
{    
    Graphics *graphics = system->GetGraphics();
    FontEngine *fontEngine = graphics->GetFontEngine();
    // ... //
    Game *game = system->GetGame();

But when it comes to the Player, you throw all that away and try to create a new Player from scratch:

Player *player = new Player();

Since this is a new instance, it always has the default values. Instead, we want to reference the existing player instance that Game.cpp is already updating each frame.

So, use the same pattern you're already using everywhere else: give Game a GetPlayer() method to return the player it's holding onto, and have the TextClass ask the Game it got from the System to get access to this existing player.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .