Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9016849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:06:56+00:00 2026-06-16T04:06:56+00:00

I have a pointer that is set to 0 , then later on in

  • 0

I have a pointer that is set to 0, then later on in the same function, inside some loops/conditions I try to re assign it.. (please read the comments)

for(Entity* tile : _originalFloorTiles)
        {
            for(Turns turn : pointsUpLeftDownRight)
            {
                if(tile->GetCollisionRect().ContainsPoint(turn.first.x, turn.first.y)){
                    turn.second = tile; //everything looks fine here, turn.second is still null and tile is a valid pointer
                    assert(turn.second); //turn.second is definitely assigned the value of tile here.
                }
               HAPI->DebugText("check pointsUpLeftDownRight now");//!Here's where it gets weird,
            // If i hover over turn and inspect it in visual studio now, turn.second is still completely valid 
            // (with the assigned value of tile).
            // But hovering over pointsUpLeftDownRight shows its contents for each turn..
            // and inside there the current turn is a NULL pointer for turn.second!
            }
        }

So one moment i have assignd my pointer no problem, and the next moment the pointer doesn’t seem to have changed at all.

To clarify, Turns is a lazy typedef for std::pair<Vect, Entity*> , apologies if that makes my code harder to read, it’s some quickly thrown together enemy ai. I’ll post the complete function below.

I’m really stumped here and not sure if i’m being an idiot or something weird is going on, would really appreciate anyone taking the time to look.

//looks for turns that the ghost can take.

void IceGhostNPC::RespondToTimePassed()
{   
    //Entity* t = _originalFloorTiles[0];

    //test if enough time has passed since ghost last decided to look for turns
    if(_lastTimeTurned < timeGetTime() - _timeBeforeSearchingForTurns)
    {
        //store points surrounding ghost in a way that we can associate them with a valid floor tile to move onto 
        std::vector<Turns> pointsUpLeftDownRight;
        pointsUpLeftDownRight.push_back(
            Turns(Vect(GetCenterXPos(), GetCenterYPos() - floorTileHeight), 0)); //point above
        pointsUpLeftDownRight.push_back(
            Turns(Vect(GetCenterXPos() - floorTileWidth, GetCenterYPos()), 0)); //point left
        pointsUpLeftDownRight.push_back(
            Turns(Vect(GetCenterXPos(), GetCenterYPos() + floorTileHeight), 0)); //point down
        pointsUpLeftDownRight.push_back(
            Turns(Vect(GetCenterXPos() + floorTileWidth, GetCenterYPos()), 0)); //point right

        //look through original floor tiles, 
        for(Entity* tile : _originalFloorTiles)
        {
            //see if its possible to take a turn 
            for(Turns turn : pointsUpLeftDownRight)
            {
                if(tile->GetCollisionRect().ContainsPoint(turn.first.x, turn.first.y)){
                    turn.second = tile;
                    assert(turn.second);
                }
                HAPI->DebugText("check pointsUpLeftDownRight now");
            }
        }

        //Now to make the behaviour more interesting we have the ghost randomly take one of the turns,
        // ( we use associated tile to check the turn is possible, and we can also change that tile to an icy patch )
        bool turnTaken = false;
        do{
            int turnChoice = rand() % 4;
            if(pointsUpLeftDownRight[turnChoice].second == 0)
                continue; //go back to top of loop if that turn had a null tile
            else
            {
                switch(turnChoice){
                    case 0: //turn upwards
                        _moveable->SetYDirection(Controller::UP);
                        _moveable->SetXDirection(Controller::NONE);
                        break;
                    case 1: //turn leftwards
                        _moveable->SetYDirection(Controller::NONE);
                        _moveable->SetXDirection(Controller::LEFT);
                        break;
                    case 2: //turn downwards
                        _moveable->SetYDirection(Controller::DOWN);
                        _moveable->SetXDirection(Controller::NONE);
                        break;
                    case 3: //turn right
                        _moveable->SetYDirection(Controller::NONE);
                        _moveable->SetXDirection(Controller::RIGHT);
                        break;
                }
                turnTaken = true;
                _lastTimeTurned = timeGetTime();
                //ice tile up baby
            }

        }while(turnTaken = false);
    }

    FinishResponding(timeGetTime());
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:06:57+00:00Added an answer on June 16, 2026 at 4:06 am

    Check this line:

    for(Turns turn : pointsUpLeftDownRight)
    

    You are iterating over copies of the elements in pointsUpLeftDownRight. Whatever value you assign to that copy will be lost when the copy is destroyed (at the end of the for body). Your assignment changes a temporary.

    Try with this instead:

    for(Turns& turn : pointsUpLeftDownRight)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have need for a function pointer that takes two arguments and returns a
If I have a character pointer that contains NULL bytes is there any built
I have a pointer to a QScriptEngine that I'm passing through the overloaded class
I have a pointer to a map that I am trying to delete (this
I have a pointer of a structure type that I made. On program start
I'm working on a problem that uses pointer arithmetic and I have found this
I have a method that uses ARC and takes an NSError pointer and I
I have a native object (C++) that has a gcroot pointer to a managed
I have a 3x3 array that I'm trying to create a pointer to and
I have the following unordered_map, that maps a pointer to an object of type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.