In Unreal Engine 4 I have two different classes (Player and Enemy) that spawn the same type of Actor (Shot). From within the newly created Shot, how can I find out which of those two classes spawned it?
I am sure there is a pretty straightforward answer to it, but I can't seem to phrase it properly when searching, as I am not getting any helpful hits.
Note: I have started digging into UE4 very recently, so it is quite possible that I have found the answer already but am just not sure what I am looking for. Or that I should simply have two different classes (Player Shot and Enemy Shot). Any help appreciated!
Edit: GetInstigator() gets me the values I expected but the description gives me the impression this is not necessarily what I am looking for: The APawn that is responsible for damage done by the spawned Actor. (Can be left as NULL).
If you're using BP, in the Shot class, create a variable (actor reference). When you spawn the actor, cast to the Shot reference, using the Return Value from the SpawnActor node as the object. After making this cast, SET the actor reference variable you created to the Self reference of what pawn spawned it.
Basically, what you are doing here is when the Shot is spawned, a reference to the pawn (player or enemy) is created within it. Hope this helps!
Related
I have two actors in my project, sometimes AI Pawn is causing a collision and sometimes a Player is causing it. Is there a way to distinguish it?
Have you checked the onHit function for the AI Pawn and Player?
Both in C++ and Blueprint, collision events give you a FHitResult (displayed as just HitResult in BP). Here is reference.
This result contains both the Actor and Component that was hit/overlapped/traced.
So, the object calling the collision event is first object, and YourFHitResult.Actor is the other object.
In BP, you can use the break hit result node to get the actor/component.
I have an issue I've been trying to fix for about a week now, but for the life of me cannot figure it out.
I have a function
DamageOnOverlap(AActor* HitActor, AActor* OtherActor
which deals damage on overlap. Within that function, I have the following function
UGameplayStatics::ApplyDamage(OtherActor, 25.0f, GetWorld()->GetFirstPlayerController(), this, UDamageType::StaticClass());
which deals damage to the overlapped actor. These two functions are in my
ZombieBasic.cpp
so then they overlap each other, they take damage and Destroy when their health is 0.
Is there a way to do a check or something so they're unable to cause damage to themselves?
I have done the following check
if(OtherActor == mainCharacter)
{
UGameplayStatics::ApplyDamage(OtherActor, 20.f, GetWorld()->GetFirstPlayerController(), this, UDamageType::StaticClass());
}
But either the they all still take damage or no damage is applied at all.
OtherActor will call AMainCharacter (AMainCharacter* mainCharacter is defined in the header file for ref).
So, I'm basically trying to stop ApplyDamage OR DamageOnOverlap from damaging other actors of the same type, which in this case, stop AZombieBasic from damaging another ABasicZombie.
Sorry if this is a rubbish explanation.
With some investigation, I have managed to fix the problem.
TL;DR
I added an Actor Tag to the mainCharacter class and did a check in ZombieBasic to see if an actor has the specified tag.
Read if you're interested
Adding an Actor tag to 'AMainCharacter' within the Blueprint Class under the Actor section and adding a new tag of Player. In ZombieBasic.cpp, in the DamageOnOverlap function I did a basic check of
if(OtherActor->ActorHasTag("Player")
{
// Deal Damage
}
Now when BP_BasicZombie overlap with themselves they no longer deal damage to themselves.
I'm new to unreal engine, I'm trying to add large force to an object with a box collider but after it collide with other object (just another instance) the overlap inside each others and become like one object and moving with each others.
Cab anyone explain their behavior and how i should resolve this?
What happens here is that both objects collide with each other continously. To fix that you could try to deactive the OnOverlap()-Event on either the overlapping Object or the colliding object.
In blueprints you can achieve that by setting the Generate Overlap Events-Variable of one of the colliding static meshes of the overlapping objects to false.
In C++ you could simply remove the dynamic event callback for one of the colliding objects like that:
CollidingComponent->OnComponentBeginOverlap.RemoveDynamic(this, &ACollidingActor::OnBeginOverlap);
Where CollidingComponent is the component of your object, which causes the overlap event to trigger.
Like #Alex said, they overlap with each other, over and over. If you didn't know, you can add breakpoints to your blueprint nodes, just like in your code, by right-clicking a node and select Enable Breakpoint (or smth like that). Your game will stop when reaching it and switch to that exact point in your blueprint. You can then hover over that node and see every variables value going in and out of it.
Hope this helps you learing to use the Unreal Engine.
What is the difference between those two methods? Why should i prefer one?
1)
GameObject.FindGameObjectWithTag("").GetComponent<Rocket>().active = true;
2)
GameObject.FindGameObjectWithTag("").GetComponent<Rocket>().SendMessage("setActive");
thanks!
Sending a message searches through all the components of the gameObject and invokes any function that has the same name as the message. Not a 100% sure but Im sure this uses reflection which is generally considered slow.
SetActive() or the active variable set the gameObject as active or not. If its not active it wont render in the scene and vice versa.
First of all it seems there are several inconsistencies with your code above:
1) Components (and MonoBehavior) don't have an active property (active belongs to GameObject), so the first line of code shouldn't compile. In addition the most recente version of unity don't have active anymore, it's substitued with activeSelf and activeInHierarchy.
And btw, both activeSelf and activeInHierarchy are read only, so you can't assing directly a value to them. For changing their value use SetActive method.
2)
The second line of code shouldn't work either (unless Unity does some magic behind the scenes) because SetActive methods belong to GameObject and not to your Rocket Component.
Now, I suppose your question was the difference between:
gameObject.SetActive(true);
and
gameObject.SendMessage("SetActive",true);
The result is the same, but in the second way Unity3D will use reflection to find the proper method to be called, if any. Reflection has an heavy impact on performance, so I suggest you to avoid it as much as possible.
I want to write GUI code that is orthogonal. Lets say I have a circle class and a square class and they need to interact. Right now, to get the circle and square talking to each other - say the circle object sends a message to the square object, I would use something like square_obj.listen_for_circle(circle_obj) where listen_for_circle is a method that implements an addlistener.
This is a problem for me since now the two objects are linked - and removing one object from my code would break it. What I am looking to do is for the circle_obj to be able to broadcast a global message say 'CIRCLE_EVENT'. Additionally square_obj would be listening for global message broadcasts of type 'CIRCLE_EVENT', and upon hearing the event - does some action.(Ahhh, now the objects have no links to each other in the code base!)
Is this possible or even reasonable in MATLAB? (or maybe i'm just going crazy).
As always, advice much appreciated.
I'm not really sure why addlistener is problematic for you. It basically just adds an event listener that doesn't do anything if the event-origin object (the circle) is deleted.
Alternately you can use event.listener or handle.listener. They are undocumented but work well, and are widely used within the Matlab codebase (m-files). See explanation here: http://UndocumentedMatlab.com/blog/continuous-slider-callback/#Event_Listener