Why does this cast always fail? UE4 - unreal-engine4

I have an ai spawner BP and inside I want to call an event by timer to increase the characters walk speed after 10s, however the cast always fails. I have tried casting to the enemy and also tried to make an enemy ref object variable but still no luck. Any idea why I cant get it?

GetPlayerCharacter returns the current "Player Character" Object; this is the object the player's PlayerController is controlling, not an spawned AI Zombie.
The cast fails because the object going in is not of the type you are trying to cast it to.
Casting doesn't "Get" you an object that you cast to. It can refine the code context for an item that is of that type already.
If you want to get a reference to the zombie to change its walk speed, you need to hold onto a reference of it after spawning to run it into this code.

Related

Animation event not detecting object

I having a little trouble with getting an animation event, to detect my LevelChanger gameObject, as an object. Once I do this, I can select the LoadScene() Function within my LevelChangerScript to load the next scene. However its not possible at the moment as the event wont let my drag and drop the gameObject, into it.
enter image description here
Maybe there is a bit of a misunderstanding how animation events work. You have to attach any script to the animator and that script has then to implement a function which you will specify in the 'Function' field in the event. This function should then be called automatically by the animator when playing the animation. The object is just an optional parameter like 'Float' or 'String'.
This might be a good source to read.

I'm Making Health Reduce Consecutively While Interacting With An Object, But It Only Reduces Once You Touch The Object

I'm trying to make health reduce while interacting with an object. I'm using Unreal Engine Blueprint. I want it so as soon as you interact with the object your health goes down, but once you stop interacting your health stops reducing.
Here's my blueprint:
I would do this in three parts:
On begin overlap, cast the overlapping object to the player pawn and if the cast succeeds, stash the player pawn in a member variable.
Run a repeating timer that deals damage every x seconds to the pawn stashed in the member variable if it's not null.
On end overlap, set the member variable to null.
This way if something other than the player comes in contact with the object, it's ignored, but when the player makes contact it starts repeatedly dealing damage until the player ends contact.
make private bool variable in the class.
set it to true when begin overlap.
set it to false when end overlap.
in Tick function reduce health when variable is true.
to be very simple...
fallow cpp way and make changes as in Tick function like the person above said ...
make private bool variable in the class. set it to true when begin
overlap. set it to false when end overlap.
in Tick function reduce health when variable is true.

Unity 2D. Null Refrence Exception problem

NullReferenceException: Object reference not set to an instance of an object
CombatBehavior.FixedUpdate () (at Assets/Scripts/CombatBehavior.cs:92)
NullReferenceException: Object reference not set to an instance of an object
CombatBehavior.FixedUpdate () (at Assets/Scripts/CombatBehavior.cs:94)
gameObject.GetComponent<DamageDeal>().DealDmg();
gameObject.GetComponent<DamageDealToSkeleton>().DealDmgToSkeleton();
Hello, im having troubles with NullRefrence Exception. Could anyone explain me what am i doing wrong. As a disclaimer i want to tell that two lines there were working but i wanted to improve some of my code, so i ended up ctrl + z-ing everything what did a mess. But unfortunetly its gone... Unity still telling me that there is Null value.
I was trying to solve it by setting something like this:
GameObject player = GameObject.Find("Player");
DamageDeal damageDeal = player.GetComponent<DamageDeal>();
damageDeal.DealDmg();
DamageDealToSkeleton damageDealToSkelet = player.GetComponent<DamageDealToSkeleton>();
damageDealToSkelet.DealDmgToSkeleton();
in my Game Object Player -> CombatBehaviour.cs -> FixedUpdate.
It's a matter of life and death, please.
Are you sure the scripts are still on the object after you did the ctrl+z deal?
Is everything correctly spelled? Case-sensitive.
With what you have presented i feel like this is the only thing we have to go by.
Triple check it all :)

Getting a sprite to issue a message when it is removed from the scene after removefromParent?

Is there some way in Swift that I can tell when an SKSpriteNode has actually been removed from the scene? I don't think it's actually done when removeFromParent is called, but instead I think it's done later, when Sprite-Kit thinks it's convenient to do so.
I'm trying to understand the full life cycle and I've noticed that a sprite can still be involved in contact and collisions in didBeginContact even after that sprite has been removed.
If I print out the contents of children (i.e. the array holding all the children of the scene) I see that the sprite is removed as soon as removeFromParent called, but the sprite is still available (at least, for this execution of the SK game loop).
Edit: This question came out from an earlier question of mine concerning didBeginContact being called multiple times for one contact (Sprite-Kit registering multiple collisions for single contact) and discovering that removing the sprite during the first contact did not prevent the subsequent contact(s). (Because SK has 'queued' to contacts in advance.) so I was wondering when the sprite is actually removed.
Am I missing the obvious? So even after removeFromParent the sprite is still around. However, it might well be because I have assigned the node to a temporary SKSpriteNode variable, then as long as that variable is around, there is a strong reference to the node, so it won't be deallocated. Also the SKPhysicsContact object itself will retain a reference to the physicsBody, which has a reference to the node which I think will also prevent allocation.
Update
To see when a sprite is actually released, use the deinit() method:
deinit {
print("Invader of type \(type) deinitialised")
}
I think this can only be added in a subclass definition, not via an extension.
Having a variable with a strong reference to the node being removed will prevent the node from being de-allocated until that variable is itself removed or changed to refer to something else.
If I've understand your question, I think it's not needed because you can always do:
if let myNode = self.childNode(withName: "//myNode") {
// ok myNode exist
}
Hope it helps you, you can write this code wherever you think is necessary.
Update:
About the reformulation of your question take a look below to these comments.
I have a suggestion after reading through the comments... move the node to a place outside of the playable area of your game, then remove it from parent. At this point you don't have to worry about when the physics body gets removed or when SK handles it. Or you could set the physicsBody? to nil at the same time, or use a bitmask flag as KoD suggested.
You can override all of the functions in the SK loop and check to see exactly when your pb is removed: https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Actions/Actions.html

Creating pointer Attributes in cocos2d iPhone

I am working on a game. There are balls that fall from the top of the screen, and the player has to catch them, as the are caught they stack ontop of one another. I have a method that creates each new ball and adds it to an Array that i use to move the sprites. Problem is that the after they collide I need them to stop moving, since the array is called on to move them they all move. And if i try to make them stop they all stop. So I was hoping to create a pointer attribute if ther is such a think, for example "sprite.position" I need a new attribute that i can check like a boolean. I was hoping to create a attribute like sprite.hasCollided and if it returns YES then the ball should no longer move. Is this possible or is there a better way to do it?
Thanks
Tanner
I would suggest you create a ball object. And add the boolean as as part of the object.
CCNodes (and, by inheritence, CCSprites) have a userData property, which is a void*. You can use this to relate a custom object to a cocos2d object. Keep in mind if you use the userData option, you will, in most cases, need to allocate any memory when you create/assign the sprite, and release it when you are done.
int* myInt = (int*)malloc(sizeof(int));
*myInt = 0;
sprite.userData = myInt;
//some time later, when you are done with the sprite
free(sprite.userData);
As an improvement on the userData property, you can do what xuanweng suggests and create a ball object containing various game-related properties for the balls, and assign an instance of this to each of your ball CCSprites using the method above.