Spawning Actor from array of classes UE5 - c++

I'm attempting to use SpawnActor() to spawn random actors from an array of classes. I've spent several years using UE4/UE5 blueprints, but this is my first time working in cpp in a long time. The trouble I'm running into is with the SpawnActor function, maybe I'm hooking up the parameters wrong.
Below is the code for my spawning logic in my cpp file:
void ADS_Generator::SpawnFlora()
{
FHitResult CurrentHit;
FActorSpawnParameters SpawnInfo;
for (int F = 0; F < FloraIterations; ++F)
{
if (SpawnedFlora.Num() <= MaxFlora)
{
CurrentHit = SpawnLineTrace(Vertices[FMath::RandRange(0, Vertices.Num() - 1)]);
if (CurrentHit.bBlockingHit == true)
{
if (FMath::RandRange(1, 100) >= FloraTolerance)
{
AActor* CurrentFlora = GetWorld()->SpawnActor(Flora[0], CurrentHit.ImpactPoint, FVector(0, 0, FMath::RandRange(-180, 180)), SpawnInfo);
SpawnedFlora.Add(CurrentFlora);
}
}
}
}
}
Below is the code for my header file where I declare the array of actor classes:
UPROPERTY(EditAnywhere)
TArray<TSubclassOf<class AActor>> Flora;
UPROPERTY(EditAnywhere)
TArray<TSubclassOf<class AActor>> Fauna;
The specific error I'm getting is this:
"C++ no instance of overloaded function matches the argument list
argument types are: (TSubclassOf<AActor>, FVector_NetQuantize, FVector, FActorSpawnParameters)
object type is: UWorld"
I've tried adding "::StaticClass()" as well as using "->StaticClass" to no avail.
Any thoughts or advice? I definitely need to watch some videos and brush up on my cpp, I'm very rusty.
Note: I'm using Flora[0] as a placeholder instead of randomizing the array index just to test.

Found the issue! The problem was my Rotation was defined as a plain FVector, but it needed to be converted to a rotation and then a quaternion, respectively. Below is the code, but fixed:
void ADS_Generator::SpawnFlora()
{
FHitResult CurrentHit;
FActorSpawnParameters SpawnInfo;
for (int F = 0; F < FloraIterations; ++F)
{
if (SpawnedFlora.Num() <= MaxFlora)
{
CurrentHit = SpawnLineTrace(Vertices[FMath::RandRange(0, Vertices.Num() - 1)]);
if (CurrentHit.bBlockingHit == true)
{
UE_LOG(LogTemp, Warning, TEXT("HIT!"));
if (FMath::RandRange(1, 100) >= FloraTolerance)
{
FTransform SpawnLoc;
SpawnLoc.SetLocation(CurrentHit.ImpactPoint);
SpawnLoc.SetRotation((FVector(0, 0, FMath::RandRange(-180, 180)).Rotation().Quaternion()));
SpawnLoc.SetScale3D(FVector(1, 1, 1));
AActor* CurrentFlora = GetWorld()->SpawnActor<AActor>(Flora[0]->StaticClass(), SpawnLoc, SpawnInfo);
SpawnedFlora.Add(CurrentFlora);
UE_LOG(LogTemp, Warning, TEXT("Spawned Flora!"));
}
}
else if(CurrentHit.bBlockingHit == false)
{
UE_LOG(LogTemp, Warning, TEXT("NO HIT"));
}
}
}
}
TLDR; Locations and Rotations are stored differently and I totally forgot.

Related

How To Pass Functions To Class In C++?

So I have been working on an Arduino program that sends MIDI data over USB, and I can't seem to figure out how to pass the function that handles sending of midi data to my main class. It worked at some point but for some reason, it doesn't, and I feel like I have tried every combination of things.
Here is my main file, simplified down to just output the MIDI data over serial:
#include "XiaoPiezoDrum.h"
void sendNoteOn(int note, int velocity, int chan) {
Serial.printf("WORKS! %d %d %d\n", note, velocity, chan);
};
XiaoPiezoDrum drum(9, 2, 3, 4);
void setup() {
drum.setNoteComFunctions(sendNoteOn);
}
Here is what that setNoteComFunctions method looks like:
void XiaoPiezoDrum::setNoteComFunctions(const std::function<void(int, int, int)>& onFunc) {
sendNote = [onFunc](auto && PH1, auto && PH2, auto && PH3) { onFunc(PH1, PH2, PH3); };
}
And here is the relevant parts of the main class definition:
class XiaoPiezoDrum {
public:
XiaoPiezoDrum();
XiaoPiezoDrum(int piezoPin, int rPin, int gPin, int bPin);
void setNoteComFunctions(const function<void(int, int, int)>& onFunc);
void RunCycle();
private:
std::function<void(int, int, int)> sendNote;
}
So everything runs just fine until I call sendNote from within RunCycle, which is just called every loop, and then serial communication just stops suddenly. What am I doing wrong? I also tried using bind instead of a lambda, and the same thing happens. When I just copy the same print statement that is in sendNoteOn in the main file into the lambda in setNoteComFunctions, it prints just fine, so I know the issue is all about linking the function from the main file.
Things I have tried:
passing the function in setNoteComFunctions both as a reference and not
using bind rather than lambda
changing the body of sendNoteOn
making the lambda arguments references
making the lambda arguments not references
returning onFunc in the lambda
I have been banging my head against the keyboard for hours with this. If anybody knows what I'm doing wrong, I'd love an explanation!
EDIT
Here is the full code of RunCycle. Many of these variables are instance variables that I left out of the provided class definition, but I know the issue is with the call of sendNote, because if I comment it out, things continue running normally. I have also tried replacing the velocity variable with a fixed integer, and the same thing happened.
void XiaoPiezoDrum::RunCycle() {
double val = sensor.read();
val = (val > 0) ? val : 0;
Serial.println(val);
unsigned long timeElapsed;
int velocity;
MaxVal = (val > MaxVal) ? val : MaxVal;
trigger = val > THRESHOLD && !triggering;
if (val > THRESHOLD && !triggering) trigger = true;
if (trigger) {
triggerTime = millis();
trigger = false;
triggering = true;
triggerBuffer.clear();
}
if (triggering) {
timeElapsed = millis() - triggerTime;
if (timeElapsed < SAMPLE_TIME) {
loopCounter++;
triggerBuffer.addValue(val);
Serial.println(val);
}
else {
velocity = map(round(triggerBuffer.getMax()), 0, 300, THRESHOLD, 127);
Serial.printf("Velocity: %d\n", velocity);
if (comFunctionsSet) sendNote(40, velocity, 1);
noteEndTime = millis();
triggerTime = 0;
triggerBuffer.clear();
triggering = false;
resting = true;
}
}
}
jignatius' answer did it! I should have known I could do that!! Thanks!
I just had to replace
void XiaoPiezoDrum::setNoteComFunctions(const std::function<void(int, int, int)>& onFunc) {
sendNote = [onFunc](auto && PH1, auto && PH2, auto && PH3) { onFunc(PH1, PH2, PH3); };
}
with:
void XiaoPiezoDrum::setNoteComFunctions(const std::function<void(int, int, int)>& onFunc) {
sendNote = onFunc;
}
So glad to have that solved! Thanks!

Unreal engine c++ write delegate OnScreenshotCaptured

I am new to C++. I've wrote code in C# and PHP.Since I am using Unreal engine I am trying to learn C++. For my project I need to make a screenshot in-game and show it immediately so I want to get it as a texture.
I made a blueprint node which calls this function i've made:
void UMyBlueprintFunctionLibrary::TakeScreenshot()
{
FScreenshotRequest::RequestScreenshot(true);
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Tried to take screenshot");
}
When I hover my mouse above RequestScreenshot I see the following pop-up:
"Screenshot can be read from memory by subscribing to the viewsport OnScreenshopCaptured delegate"
So that is what I try to do but I have no idea how I looked up this:
https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Engine/UGameViewportClient/OnScreenshotCaptured/
Can someone tell me how to implement this and how you see/know how to implement it?
I have an alternative, no delegate, but FRenderTarget::ReadPixel() to some buffer you allocated, by implementing your own UGameViewportClient (inherit it), and overriding Draw() function.
I'll show the essential codes, but not complete.
void UMyGameViewportClient::Draw(FViewport* Viewport, FCanvas* SceneCanvas)
{
Super::Draw(Viewport, SceneCanvas);
if (any_condition_you_need) {
CaptureFrame();
}
}
void UMyGameViewportClient::CaptureFrame()
{
if (!Viewport) {
return;
}
if (ViewportSize.X == 0 || ViewportSize.Y == 0) {
return;
}
ColorBuffer.Empty(); // Declare this in header as TArray<FColor>
if (!Viewport->ReadPixels(ColorBuffer, FReadSurfaceDataFlags(),
FIntRect(0, 0, ViewportSize.X, ViewportSize.Y)))
{
return;
}
SaveThumbnailImage();
}
void UMyGameViewportClient::SaveThumbnailImage()
{
IImageWrapperModule& wrappermodule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
auto wrapper_ptr = wrappermodule.CreateImageWrapper(EImageFormat::PNG);
for (int i = 0; i < ColorBuffer.Num(); i++)
{
auto ptr = &ColorBuffer[i];
auto r = ptr->R;
auto b = ptr->B;
ptr->R = b;
ptr->B = r;
ptr->A = 255;
} // not necessary, if you like bgra, just change the following function argument to ERGBFormat::BGRA
wrapper_ptr->SetRaw(&ColorBuffer[0], ColorBuffer.Num() * 4,
ViewportSize.X, ViewportSize.Y, ERGBFormat::RGBA, 8);
FFileHelper::SaveArrayToFile(wrapper_ptr->GetCompressed(), *ThumbnailFile);
}

Variable inside lambda function is showing garbage value

I am using this method in a Cocos2d X game.
void OpponentNode::discard(int cardNum)
{
log("\nOpponentNode::discard <%d>\n", cardNum);
for (int i = 0; i < vecOpponentHand.size(); i++)
{
if (vecOpponentHand.at(i) == cardNum)
{
vecOpponentHand.erase(vecOpponentHand.begin() + i);
break;
}
}
CardSprite * discardedCard;
for (int i = 0; i < vecOpponentCards.size(); i++)
{
if (vecOpponentCards.at(i)->getTag() == cardNum)
{
discardedCard = vecOpponentCards.at(i);
vecOpponentCards.erase(vecOpponentCards.begin() + i);
break;
}
}
log("\nOpponentNode::discard <%d>\n", cardNum);
discardedCard->makeFaceUp();
RotateTo * rotate = RotateTo::create(0.4 * SPEED_MULTIPLIER, 0);
MoveTo * move = MoveTo::create(0.4 * SPEED_MULTIPLIER,
origin + Vec2(visibleSize.width * 0.75, visibleSize.height * 0.6));
Spawn * spawn = Spawn::create(rotate, move, NULL);
CallFunc * callFunc = CallFunc::create(
[&]()
{
log("\nOpponentNode::discard <%d>\n", cardNum); //this one shows garbage/different value
if (delegate)
{
delegate->opponentNodeDidFinishDiscard(this, cardNum);
}
this->removeChild(discardedCard);
});
discardedCard->runAction(Sequence::create(spawn, callFunc, NULL));
log("\nOpponentNode::discard <%d>\n", cardNum);
}
Strangely, when I log the integer cardNum like above, I get different value from the log inside the lambda function. For example, I get "OpponentNode::discard <402>" from the top 2 logs and the bottom most log but get "OpponentNode::discard <64>" from the log inside the lambda function.
Other points:
The lambda block is executed last.
I mostly get values like 64 or garbage values like -15493456.
My guess is the integer cardNum is getting deallocated before the execution. Can anyone point me to the right direction?
You're capturing a reference to the cardNum parameter. I would think you want to capture that one by value.
It's not clear to me what delegate is. Assuming it's a class member then I think you just need [this, discardedCard, cardNum]. Which you could abbreviate to just [=], although I think the explicit one is clearer.

Callback function confuses argument?

I have a sfml window container, and it appears to be working, however the glViewPorts are the wrong size, which I assume is because the wrong sf::Window is being passed.
Here is a function which adds to the window: It takes some information about the sfml window.
int WindowContainer::PushBack(WindowData& data)
{
if(data.WindowSettingsOK() && data.VideoModeOK()){
mWindowVector.resize(mWindowVector.size() + 1);
mDisplayFuncVector.resize(mWindowVector.size());
mInputFuncVector.resize(mWindowVector.size());
mWindowVector.at(mWindowVector.size() - 1) = new sf::Window();
mWindowVector.at(mWindowVector.size() - 1)->Create(data.VideoMode(), data.Title(), data.Style(), data.Settings());
mWindowVector.at(mWindowVector.size() - 1)->SetPosition(data.PositionX(), data.PositionY());
mDisplayFuncVector.at(mWindowVector.size() - 1) = nullptr;
mInputFuncVector.at(mWindowVector.size() - 1) = nullptr;
return 0;
}
else{
PrintError(ErrorMessageType::BadSettings);
return 1;
}
}
Alternatively, this function may be called to setup the display and input function callbacks:
int WindowContainer::PushBack(WindowData& data, function_p displayFunc, function_p inputFunc)
{
int return_val = PushBack(data);
mDisplayFuncVector.at(mWindowVector.size() - 1) = displayFunc;
mInputFuncVector.at(mWindowVector.size() - 1) = inputFunc;
return return_val;
}
Then, when the window needs .Display()'ing, this function is called:
void WindowContainer::ProcessDisplay()
{
for(unsigned int i = 0; i < mWindowVector.size(); i ++){
if(mDisplayFuncVector.at(i) != nullptr){
mDisplayFuncVector.at(i)(*mWindowVector.at(i), mClock, (const void*&)mExternalDrawingDataPointer);
}
mWindowVector.at(i)->Display();
}
}
... This is all good, until the result on the screen is that resizing one window affects the viewport of both windows. This suggests that calling the callback function: mDisplayFuncVector.at(i)(*mWindowVector.at(i), mClock, (const void*&)mExternalDrawingDataPointer); gives the argument of *mWindowVector.at(0) each time, instead of each window individually. (As in *mWindowVector.at(i))
Can anyone help with this problem?
The main loop contains this code:
while(container.Access(0)->IsOpened()){
container.ProcessInput();
container.ProcessDisplay();
}
Container.Access(int) is this function:
const sf::Window*& WindowContainer::Access(unsigned int index)
{
if(index > mWindowVector.size()){
PrintError(ErrorMessageType::IndexOutOfRange);
}
else{
return (const sf::Window*&)mWindowVector.at(index);
}
return (const sf::Window*&)mWindowVector.at(0);
}
Thanks again, I'm sure I have made a mistake somewhere but cannot spot it.
I have been thinking about this question and suspect openGL becomes confused with which window is it drawing to if more than one object is pushed back without a call to Display() to sync everything.
I am yet to test this and confirm.
EDIT The window container now works. It has nothing to do with the callback functions argument.

Error in Arduino programming

I am receiving the following error:
error: expected constructor, destructor, or type conversion before ‘(’ token
Here is the source code that I have written:
void setup() {
pinMode(1,OUTPUT);
[...]
pinMode(13,INPUT);
}
int i = 1;
bool pushed = digitalRead(13);
bool val = 0;
randomSeed(analogRead(0));
void loop() {
if (pushed == 1) {
for (i = 1; i < 9; i++) {
val = random(2);
digitalWrite(i,val);
}
}
}
The variables and the setup are OK; the error is located on the for line. Can anyone tell me how to fix this?
(edit : added the begining of the script, and sorry for the presentation (first question here)
(edit : looks like the error is not in the "i" definition. I'm using an Arduino UNO SMD Edition, if that helps (and the arduino alpha 0022 linux version of the IDE) )
EDIT: okay guys, solved now. It appears that my version of Arduino IDE was not completely downloaded, and that I put the randomSeed in the wrong place (it should be in the setup function.) (when i did put it in the setup function before updating, it shown an error message, saying /opt/arduino/lib/math.h was missing something (or something like that, i don't have the full message) ). Thanks for your help and i hope i'll be able to help you in arduino soon!
for (int i = 1; i < 9; i++)
is valid in C99/C11 but not valid in C89.
If you use a C89 compiler you have to define i outside the for loop clauses:
int i;
for (i = 1; i < 9; i++)
Also in C89, all declarations have to follow the left brace of a block, you cannot freely mix declarations and statements.
You appear to have a statement randomSeed(analogRead(0)); floating in between your setup() and loop() function definitions.
Move it and any other IO operations to the end of the setup() function so you read after setting up the pin directions:
int i = 1;
bool pushed;
bool val = 0;
void setup() {
pinMode(1,OUTPUT);
[...]
pinMode(13,INPUT);
pushed = digitalRead(13);
randomSeed(analogRead(0));
}
void loop() {
if (pushed == 1) {
for (i = 1; i < 9; i++) {
val = random(2);
digitalWrite(i,val);
}
}
}
That will reading the value of pin 13 into pushed only once ( e.g. you are holding a button when powering it on ); depending what you want it to do you may want to move the read to the start of loop() so writes random values whenever the button is pressed.
In C (previous to C99), it's not permissible to define a new variable in the first expression of a for loop. Try declaring your variable i at the top of the function instead.
#define pinMode1 1
#define pinMode2 13
bool pushed;
bool val = 0;
void setup() {
// Declare OUTPUT pin.
pinMode(pinMode1, OUTPUT);
// Declare INPUT pin.
pinMode(pinMode2, INPUT);
// Set digitalRead().
pushed = digitalRead(pinMode2);
// Initializes the pseudo-random number generator.
randomSeed(analogRead(0));
}
void loop() {
if (pushed == 1) {
for (int i = 1; i < 9; i++) {
val = random(2);
// Set i to HIGH or LOW.
digitalWrite(i, val);
}
}
}