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"

Function pointers in Unreal compared to standard C++. Getting an unexpected error which I cannot reproduce without UE4

I tried to reproduce this issue in a non Unreal Engine project, but I can't seem to do it, so I'm quite unsure how to solve this, you can see how I tried to reproduce it here.

Anyhow, I'm trying to write a triggering gameplay system which works like this:

You have a trigger, can be anything, a volume trigger, a written button class which trigger something, a timed event or anything. That said trigger calls its trigger option class which only exists for the possibility of different usecases, some options at the top of my head: proxytrigger which instantly triggers, delayedtrigger which delays the trigger process, or something more complex like a trigger option that will only give you the OK sign if your health is 100%.

Once the TriggerOption tells the real Trigger that it's been triggered, the TriggerAction will play, which can also be anything, a door opening perhaps? I wonder how many times I've said "trigger" by now ... anyway, that's the entire system. However, the process of TriggerOption telling TriggerActor (the actual trigger) that it should get triggered happens through a function pointer:

This is the function which sets up the function pointer:

 void ATriggerActor::AttemptToTrigger()
 {
     if (!TriggerOption)
     {
         UE_LOG(LogTemp, Error, TEXT("TriggerOption is nullptr in TriggerActor: %s?"), *GetName());
         return;
     }
 
     if (CanBeTriggered())
     {
         TriggerOption->StartTriggerOption(this, &ATriggerActor::OnTriggerOptionCompletedCB);
         bHasBeenTriggered = true;
         TriggerCount++;
     }
 }

This is the function that calls it, or stores it or whatever this subclass wants to do with it, in this case, it just calls it.

 void UTriggerOptionProxy::StartTriggerOption(ATriggerActor* CallbackOwner, VoidFuncPtrNoParams CompletionCB)
 {
     (CallbackOwner->* (CompletionCB))();
 }

This is also the typdef for the function pointer btw: typedef void(ATriggerActor::*VoidFuncPtrNoParams)(void);

When trying to call the function pointer, even though the CallbackOwner and the CompletionCB is valid memory, it throws this:

Exception thrown at 0x00007FF8A0954197 (UE4Editor-UE4Systems-3138.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

EDIT: Trying to fix the code format...

Product Version: UE 4.21
Tags:
ue4error.png (79.4 kB)
more ▼

asked Jan 19 '19 at 09:07 PM in C++ Programming

avatar image

MLarsson98
1 1 1

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

1 answer: sort voted first

Hey,

Im not sure i understod fully what you want, but seems to me you try to overthink this a little bit :)

What about delegates, timers, events, interfaces? :) Technically all of this things is designed to trigger functions at some point and they implemented properly so you can use it freely...

I know this is not what you asked, but maybe a good advice ;) but if you wanna go with function pointers, maybe this wiki helps: https://wiki.unrealengine.com/Function_Pointers

https://docs.unrealengine.com/en-US/Programming/UnrealArchitecture/Delegates

https://wiki.unrealengine.com/Delegates_In_UE4,_Raw_Cpp_and_BP_Exposed

https://orfeasel.com/using-delegates/

https://docs.unrealengine.com/en-us/Programming/UnrealArchitecture/Delegates/Events

https://www.tomlooman.com/using-timers-in-ue4/

more ▼

answered Jan 22 '19 at 02:53 PM

avatar image

AmphDev
1.4k 15 9 33

(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