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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

Passing a character Pointer is null

Hi guys,

I'm trying to pass a character pointer to my spell class, and initially it is storing the pointer fine. I can access methods such as GetActorLocation(),etc. but when I try to access character specific methods, it crashes and says that the pointer is NULL (Mind you it is valid throughout until I access a pointer specific method)

I initially thought it was GC but even after UProperty() the character pointer is null. (This is a networked game)

My character .cpp


My Spell.cpp This debug msg is not firing - CASTER is valid when I initially set it

  // A reference to the caster of the spell
  MyCharacter* Caster;

 void myspell::SetCaster(mycharacter* caster)
   if (caster)
     Caster = caster;
     if(Caster == NULL){
            GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("Caster is null"));

Afterwhich I spawn my spell at: CASTER is Valid in this method


The following method I call: CASTER is NULL



EDIT1: Changed post to better explain my problem.

Product Version: Not Selected
more ▼

asked May 07 '15 at 11:31 PM in C++ Programming

avatar image

129 10 11 15

avatar image Duncan Dam May 08 '15 at 01:38 AM

what you mean be networked ? you mean you set the caster value on server ?

avatar image Venros May 08 '15 at 01:55 AM

As in this is a networked game, so i figured the easiest way to tell a spell that Iam the owner and extract some data from said owner was to go this route. Instead of iterating thru all the controllers etc. Mind you it works in the base class, but in all inherited classes its null. Hell i even used getters, and overriden base method and reset the caster in derived class, but nothing worked.

avatar image Duncan Dam May 08 '15 at 02:10 AM

Try to set the Instigator of the spell actor to this caster, this Instigator is used similar to your caster, but it's inherited from actor class, i always work for me when i use this to detect who the owner of bullet. 1 possible is you set in server and get it in client but don't replicate the caster, pointer through inherited should work.

avatar image Venros May 08 '15 at 05:11 AM

My issue is that lets say I'm casting a firespell, I need the pointer to the caster to retrieve data such as +20% to fire spells, etc. And I'm doing this for all the damage types, so there is no way to extract this data without that pointer. It just doesnt make sense why it is not working in derived. Before I inherited from the base class, setting the caster pointer actually worked ( because i set Caster-> actor location(), etc).

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

So I have tried using Ramas Solution to get local PC:

     TObjectIterator<AYourPC> Itr; //Not looping, just want the first entry
     if (!Itr) return nullptr;  //This can happen while PIE is exiting
     return *Itr;


   playerController = GetPC();
   Caster = Cast<MyCharacter>(playerController->GetPawn());

Mind you this works on the server, If I try casting from client 1, the spell spawns at the server owned character.

Also if I run the game on a dedicated server, it flat out crashes on both. Another issue is that if somehow the client runs two copies of the game, then islocallycontrolled PC will cause the 2nd client to spawn spells at the first clients location.

All I need is the caster that spawned the spell (they are on different cpp files, I even tried setting the instigator on my character.cpp and getinstigatorcontroller on myspell.cpp but that didnt work).



So I finally got this working, here is what I had to do:

Right after I initially spawn the spell (to instantiate it and add it to my spell bar), I set the owner like so: (Mind you i had to set it on the server):

 ASpellSystem* spell = GetWorld()->SpawnActor<ASpellSystem>(newSpell);

Then under my spell.cpp I set the Caster to:

   Caster = Cast<MyChar>(GetOwner());

Hope this helps someone out.


more ▼

answered May 12 '15 at 06:50 PM

avatar image

129 10 11 15

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
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