Raycasting from player, hitting object and setting off custom event C++ UE4 - c++

I've been going in circles for a day now trying to get this going and I'm just getting nowhere. I'm trying to have multiple objects in the scene with individual animations and when I fire a raycast from my player and it hits one, have the UFunction(blueprintimplementableevent) go off on the object that it hits. Please help me this is absolutely stumping me.

What I would do is use a BlueprintNativeEvent. This will allow you to create an Implementation function of the stuff you want to happen in C++, as well as a blueprint implementation, which can be different for each blueprint of that object type.
It is quite simple to use as well, for example, in your character's header file:
/** Called when the player presses the fire key */
UFUNCTION(BlueprintNativeEvent, Category = "Shooting")
void OnHit();
virtual void OnHit_Implementation();
And now your Cpp:
void AMyGameCharacter::OnHit_Implementation()
{
UE_LOG(LogTemp, Warning, TEXT("OnHit_Implementation!"));
// Do whatever stuff you want to do in C++ here
}
Now over in your character / actor blueprint, just go to the event graph, right click, and add the event of type OnHit. Make sure that you make a callback to the parent OnHit event though (right click on the event and hit "Add Call to Parent Function")
If you want an example of this you can look at the C++ Battery Collector series or the docs.

Related

UE4 c++ and blueprint share input

Im trying to make a game and used a bit of c++ for a grabber and open door function where I bind in both of them the e key called Interaction to theyr respective usage, now im trying to make a dialog box and had to use some blueprint and linked the e button, the same as before to the show ui function but whenever I bind it and start the game my other two c++ bind don't won't work and when the dialog one is deactivated my two c++ one will work. So yeah I was wondering how could I use blueprint and c++ at the same time on the same keybind. I tried for the moment to create a new input on the same key and the [roblem persist and I cannot find any usefull information on the net.
In nutshell, APlayerController::PushInputComponent(UInputComponent InputComponent), this method will help register your Actor, Widget or anything you want to bind input event.
So it is OK that bind same key to multiple Actors' behavior, but you need concern that this KEY will be consumed in default, in your case, after construction of UInputComponent, while bind action, you may set InputComponent->BindAction(…).bConsumeInput = false.
This flag indicates that if delegate calling will consume this key, so lower priority input component is still eligible to trigger.

Using C++ w/ Lua in a runtime environment

I have looked this question up so many times that I am convinced I am missing a huge piece of the puzzle when it comes to integrating LUA into my C++ Game Engine. What I want to do is run my game engine, then while its running I would like to click on my ui and click "add script" and then run the script. That part is easy enough to do but what I DON'T get is how a script that seemingly gets ran in place with lua_dofile could have code that gets mouse input or moves the character based on input. I don't see anyway to do this effectively. Am I supposed to allow the LUA state to be created and destroyed every frame or do I make the script fire every frame? In an application like this:
void init();
void update();
void render();
void end();
How would I use LUA to control the movement of an entity.
Lets say I set up the lua state so that you can write lua code like this:
entity1 = Entity.new()
entity1:setPosition(4,5)
How would I give the script some input from mouse to move the entity to the mouse position?
My overarching question is what is the best way to have a script control my entities in a way that if I supply the Lua State the ability to move my entities, then a scripter could write a "game" (i.e. a interactive runtime application)
To give a concrete example, suppose you wanted to teleport a ball to the player whenever they click the mouse. Here's what you could write to do that in Lua:
local function clickHandler(event)
local x, y, z = event.player:getCoords()
local ball = event.world:getBall()
ball:teleportTo(x, y, z)
end
game.registerEventHandler('click', clickHandler)
To make that example actually work in your engine, here's what you'd need to do:
Create a registerEventHandler function that saves the given callback somewhere C can access it
Create a getCoords function that returns the coordinates of a given player
Create a getBall function that gets a ball in your gameworld
Create a teleportTo method that teleports an entity to a given location
Whenever a player clicks the mouse, run all click event handlers that you saved, with an event object containing the player and the gameworld
And as for this question in particular:
Am I supposed to allow the LUA state to be created and destroyed every frame or do I make the script fire every frame?
No. You create a lua_State when your game starts, load the scripts in it, and then just keep using that state to call the event handlers.
Essentially the Lua scripts define functions to be called at each event. So the host C++ loads the scripts and then enters its main loop and calling back Lua.
See how LÖVE does it.

What is the correct way to access a blueprint class component in C++

I recently switched from unity and I wanted to know if there was a “getComponent” equivalent in UE4? I have an enemy script and I created a blueprint from that script and have added a widget with a progress bar to show the enemies health during combat. I have seen a lot of examples of how to do this in blueprint but if possible, I would like to just do it in code and let the percentage be calculated when it is needed to be, like after damage or something, rather than binding via blueprints.
I have tried using the getcomponentbyclass method but that throws an error because the component I want is not a child of the actor class.
Any help appreciated
You need to call GetComponentByClass() on an Actor.
If your Enemy is a Component, you can call getOwner() to retrieve the actor owning this component.
You can then call GetComponentByClass() on this actor instance.

How to alternate between two scenes in cocos2d-x?

I'm creating a game using Cocos2d-x. I'm currently creating a gameover menu, in that menu I need to be able to switch to both my menuscene and my gamescene (When I say switch to gamescene I only really only mean "to restart" the game). But circular dependencies stop me from being able to do this.
MenuScene needs to be able to useGameScene::create() in order to switch to the gamescene and the gameover menu needs to be able to use both of GameScene::create() or its restart funtion and MenuScene::create() which is giving me circular dependency problems
I can't separate my gameover menu to it's own file as I still need the GameScene dependency and GameScene would need gameover.
I can't combine them as GameScene then needs to depend on MenuScene
So my question is: How do I alternate between two scenes in cocos2d-x c++.
I read somewhere about pushing and popnig scenes in Director, but I don't really understand how that works, or if I could use that for my purpose.
Thank you in advance!
EDIT:
Now that I think about it, could I not just push mMenuScene to Director before switching to GameScene? That should work if I understand that push/pop mechanic correctly.
I think you might have a misconception of how complex this is, using the way I provided below you can and should definitely split your game over scene into its own file.
The scene replace is easy enough, just use the code below:
Including your file:
#include "MainGameScene.h"
Creating and switching scenes in your onClickListener:
auto gameOverScene = GameOverScene::createScene();
// use code below for hard replace
Director::getInstance()->replaceScene(gameOverScene );
// or use code below for transition fade replace
Director::getInstance()->replaceScene(TransitionFade::create(1, gameOverScene , Color3B(255, 255, 255)));
As for the restart functionality. I usually provide a callback to my game over scene that I call when the restart button has been clicked. Not that I ever swap out my scene completely for a mobile game over scene, but I still do it the same way regardless. So lets do steps (This assumes you seperated your game over scene into it's own file named GameOverScene :) ).
Store a function pointer in your GameOverScene.h to your reset method in MainGameScene:
std::function<void()> _resetCallback;
Set your function pointer from the main game scene, before running with the GameOverScene.
auto gameOverScene = GameOverScene::createGameOverScene();
gameOverScene->setResetCallback(std::bind(&MainGameScene::reset, this));
When your reset button is clicked, call the _resetCallback
void GameOverScene::onResetClicked(Ref* sender)
{
_resetCallback();
}
This should provide you with all the functionality you need to set up a what you want as well as remove the circular dependency that you have. I have used this way many times before and it always works. Let me know if this solution works for you.

Cocos2D: Run a parent update method with custom interval

I´m trying to put a together a small level system for my game. I want a update method that takes care of my game main logic. So I declared a method in my parent class .h file:
Level.h:
-(void)gameLogicTick:(ccTime)dt;
Then I do the implementation in Level.m, don´t think the exact code is relevant.
I created a subclass of Level called Forest. In the Forest init-method I would like to specify the interval of gameLogicTick, I did this:
[self schedule:#selector(gameLogicTick:)interval:5.0f];
This makes my game crash(after 5 seconds) with no debug message what so ever.
So how can I create a schedule update-method where I can specify the interval in the current class?
What you do is ok. Perhaps the Forest object got deallocated (ie because it was never added as child to a parent node for example) and the next time CCScheduler updates the scheduled methods, it will try to run your method on an already released instance.
You should enable the global exception breakpoint in Xcode to get more crash info and the exact line where the crash occurs.
Don't try messing with the parent class gameLogicTick: method. Leave that one to do its thing at full speed. In your Forest.m, simply call
[self schedule:#selector(update:) interval: 5.0f];
then make sure you have the appropriate update:(ccTime) delta in your Forest implementation. Just don't name it the same as your parent class's method and you won't have any problems.