Skip to content

Instantly share code, notes, and snippets.

View sephirot47's full-sized avatar

Victor Anton Dominguez sephirot47

View GitHub Profile
@sephirot47
sephirot47 / UE4 Get HUD from anywhere
Created June 17, 2015 20:51
UE4 Get HUD from anywhere
//Supose we are inside a Component ACTOR OF THE COMP.
AREMHUD *hud = Cast<AREMHUD>(UGameplayStatics::GetPlayerController( this->GetOwner(), 0)->GetHUD());
//Custom HUD class: AREMHUD
@sephirot47
sephirot47 / UE4 Get Camera Location or Position and Forward Vector .cpp
Last active December 26, 2023 17:51
UE4 Get Camera Location or Position and Forward Vector (useful for Tracing)
APlayerCameraManager *camManager = GetWorld()->GetFirstPlayerController()->PlayerCameraManager;
FVector camLocation = camManager->GetCameraLocation();
FVector camForward = camManager->GetCameraRotation().Vector();
@sephirot47
sephirot47 / UE4 Draw Debug Line .cpp
Last active August 21, 2023 07:05
UE4 Draw Debug Line
#include "DrawDebugHelpers.h"
DrawDebugLine(GetWorld(), traceStart, traceEnd, FColor::Green, true, 1.0f);
@sephirot47
sephirot47 / AlternativeDrawDebugFrustum.cpp
Last active August 3, 2023 22:01
UE4 C++ Alternative DrawDebugFrustum
#pragma once
#include "CoreMinimal.h"
#include "EngineUtils.h"
#include "DrawDebugHelpers.h"
inline void DrawDebugQuad(const UWorld* World, const FVector& BotLeft, const FVector& TopLeft, const FVector& TopRight, const FVector& BotRight, const FColor& Color = FColor::Green, const bool bPersistentLines = false, const float LifeTime = -1.0f, const uint8_t DepthPriority = 0, const float Thickness = 0.0f)
{
DrawDebugLine(World, BotLeft, TopLeft, Color, bPersistentLines, LifeTime, DepthPriority, Thickness);
DrawDebugLine(World, TopLeft, TopRight, Color, bPersistentLines, LifeTime, DepthPriority, Thickness);
@sephirot47
sephirot47 / UE4 Get ALL Actors in the level
Created June 16, 2015 14:10
UE4 Get ALL Actors in the level
//For every actor in this level...
for (TActorIterator<AActor> actor(GetWorld()); actor; ++actor)
{
//Do whatever you want with your actor
actor->BlahBlah();
}
@sephirot47
sephirot47 / ShootThroughCrosshair.cpp
Created February 25, 2021 10:13
UE4 C++ Shoot through crosshair (or any other screen point)
const APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
// Get viewport size
int ViewportSizeX, ViewportSizeY;
PlayerController->GetViewportSize(ViewportSizeX, ViewportSizeY);
// Deproject screen point
FVector ShotStart, ShotDirection;
PlayerController->DeprojectScreenPositionToWorld(ViewportSizeX * 0.5f, ViewportSizeY * 0.5f, ShotStart, ShotDirection); // Assuming crosshair is in screen center
ShotStart += ShotDirection * 100.0f; // Offset a bit so that shot starts from gun end
@sephirot47
sephirot47 / UE4 C++ Set Object Type or Set Collision Channel Type .cpp
Last active July 14, 2023 12:33
UE4 C++ Set Object Type or Set Collision Channel Type
/*
Ok, so you want to change your Object Type (or collision channel) to a custom channel in runtime.
But there's a problem, it seems UE4 doesn't support this yet, but there's a workaround hehe :)
For example, imagine you create the ObjectType Building.
Now, imagine you want to change the ObjectType of all the StaticMeshComponents of the object OBJECT (from WorldStatic to Building, for example).
You can try something like this:
*/
@sephirot47
sephirot47 / UE4 C++ Spawn Actor from Blueprint .cpp
Last active May 26, 2023 18:24
UE4 C++ Spawn Actor from Blueprint
// MyClass.h ==============================
UClass *mBlueprintClass = nullptr;
// ==========================================
// MyClass.cpp ==============================
MyClass::MyClass()
{
static ConstructorHelpers::FObjectFinder<UBlueprint> blueprint_finder(TEXT("Blueprint'/Game/Path/To/Asset/MyBlueprint.MyBlueprint'")); // This path can be obtained from the editor doing right click + "Copy Reference"
if (blueprint_finder)
@sephirot47
sephirot47 / gist:8e475b090d4314b6a628
Created June 30, 2015 14:32
UE4 Get Viewport / Screen Size and Center
/*
IMPORTANT!
In order to be able to use this functions, in the file "YourProject.h" change this:
#include "EngineMinimal.h"
For this:
#include "Engine.h"
@sephirot47
sephirot47 / UE4 Load Texture
Created June 17, 2015 20:54
UE4 Load Texture
//En el constructor (no el BeginPlay)
UTexture2D *texture;
static ConstructorHelpers::FObjectFinder<UTexture2D> Texture(TEXT("/Game/StarterContent/Textures/T_Spark_Core.T_Spark_Core"));
texture = Texture.Object;