x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Casting GameMode returns nullptr

If been struggling to solve this for two days now. In my gameMode called AGameMode_Cell I got a enum in which I store which of two basic control settings is currently active. I also got a blueprint derived from it in which I just set that the cursor is visible ingame.

I want to access my gameMode in my character controller through this code AGameMode_Cell* gameMode = Cast<AGameMode_Cell>(UGameplayStatics::GetGameMode(this)); Since I'm a good little coder I check if the cast is successful before continuing but it returns a nullptr every time. So i checked what GetGameMode() actually returns and turns out it is of the type GameMode_Cell_C which is seemingly incompatible with AGameMode_Cell.

I've been trying other methods of casting it and also tried doing it completly in the character controller blueprint but it also didn't work.

It would be fantastic if someone could help me with this problem. It's been driving me crazy for like two days now.

Product Version: UE 4.18
Tags:
more ▼

asked Mar 05 '18 at 08:34 PM in C++ Programming

avatar image

GameAboutThings
3 1 1 4

avatar image Shadowriver Mar 06 '18 at 02:53 PM

Is this multiplayer game?

avatar image GameAboutThings Mar 06 '18 at 02:57 PM

It isn't. If the default empty game isn't then it isn't.

avatar image Shadowriver Mar 06 '18 at 04:07 PM

ok do this command in console GetAll GameMode it should display which GameMode class is on

avatar image GameAboutThings Mar 06 '18 at 05:10 PM

I tried that command and the console didn't output anything. So I tried it with GetWorld()->GetAuthGameMode()->GetClass()->GetName(). That returned GameMode_Cell_C

avatar image AmphDev Mar 06 '18 at 05:18 PM

GameMode_Cell_C means that is a blueprint class... so thats made from AGameModeCell, so basically you can cast that to AGameModeCell

Can you paste the whole code and some screenshot about your world config / gamemode bp?

avatar image GameAboutThings Mar 06 '18 at 05:34 PM

!!>First of all: I changed the bleuprint name to GameMode_Cell_BP because I tried something and wanted to stick to naming conventions which I hand't previously. It shouldn't affect this problem.

avatar image GameAboutThings Mar 06 '18 at 06:16 PM

So now the returned GameMode is GameMode_Cell_BP_C

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

I'*ll recreate the whole blueprint step by step and if it works, fine, if not I'll find a different place to store the control settings.

more ▼

answered Mar 06 '18 at 06:31 PM

avatar image

GameAboutThings
3 1 1 4

avatar image AmphDev Mar 06 '18 at 06:37 PM

oh lol wait a sec... try to do this with delay node... maybe since begin play can run "fast" and world creating is not finished in editor

avatar image GameAboutThings Mar 06 '18 at 06:57 PM

Oh come on.... Adding a timer in code actually worked.

avatar image AmphDev Mar 06 '18 at 07:02 PM

well sometimes references just simply not set when beginplay happens.. since when you play in editor everything happens immediatly... and actor life cycles may spawn player / controller before world set his gamemode...

avatar image GameAboutThings Mar 06 '18 at 07:05 PM

I will consider this in future whem something like this happens.

(comments are locked)
10|2000 characters needed characters left

Hello!

  1. did you using that gamemode or that gamemode is configured as default gamemode in world settings? ( https://docs.unrealengine.com/latest/INT/Gameplay/Framework/GameMode/ )

  2. is this a multiplayer game and you try maybe get gamemode on client? Because on client gamemode is not exist.

  3. you can use AGameMode_Cell* gameMode = Cast<AGameMode_Cell>(GetWorld()->GetAuthGameMode());

more ▼

answered Mar 05 '18 at 09:29 PM

avatar image

AmphDev
1.5k 15 9 33

avatar image GameAboutThings Mar 05 '18 at 09:34 PM

It is my default GameMode in that world.

I didn't change anything from the default settings of the empty project, so I assume it is single player.

I used GetAuthGameMode() with the exact same result.

avatar image AmphDev Mar 05 '18 at 10:08 PM

and you inherited a new bp gamemode class from your gamemode?

avatar image GameAboutThings Mar 06 '18 at 01:28 AM

Yes. I created it with a right click on the c++ class and used 'create bp class derived from this' (or whatever it says)

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question