# Please Explain this code

**URL:** <https://forums.unrealengine.com/t/please-explain-this-code/484428>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [June 4, 2021, 4:47pm UTC](https://forums.unrealengine.com/t/please-explain-this-code/484428 "2021-06-04T16:47:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shivansh\_pandey2](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/shivansh_pandey2/32/710746_2.png) [@shivansh\_pandey2](https://forums.unrealengine.com/u/shivansh_pandey2)\
**Post date:** [June 4, 2021, 4:47pm UTC](https://forums.unrealengine.com/t/please-explain-this-code/484428/1 "2021-06-04T16:47:36Z")

</div>

Please Explain me this:

```
UWorld* TheWorld = GetWorld();

if (TheWorld != nullptr)
{

AGameModeBase* GameMode =
UGameplayStatics::GetGameMode(TheWorld);
AMyGameModeBase * MyGameMode =
Cast<AMyGameModeBase>(GameMode);

}

```

I want to know that what these (listed below line1 , Line2 , line3) lines do in the code above.

```
UWorld* TheWorld = GetWorld(); //Line1

AGameModeBase* GameMode = UGameplayStatics::GetGameMode(TheWorld); //Line2

AMyGameModeBase * MyGameMode = Cast<AMyGameModeBase>(GameMode); //Line3

```

Thanks in advance…

---

<div class="post-metadata">

**Author:** ![Tuerer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tuerer/32/1008648_2.png) [@Tuerer](https://forums.unrealengine.com/u/Tuerer)\
**Post date:** [June 4, 2021, 6:02pm UTC](https://forums.unrealengine.com/t/please-explain-this-code/484428/2 "2021-06-04T18:02:49Z")

</div>

1. Gets a reference to World. It’s occasionally needed for various functions, and GetGameMode() is one of them.
2. Gets a reference to the GameMode that’s selected in your Project Settings. But since this function gets the GameModeBase, you need to 3:
3. Cast it to your specific GameMode in order to access its specific functions and Variables.
