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

	      spellBar[index]->SetCaster(this);
	      spellBar[index]->CastSpell();

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
 UPROPERTY()
 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

  GetWorld()->SpawnActor<ASpellSystem>(GetClass(),
                                       GetSpellCaster()->GetActorLocation(),
                                       GetSpellCaster()->GetActorRotation(),
                                       spawnInfo);

The following method I call: CASTER is NULL

GetSpellCaster()->GetDamageModifier_Fire()

Ty

EDIT1: Changed post to better explain my problem.

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

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.

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.

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).

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

  FORCEINLINE AYourPC* GetPC()
  {
    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).

//////////////////////////////////////////////////////////////////////////////////

**

EDIT2:

**

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);
spell->SetOwner(this);
spellBar.Add(spell);

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

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

Hope this helps someone out.

Ty