Crash for no reason | C++

character is alive in moment that i call UseSpellWithCast(), now code looking like this

and the StartCasting() i call into mainchar in function that i call UseSpell()
image

You are calling interface functions directly again. Please refrain from doing that, you are crashing your game because of it. In AMainChar you need to invoke the interface command with execute.

Please install the debug symbols to get normal error messages and not shot’s in the dark with just line numbers.



void AMainChar::SpellCast() {
	if (Magic == nullptr)return;
	if (Magic->GetNeedCast()) {
		UE_LOG(LogTemp, Display, TEXT("Need cast"));

		//	StartCasting(); <---- WRONG DO NOT CALL THE INTERFACE FUNCTION DIRECTLY!
		if (GetClass()->ImplementsInterface(UINF_MainChar::StaticClass()))
		{
			IINF_MainChar::Execute_StartCasting(this);
		}				
	}
}

i tried with it, sorry mate, i dont have a crash in MainChar, and in other cpp files where i use interface functions, i have it in MagicComponent om line 51 and 61

Your code clearly shows you are calling interface commands directly from the character. If you had debug symbols the engine would give you an exact error telling you not to do that.

I cannot help you any further if you keep omitting the truth.

1 Like

Not in case of ActorComponents, it’s optional. Just like you don’t have to do this when you spawn an actor in the world, an actorcomponent added to an actor is linked automatically.

Well that’s not the cause of the crashes.

The OP keeps insisting on calling the interface commands directly when his character is implementing said interface.

The engine clearly states the cause of the crashes, suggesting to call the execute command in place of direct function calls.

1 Like

i can try one more time with Execute_Functions, and will see

What UE version are you on anyway? In 4.27 or earlier it was like this:

if (OtherOwner->Implements<UGrabInteractionInterface>()) {
	bCanBeGrabbed = IGrabInteractionInterface::Execute_CanBeGrabbed(OtherOwner, InPrimitiveComponent);
}
else if (const IGrabInteractionInterface* OtherInterface = Cast<IGrabInteractionInterface>(OtherOwner)) {
	if (OtherInterface) {
		bCanBeGrabbed = OtherInterface->CanBeGrabbed(InPrimitiveComponent);
	}
}

Things should be simplified since, but you can try this anyway since it’s working code on my end.

Remember to also replace the direct call to UseSpell. It too has to be an Execute.

UseSpell isnt interface function


Used it like this and still have a crash

UnrealEditor_MyProject2_patch_0!UMagicComponent::UseSpellWithCast() [C:\Users\user\Documents\Unreal Projects\MyProject2\Source\MyProject2\MagicComponent.cpp:61]
UnrealEditor_MyProject2_patch_0!UMagicComponent::UseSpell() [C:\Users\user\Documents\Unreal Projects\MyProject2\Source\MyProject2\MagicComponent.cpp:51]
UnrealEditor_MyProject2_patch_0!TBaseUObjectMethodDelegateInstance<0,AMainChar,void __cdecl(void),FDefaultDelegateUserPolicy>::Execute() [D:\egs\UE_5.3\Engine\Source\Runtime\Core\Public\Delegates\DelegateInstancesImpl.h:650]

here is interface function implementation in MainChar

UFUNCTION(BlueprintNativeEvent)
    void StartCasting();
    virtual void StartCasting_Implementation() override;

afbeelding

You will get more info on a crash. Probably the exact error.

If something crashes, post the full code and the full log. Format each file in a separate code block in the reply.

1 Like

Ok so I looked over the code again. The use of the interface is a bit backwards. The character should really by calling a magic interface passing in itself during the casts.

But here you went the other route where the magic system is direct but the caster internally is an interface. Not intuitive.

so this part can be called directly because the magic sys has no interface per say.


void AMainChar::SpellCast() {
	if (Magic == nullptr)return;
	if (Magic->GetNeedCast()) {
		UE_LOG(LogTemp, Display, TEXT("Need cast"));							
		Magic->UseSpellWithCast();					
	}
	else {		
		Magic->UseSpell();		
	}
}

But all calls to the owner inside of the magic interface have to have the execute command.

These calls are what you would call putting the cart before the horse.

did it how u told
Magic Code:

void UMagicComponent::UseSpell()
{
	if(!bCanDoMagic || !GetOwner()|| !CurrentSpell || !CurrentSpellClass || 
	!GetOwner()->GetClass()->ImplementsInterface(UINF_MainChar::StaticClass()) || !Cast<IINF_MainChar>(GetOwner()))  return;
	if(!CurrentSpell->bNeedCast){
	   ASpells_Base* SpawnedSpell=GetWorld()->SpawnActor<ASpells_Base>(CurrentSpellClass);
	   SpawnedSpell->SetOwner(GetOwner());
	   SpawnedSpell->Activate();
	}
}

void UMagicComponent::UseSpellWithCast()
{
	if(!bCanDoMagic || !GetOwner()|| !CurrentSpell || !CurrentSpellClass || 
	!GetOwner()->GetClass()->ImplementsInterface(UINF_MainChar::StaticClass()))  return;
	GetWorld()->GetTimerManager().SetTimer(MyTimerHandle, this, &UMagicComponent::Casting, 2.0f, false);
	if (CastWidget)
	{
		CastWidget->AddToViewport();
	}
	bCanDoMagic = false;
}

MainChar:

void AMainChar::SpellCast()
{
    if(Magic==nullptr) return;
    if (Magic->GetNeedCast()) {
		UE_LOG(LogTemp, Display, TEXT("Need cast"));
        //	StartCasting(); <---- WRONG DO NOT CALL THE INTERFACE FUNCTION DIRECTLY!
		if (GetClass()->ImplementsInterface(UINF_MainChar::StaticClass()))
		{
			IINF_MainChar::Execute_StartCasting(this);
		}	
        Magic2->UseSpellWithCast();			
	}
    else 
    {
     Magic->UseSpell();
    }
}

Still have a crash, but he changed


MagicComponent.cpp:58 is:

bCanDoMagic = false;

bool property cant be “Access violation”

Access violation 0x000000…(all zeroes) means you are trying to access a nullptr at line 58

1 Like

Magic2 not declared. Still not seeing the full code and crash info

Good catch on magic2, that might be the null. Though if it was not defined in the header than wouldn’t intellisense catch it? Compiler would throw an error too.

Still Crashes with no Magic2
Magic2 was Magic2=CreateDefaultSubobject(TEXT(“MyHope”));
Crash:


MainChar Code:

void AMainChar::SpellCast()
{
    if(Magic==nullptr) return;
    if (Magic->GetNeedCast()) {
		UE_LOG(LogTemp, Display, TEXT("Need cast"));
        //	StartCasting(); <---- WRONG DO NOT CALL THE INTERFACE FUNCTION DIRECTLY!
		if (GetClass()->ImplementsInterface(UINF_MainChar::StaticClass()))
		{
			IINF_MainChar::Execute_StartCasting(this);
		}	
        Magic->UseSpellWithCast();			
	}
    else 
    {
     Magic->UseSpell();
    }
}

MagicComponent code:

void UMagicComponent::UseSpell()
{
	if(!bCanDoMagic || !GetOwner()|| !CurrentSpell || !CurrentSpellClass || 
	!GetOwner()->GetClass()->ImplementsInterface(UINF_MainChar::StaticClass()) || !Cast<IINF_MainChar>(GetOwner()))  return;
	if(!CurrentSpell->bNeedCast){
	   ASpells_Base* SpawnedSpell=GetWorld()->SpawnActor<ASpells_Base>(CurrentSpellClass);
	   SpawnedSpell->SetOwner(GetOwner());
	   SpawnedSpell->Activate();
	}
}

void UMagicComponent::UseSpellWithCast()
{
	if(!bCanDoMagic || !GetOwner()|| !CurrentSpell || !CurrentSpellClass || 
	!GetOwner()->GetClass()->ImplementsInterface(UINF_MainChar::StaticClass()))  return;
	GetWorld()->GetTimerManager().SetTimer(MyTimerHandle, this, &UMagicComponent::Casting, 2.0f, false);
	if (CastWidget)
	{
		CastWidget->AddToViewport();
	}
	if(bCanDoMagic) // line 58
	bCanDoMagic = false;
}

how can if(bCanDoMagic) crash the game? bCanDoMagic is global property

Perhaps because it’s not marked as a UPROPERTY() it might get GC’d?

Global how? We’re not getting the full code.
It’s not what the crash log says, it falls apart when your widget is added to the screen. Try not creating / adding that widget.

1 Like