Best way to compare actors in C++ ?
--- Relevant code in the Character class ---
Please note that the raycast() method is private, hence it acts as a mutator (setter) and returnRaycastActor() acts as an accessor (getter)
--- Relevant code in Interactable class ---
If possible, can someone suggest a more appropriate way to compare the actor being hit in the raycast and the interactable objects. Not too familiar with the library definitions, methods and functions, so it would be helpful if someone more knowledgeable/someone who's been through a similar experience and advise me on what to change. Specifically, the third nested if statement in the "interactImplementation()" method does not work at all, as you'd expect due to it being a strange comparison. The desired behavior was to fetch the type of actor that the object hit in the raycast was and compare it to the interactable actor, which we can then use to identify that the player is directly observing the interactable object. This is entirely my fault, I'm too lazy to sift through tonnes of documentation to find what I need, so I resort to asking others (since I'm relatively new to UE4, and C++ (But I come from Java and Objective-C)). In addition to this, in the initial Character class, I'm using the component attached to the root to cast the ray from. I didn't really want to do this, instead I wanted to cast from the camera directly, but that was yielding strange results (The source/origin of the cast was different to the position of the camera, hence the vector differed from the expected vector, but the final location was the same). The last thing I think I should mention is that the debug message only shows "StaticMeshComponent0" as the name of the collided mesh, regardless of where you look (Which, btw, is very limited since it's generated from the character model, thus lacks the ability to freely rotate in the x-axis).
Thanks in advance for any help offered! (Senpai, save me from this hell)
asked Jun 25 '18 at 03:43 PM in C++ Programming
If I misunderstood I apologize, but the comparison of actors is the comparison of the pointer to the actor.
(if you where to view the value of Ptr it would be a memory-address, such as "0x00F34C", this is simply a number representing where the actor is stored in memory).
the this pointer of a actor class is also a pointer, as is any other pointer to an actor.
Note that regardless of type, a pointer is a unique address in memory, that is to say, two different instances of actor objects will have two totally different pointers. Further, the pointer's address is not changed by the inheritance of an object, for example:
By this simple logic, I can easily make a comparison between objects with definite certainty!
It is worth noting two small things here. This is not an Unreal mechanism, this is pure C++ and any tutorial on C++ inheritance and pointers will provide you the knowledge you seek.
Secondly, there is absolutely no gaurentee that if an actor is destroyed the memory will not be reused. For example, if I stored the value of any Ptr variable above, then during my game that object was destroyed, the memory address might be used for another actor, or something totally different.
An Instance of an Actor is an object, unless you implement something (such as a UUID), the only way to compare the objects would be to compare every property of the Actor. The Pointer to the Actor however is not the Actor, it is a unique memory-address of where the actor is stored, Compare pointers to get the result you seek. Many of my students have understood this better if I simply use the analogy of a postal address. There might be many people with your name in the world, but only you live at your home-address, by the same logic there may be many AActors in the game, but only one exists at a specific memory address.
If you wish to discover if an object is of a specific type (such as a certain class, you can use IsA, eg: Ptr1->IsA(AMyPlayerCharacter).
Also, upon re-reading your question I noticed your issue with firing the raytrace from the camera. I would suggest attaching something to the camera, just in-front of it (such as a USceneComponent), then firing the ray-trace from that.
Hopefully that will help you kōhai!
Follow this question
Once you sign in you will be able to subscribe for any updates here