3D Tetris game

Hi all,

I’d like to create a Tetris 3D game.

The basic pawn is a cube, everytime a new tetris shape is loaded at the top of the screen, i’d like an animation of cubes assembling together into one shape. I presume i can attach pawns to others.

d5a8772705e2a6aa19a36ad81cacc8a33dbea721.jpeg

I don’t want to create by hand every single cube on the sceen (100 cubes ?), i’d like to create one cube pawn and duplicate it 100 times or something, and then use dynamic materials because every tetris object (composed with cubes) has its how color.

For each tetris shape, i’d like to use TArray.

Is this the best way to structure such project ?

I created a cube actor on my scene, i added c++ component to him whose the class is named “CubesManager”.

How can i duplicate this cube on the scene in

** void UCubesManager::BeginPlay() { } **

please ?

I once made a tetris game in UE4, maybe not the same with your idea, but I wish it will be helpful for you.

video:

src:

Thank you so much fengkan

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) :smiley:

Now i’d like to store global variables (score, number of cube per row, number of cubes per column, limited time, and so forth) , so i decided to create a GameInstance blueprint.

How can i edit GameInstance blueprint in c++ please ? where is the associated file ?

You need to create the C++ class for the game instance and set it in the project settings.

Here is a blank example:

Header:


#pragma once

#include "MyGameInstance.generated.h"

UCLASS()
class UMyGameInstance : public UGameInstance
{
	GENERATED_BODY()
public:
	UMyGameInstance(const FObjectInitializer& ObjectInitializer);


	/** virtual function to allow custom GameInstances an opportunity to set up what it needs */
	virtual void Init();

	/** virtual function to allow custom GameInstances an opportunity to do cleanup when shutting down */
	virtual void Shutdown();

};

CPP:




#include "mymodule.h"
#include "MyGameInstance.h"

//---------------------------------------------------------------------------------------------------------------------
/**
*/
UMyGameInstance::UMyGameInstance(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
	
}

//---------------------------------------------------------------------------------------------------------------------
/**
*/
void UMyGameInstance::Init()
{
	Super::Init();
}

//---------------------------------------------------------------------------------------------------------------------
/**
*/
void UMyGameInstance::Shutdown()
{
	Super::Shutdown();
}

Thanks thanks

I think if you take a look in Unreal Match 3 example would be a great help. Sure I would start with that.