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"

How to get a reference to my GameMode class instance

Hi All,

I've searched the web and the source code but can't figure it out. I am trying to call a function in my GameMode class from my Character.

Thanks.

Product Version: Not Selected
Tags:
more ▼

asked Apr 05 '14 at 07:19 PM in C++ Programming

avatar image

staticvoidlol
2.6k 95 40 111

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

1 answer: sort voted first

GetWorld()->GetAuthGameMode()

It will return AGameMode pointer so you will need to cast it to your gamemode class pointer fi you want to call your code in it

(ASomeGameMode*)GetWorld()->GetAuthGameMode()

more ▼

answered Apr 05 '14 at 07:31 PM

avatar image

Shadowriver
35.2k 921 166 1098

avatar image staticvoidlol Apr 05 '14 at 07:43 PM

Ah thanks very much! I found the solution just as this notification rolled in :/.

avatar image staticvoidlol Apr 06 '14 at 10:41 AM

I just have another quick question:

When casting to e.g. "MyAwesomeGameMode", is the only way to get the compiler to recognize this class to include it in the class I'm trying to do the cast in?

i.e. by adding #include "MyAwesomeGameMode.h"

avatar image Chrys May 25 '14 at 01:36 PM

You can add the header inclusion in you PCH file, that one you have to include it anyway at the start of your cpp or your project won't compile. Other than that if you declare your game mode member pointer of your class you can simply forward declare it. class MyGameMode; before your class declaration where you want to store the pointer to your game mode. Once in the cpp you must have an include of your game mode header somewhere though, be it a direct include or via PCH.

avatar image Kochab May 25 '14 at 07:12 AM

I believe there is a typo in your 2nd code line. World should be GetWorld.

 ASomeGameMode* gm = (ASomeGameMode*)GetWorld()->GetAuthGameMode();

avatar image Shadowriver May 25 '14 at 04:01 PM

indeed there is

avatar image simmania Feb 19 '15 at 08:56 AM

Note that GameMode is a Server only object. It will return NULL in a network game on the clients.

avatar image SlimyDeath Nov 11 '16 at 03:36 PM

maybe

 if(GetWorld()) {
   auto gamemode = (ASomeGameMode*)GetWorld()->GetAuthGameMode();
 }

is the better choice, because it stops the editor from crashing. (if you put it into the constructor at least)

avatar image a-m-k May 19 '18 at 07:56 PM

Is it possible that calling the function GetAuthGameMode inside the constructor of the actor crashes the editor? Because that's what is happening. I can call the function in BeginPlay, but not in the constructor. Is that normal?

avatar image rahulxxl Jun 29 '18 at 08:10 PM

Yups, Its normal. When we call the GetAuthGameMode function, it returns the pointer to actual object. In construction phase, the object is not present. (A kind of NULL). That is why editor crashes. But in the BeginPlay function, all of the construction phase is completed and the GameMode object now actually exists. Hence, no editor crashes

(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