UE4 Dev 01 – Removing Items

The last two days I have been busy with refactoring and moving code from Blueprint to C++. Today I realised that I finally had to implement methods to remove items from my inventory component.

A while back I decided to implement a slot based inventory system that can contain stacks of items if they are set to be stackable. The inventory system is implemented as an ActorComponent and can thus be attached to every Actor in the game. I don’t know if this is the preferred way to implement such a system in Unreal, but I’m happy with how it works at the moment.

The functionality to remove items is of course essential and I need it to remove items once they are equipped by the player or consumed/used.

Yes, the ui isn’t yet updating properly 😉

There are two methods that can be used to remove items in the inventory at this time. One that removes a certain instance of an item and one that removes an specified amount of an item type.

bool UInventory::RemoveInstance(UItemInstance* Instance)
{
	FInventorySlot Slot;

	if (!FindSlotOfInstance(Instance, Slot))
		return false;

	this->Slots[Slot.SlotIndex].Free();

	TArray<int32> ChangedSlots;
	ChangedSlots.Add(Slot.SlotIndex);
	SlotsRefreshed.Broadcast(ChangedSlots);

	return true;
}

This methods as mentioned before takes care of freeing the entire slot that is occupied by the passed Item Instance. An Item Instance is a wrapper that I will be using to save per instance data for an item in the inventory (for example durability, generated stats etc.).

bool UInventory::RemoveItem(UItem* Item, int32 Count)
{
	....

	TArray<int32> ChangedSlots;

	FoundSlots.Sort([](const FInventorySlot& LHS, const FInventorySlot& RHS)
	{
		return LHS.GetStackSize() < RHS.GetStackSize();
	});

	int32 i = 0;
	int32 Index = FoundSlots[i].SlotIndex;
	bool HasBeenRemoved = false;
	
	while (Count > 0)
	{
		....
	}
		
	if (HasBeenRemoved)
	{
		SlotsRefreshed.Broadcast(ChangedSlots);
	}

	return HasBeenRemoved;
}

This method can be called whenever I want to remove not a specific instance but an amount of items. I omitted some of the code from the sample to shorten the code.

First of all I sort the list of slots I found that hold the passed UItem. I sort it by StackSize in order to start removing items from the lowest stack found. Afterwards I iterate over the Count till it reaches zero and I removed all items from the inventory. The SlotsRefreshed array contains all the indices that changed which will be broadcasted to subsystems (such as the UI).

IKL – Polishing & Runes

Today there isn’t alot of new things that got into Idle Knight Legends, but I did something that is equally as important – polish. What do I mean with polish? For me polish can be everything from adding new finished graphics assets, add effects / animations to convey better feedback to the player or adjusting the way how it feels to press a button, scroll a list and so on.

Giving the game that extra touch and statisfaction the player can make the difference how the game feels at the end. Polishing can be a bit boring from time to time since adjusting values over and over again till you found the sweet spot can be very mundane.

Today I received some more final graphics that added a bit more of eye candy to the existing user interface. Furthermore I had to restructure the rune window. This window shows all available runes the player collected showing I mostly revamped a window that displays all available runes to the player. To make it more statisfying to open the window I added an existing slide up animation when opening the window (I’m not yet statisfied and might revisit later). Since the style of the window changed a bit I had to resize some of the window elements.

As you can see there is still some polish todo for example translation strings to be added and some other placements, icons to add. These runes aren’t the final ones and thus are missing information about what they do.

Kurzmitteilung

Compiling HLSL Shader with premake5

I guess a lot of you that use premake often already know this, but since I find this to be handy I want to share it. A couple of days ago I needed to get shaders up and running in a very simple way. I started out by using the obligatory methods from the DirectX Api:

D3DCompileFromFile or the alternative D3DCompileToBlob. I still employ these methods however instead of compiling the file at runtime I decided to load pre-compiled shaders for now.

Since I don’t want to compile shaders manually each time I compile the project I looked for a solution that can be used with premake5. Everything I write I’ve found at https://stackoverflow.com/questions/55055150/premake5-how-to-build-hlsl-shaders. For an in depth look navigate to the link.

How does it work?

Navigate to your main premake5.lua script file and open it. Add the following lines of code to your project section.

shadermodel("5.0")

   shaderassembler("AssemblyCode")
   local shader_dir = "../Assets/Shader/"

   -- HLSL files that don't end with 'Extensions' will be ignored as they will be
   -- used as includes
   filter("files:**.hlsl")
     flags("ExcludeFromBuild")
     shaderobjectfileoutput(shader_dir.."%{file.basename}"..".cso")
     shaderassembleroutput(shader_dir.."%{file.basename}"..".asm")

   filter("files:**_ps.hlsl")
     removeflags("ExcludeFromBuild")
     shadertype("Pixel")

   filter("files:**_vs.hlsl")
      removeflags("ExcludeFromBuild")
      shadertype("Vertex")


   -- Warnings as errors
   shaderoptions({"/WX"})

The file tells premake which kind of shader is contained in each of the files. I think the keywords in here are very self-explanatory however with each filter you can specify how each shader file will be compiled. Shadertype defines what type of shader the file is containing, the shaderentry contains the name of the entry function of the shader.

Kurzmitteilung

Day 0 – Learning Game Dev

I have to admit that I lied in the headline. It’s not literally my first day of game development, of course. Actually it’s quite the contrary – I do this for alot of years now. However, there is still so much to learn for me thus it feels like day 0. This is the reason why I decided to go back to a passion project of mine and write about my journey in this blog.

A couple of years ago I started to work on my own game engine with C++ and DirectX / OpenGl. I guess that’s what a lot of people have done or still do. I never got that far since I always have been busy with work.

This is something I truly regret since I’ve a lot of interest in low-level engine design and C / C++ programming. I mean after all it’s a secret dream of mine to break into the AAA industry to work on some of these big projects. Eventhough, I have a lot of fun with what I do now, but I still have this itch to get back into game engine programming.

A while ago I finally decided to go back and pick up again where I left a year ago while reading some great articles about game engine architectures (I will post another blog post focusing on resources I use later). Since then I got DX12 up and running, learned about ImGui and got tinyobjloader integrated. However I realized that progress is really slow when using DX12 and decided to use OpenGL (for now) in order to be able to get to the advanced concepts of engine development faster instead of dealing with all the hassle that is DX12. I will most likely come back to DX12 later once I got more stuff implemented into the engine. After all the goal is to develop a small 3D game with it.

Besides working on the engine at some days I decided to dig a bit deeper into Unreal Engine 4. For the last couple of years I solely used Unity to develop games professionally or in private. At we work use Unity, too since it’s pretty straight forward and at least for smaller games quite convenient.

I stumbled over a very neat art package in the asset store of Unreal and my imagination ran wild. I decided to start a small prototype project and work on it in my spare time to make myself more familiar with Unreal and broaden my skill.

I will post regular updates of the two projects whenever I have something interesting to tell. I try to post something every day however this depends on how busy I’m with work. I might post articles about work related stuff from time to time, too.

This blog isn’t meant as some sort of tutorial series on how to write an engine or game but rather to write down my journey of me learning all the concepts and improving my skill.