Ok folks,
I found a way to dynamically spawn objects thanks to a tutorial.
First you have to create 2 c++ classes child of Actor class : ACubes_Controller and ACubes_Parent
you just have to write some code in Cubes_Controller.cpp Cubes_Controller.h :
Cubes_Controller.h :
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Actor.h"
#include "Cubes_Parent.h"
#include "Cubes_Controller.generated.h"
UCLASS()
class TETRIS2_API ACubes_Controller : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ACubes_Controller();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick(float DeltaSeconds) override;
float XCoord;
UPROPERTY(EditDefaultsOnly, Category = "Our Spawning object")
TSubclassOf<ACubes_Parent> SpawningObject;
};
Cubes_Controller.cpp :
// Fill out your copyright notice in the Description page of Project Settings.
#include "tetris2.h"
#include "Cubes_Controller.h"
// Sets default values
ACubes_Controller::ACubes_Controller()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void ACubes_Controller::BeginPlay()
{
Super::BeginPlay();
//UE_LOG(LogTemp, Warning, TEXT("test test_____________ test __________________test_____________test"));
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = Instigator;
ACubes_Parent* NewObject = GetWorld()->SpawnActor<ACubes_Parent>(SpawningObject, FVector(0.f, 0.f, 0.f), FRotator::ZeroRotator, SpawnParams);
}
// Called every frame
void ACubes_Controller::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
You compile in UE4, and it should work.
Then in the class viewer you create a couple of blueprints :
BP_Cubes_Parent from ACubes_Parent class
BP_Cubes_Controller from ACubes_Controller class
In BP_Cubes_Parent you add a satic mesh component, and drag and drop your mesh in the correct place (my mesh is a cube since my project is a Tetris)
In BP_Cubes_Controller you define SpawningObject as “BP_Cubes_Parent”
you drag and drop these 2 blueprints on the scene, you play the level and you can see that your mesh is dynamically duplicated (or spawned)