# Accessing int32 variable crashes the game

**URL:** <https://forums.unrealengine.com/t/accessing-int32-variable-crashes-the-game/464793>\
**Category:** Getting Started & Setup\
**Tags:** variables, unreal-engine, crash, question, CPP\
**Created:** [May 7, 2020, 3:36am UTC](https://forums.unrealengine.com/t/accessing-int32-variable-crashes-the-game/464793 "2020-05-07T03:36:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Althaen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Althaen](https://forums.unrealengine.com/u/Althaen)\
**Post date:** [May 7, 2020, 3:36am UTC](https://forums.unrealengine.com/t/accessing-int32-variable-crashes-the-game/464793/1 "2020-05-07T03:36:04Z")

</div>

I have a grid that I use as a map. I set an int value for the width and height but when I try to read these variables the game crashes.

If I try to print out the value just after setting it the game does not crash but from another function it will.

**Declarations:**

```
	UPROPERTY(EditAnywhere)
		int32 MapWidth;

	UPROPERTY(EditAnywhere)
		int32 MapHeight;

```

**Functions:**

```
void UGameMap::Generate(int32 Width, int32 Height)
{
	MapWidth = Width;
	MapHeight = Height;

   	Print(FString::FromInt(MapWidth)); //does not crash the game

	for (size_t x = 0; x < 50; x++)
	{
		for (size_t y = 0; y < 50; y++)
		{
			FWorldTile Tile;
			WorldTiles.Add(Tile);
		}
	}
}

bool UGameMap::InMapBounds(FVector2D TilePosition, FVector2D BuildingSize)
{
	Print(FString::FromInt(MapWidth)); //crashes the game

	if (TilePosition.X < 0 || TilePosition.X + BuildingSize.X > 50)
		return false;
		//|| TilePosition.X + BuildingSize.X > MapWidth || TilePosition.Y < 0 || TilePosition.Y + BuildingSize.Y > MapHeight)
	//	return false;

	return true;
}
```

---

<div class="post-metadata">

**Author:** ![Bino](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bino](https://forums.unrealengine.com/u/Bino)\
**Post date:** [May 7, 2020, 4:27am UTC](https://forums.unrealengine.com/t/accessing-int32-variable-crashes-the-game/464793/2 "2020-05-07T04:27:02Z")

</div>

Logging these issues you should add a callstack to make it a bit easier. Have you check what the value MapWidth is with a debugger just before the crash? Are you 100% sure it’s initialized?

Looking at the implementation FString::FromInt it could (??) crash but I doubt it after testing it out.

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [May 7, 2020, 12:19pm UTC](https://forums.unrealengine.com/t/accessing-int32-variable-crashes-the-game/464793/3 "2020-05-07T12:19:37Z")

</div>

Also how do you create those object classes? they are reference them somewhere afterwards? There possibility they get garbage collected, which may create invalid pointers and invalid “this”. I this case function code will run but will crash in first attempt to access local object variables as this pointer will be invalid.

---

<div class="post-metadata">

**Author:** ![Althaen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Althaen](https://forums.unrealengine.com/u/Althaen)\
**Post date:** [May 8, 2020, 1:03am UTC](https://forums.unrealengine.com/t/accessing-int32-variable-crashes-the-game/464793/4 "2020-05-08T01:03:28Z")

</div>

Turns out I forgot to set the pointer I am trying to access to an object.

---

<div class="post-metadata">

**Author:** ![Althaen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Althaen](https://forums.unrealengine.com/u/Althaen)\
**Post date:** [May 8, 2020, 1:05am UTC](https://forums.unrealengine.com/t/accessing-int32-variable-crashes-the-game/464793/5 "2020-05-08T01:05:07Z")

</div>

Forgot to set object as a variable so it was a nullptr.

---

<div class="post-metadata">

**Author:** ![MrAftonWilliam](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MrAftonWilliam](https://forums.unrealengine.com/u/MrAftonWilliam)\
**Post date:** [June 30, 2024, 3:21am UTC](https://forums.unrealengine.com/t/accessing-int32-variable-crashes-the-game/464793/6 "2024-06-30T03:21:32Z")

</div>

Hey! Can you elaborate on this further? I don’t see any object, and Im having the same issue.
