How to Properly Initialize & Register a Custom Editor Mode?

Dear Friends at Epic!

This is the Chronicle of my attempts to create an Editor Mode

I hope you find it entertaining at the very least :slight_smile:

( I dont know what I am doing cause all of this is undocumented :slight_smile: )

Please note I am at no point claiming my code should make sense, and if it doesnt please do correct me :slight_smile:

The only thing I can proclaim is that it compiles :slight_smile:

#The Summary

I’ve made a basic FEdMode and when I try to register it (not even run it, just register it), the Editor crashes without any useful callstack info.

I am probably missing many things I should be doing, cause all I am doing is assigning the ID for the EdMode before trying to register it.

A sub-question of this thread is, how do I properly make a TSharedRef?

Please see my code below for details

Thanks!

#The FVictoryEdAlignMode.h

#pragma once

#include "UnrealEd.h" 
#include "Editor.h"

class FVictoryEdAlignMode : public FEdMode
{
	
public:
	void JoyInit();

	
public:
	FVictoryEdAlignMode(); 
	~FVictoryEdAlignMode(); 

public:
	virtual void Enter() OVERRIDE;
};

#FVictoryEdAlignMode.cpp

// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.

//Victory Alignment Mode

#include "VictoryGame.h"


FVictoryEdAlignMode::FVictoryEdAlignMode(){}
FVictoryEdAlignMode::~FVictoryEdAlignMode(){}

void FVictoryEdAlignMode::JoyInit()
{
	ID = FName("VictoryEditorMode");
}

void FVictoryEdAlignMode::Enter()
{
	UE_LOG(Victory, Warning, TEXT("Entered Joy Mode!"));
}

Registering the Mode in UnrealEdEngine

I have extended UnrealEdEngine and I run this code when user clicks on an actor.

I have verified that this process works and the code is being run.

void UVictoryEdEngine::NoteSelectionChange()
{
	Super::NoteSelectionChange();
	//~~~~~~~~~~~~~~~~~

	if(!CreatedVictoryEdMode)
{
	//Proper way to make a shared ref ???
	FVictoryEdAlignMode NewMode = FVictoryEdAlignMode();
		
	//Create VictoryEdMode
	TSharedRef VictoryEdMode(&NewMode);
		
	//Init VictoryEdMode
	NewMode.JoyInit();
		
	//Register
	//GEditorModeTools().RegisterMode(VictoryEdMode);

	CreatedVictoryEdMode = true;
}

//Activate
//GEditorModeTools().ActivateMode(FName("VictoryEditorMode"));

}

#TSharedRef ?

What is the proper way to initialize a TSharedRef?

I dont have any .cpp files from any of the ue4 samples to refer to this, as they usually call a function that returns a sharedref.

#Module Startup?

Since I am not using a module to handle the FEdMode, is there anything that I should know about to make the editor mode register happily?

#Super?

Do I need to call any functions in my custom EdMode class like this?

void FVictoryEdAlignMode::Enter()
{
	FEdMode::Enter(); //Super?
}

#The Progress

This pictures shows that

-My extended UnrealEdEngine class is working

-The actor click event is occurring, in which the create editor mode code is running

-The custom editor mode does get created

-The custom editor mode does get initialized with its unique ID before being Registered

-I commented out the actual registration part to take this pic since that causes the crash

:slight_smile:

#Thanks!

Thanks in advance!

Rama

Dear Community,

I am very happy to report that Wraiyth (forum name) helped me to finally create my own editor mode!

I was just not making the shared pointer properly :slight_smile:

in my custom editor engine class:

if(!CreatedVictoryEdMode)
	{
		//Proper way to make a shared ref ???
		TSharedRef VictoryEdMode = MakeShareable( new FVictoryEdAlignMode() );
		
		//Init VictoryEdMode
		VictoryEdMode->JoyInit();
			
		//Register
		GEditorModeTools().RegisterMode(VictoryEdMode);
	
		CreatedVictoryEdMode = true;
	}
	
	//Activate
	GEditorModeTools().ActivateMode(FName("VictoryEditorMode"));

:slight_smile:

The key line I was not figuring out:

//Proper way to make a shared ref ???
    		TSharedRef VictoryEdMode = MakeShareable( new FVictoryEdAlignMode() );