Distance check crashes game

I have an actor and a pawn, and when I press the space bar the code is supposed to go through the code for the pawn, find the reference to the distance checker in the other file, run that function, then go back to normal. I have used the line AItem::AItem().HasDuckFoundItem(); to reference the function. The function is supposed to get the pawns location using ADuck::ADuck().GetActorLocation(); and compare this to the items location, then if it is less than a given value (100 in this case), the item gets new random x and y coordinates and is moved to a different point on the map. However, every time the code tries to reference the function, the entire game crashes and I have to restart my editor. Here is the code:

`
if (bTurningLeft)

{


	TurningValue.Yaw -= 1;


}


if (bTurningRight)


{


	TurningValue.Yaw += 1;


}


else if (bDiving)


{


	TurningValue.Pitch -= 1;


	AItem::AItem().HasDuckFoundItem();


}

`

`
void AItem::HasDuckFoundItem()

{

FVector DuckPlace = ADuck::ADuck().GetActorLocation();


FVector ItemPlace = GetActorLocation();


float DistDuckItem = sqrtf(((ItemPlace.X - DuckPlace.X) * (ItemPlace.X - DuckPlace.X)) + ((ItemPlace.Y - DuckPlace.Y) * (ItemPlace.Y - DuckPlace.Y)));


if (DistDuckItem < 100 && !ItemFound)


{


	// Get new x and y coordinates for the item


	RandomXPlace = FMath::RandRange(165, 665);


	RandomYPlace = FMath::RandRange(165, 665);


	ItemFound = true;


}


// Place the item in its new position


FVector NewItemLocation = GetActorLocation();


NewItemLocation.X = RandomXPlace;


NewItemLocation.Y = RandomYPlace;


SetActorLocation(NewItemLocation);


ItemFound = false;

}
`

Does anyone know why this crashes the game? Have I accidentally somehow made it run the function an infinite number of times, overloading the program? Does it not like the way I move the item? What’s going on?!?!

Hi! If you could do two things you would be more likely to solve this:

  1. Make that code readable. All on one line makes people want to help you less
  2. Post the log files that are produced during this problem ()

Buddy I hate to say it but Pick up a C++ book. It looks like you guessing lines of Code here but not really understand what you actually doing.

“AItem::AItem().HasDuckFoundItem();” << Are you understanding what you do here?

Are you understanding what a refference or Pointer is? If so can you point it out in the Code above?

You do some fundamental things wrong here. Same for the Questions you Posted recently are some very simple things that should be obvious. And don´t take it in a Bad way its honest advice.

Yeah, I know I do a lot of things wrong. I am incredibly new to C++ and I honestly have no idea what I’m doing, I’m just learning by doing. I post a lot because when I look at my code, in my head it makes sense and should work, and I just don’t understand why it doesn’t sometimes. I don’t take any of this in a bad way, I know I need to try a different method of learning the language. I kinda wish there was an easier way, but there isn’t and I will invest in a C++ manual as you suggest.

I think I understand. I will try to do this, thanks for the idea.

I may have an idea.

“AItem::AItem().HasDuckFoundItem()”

You are creating a temporary local variable of type AItem but inside the HasDuckFoundItem() function you are calling Actor functions as if the item is an actor in the world – which it can’t be if you are just creating a temporary variable inside another function.

If you want to know if the duck has found a particular item, you need a pointer to the item actor and then you can do “item->HasDuckFoundItem()”.

Same goes for “ADuck::ADuck().GetActorLocation()”. The duck is an actor in the world but this statement creates a new duck actor and gets its location – which it doesn’t have because it isn’t spawned in the world.

To get the duck’s location, you need the pointer to the duck actor and you need to call “duck->GetActorLocation()”.

Hope that helps.

Indeed. You are trying to do too much too soon. You need to build up to making a video game. Like with sports, you need the fundamentals before you can have any finesse.

Anything you want to learn can be found for free online. Tutorial videos and reference sites are enough if you have the motivation to learn.

If you really want to make a game, you might want to stick to blueprints. C++ is more than you need at this point.

Also, general coding problems like this are better covered on forums like Stack Exchange.

Good luck!

PS: I didn’t look hard but it seems like what you are trying to do could be done using functions that UE4 already provides.