<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[Shooter Game Tutorial]]></provider_name><provider_url><![CDATA[http://shootertutorial.com]]></provider_url><author_name><![CDATA[andrzejkoloska]]></author_name><author_url><![CDATA[https://shootertutorial.com/author/andrzejkoloska/]]></author_url><title><![CDATA[How to create&nbsp;Inventory]]></title><type><![CDATA[link]]></type><html><![CDATA[<p><a href="http://shootertutorial.com/2015/06/06/how-to-create-inventory/"><img class="alignnone" src="https://i0.wp.com/giant.gfycat.com/TangibleShyAlpineroadguidetigerbeetle.gif" alt="" width="640" height="360" /></a></p>
<p>Before start you should watch <a href="https://www.youtube.com/watch?v=0IO1zMVgG80&amp;list=PLZlv_N0_O1gZo6zXTHGGSH8gxaA7a_zCt&amp;index=2" target="_blank">this tutorial</a> from Epic. Creating Inventory isn&#8217;t so hard but it requires Arrays and Structs to work with.</p>
<p>What I would like to accomplish:</p>
<ul>
<li>Player will have his backpack which will be global inventory with all of the weapons collected,</li>
<li>Player can choose which weapon he would like to use in mission,</li>
<li>Inventory should be generic. I don&#8217;t know yet which weapons I will use and which stats they will have,</li>
<li>Game should remember which weapons I&#8217;ve chosen so I don&#8217;t need to choose them again,</li>
</ul>
<p><!--more--></p>
<pre><strong>This Tutorial has been created using Unreal Engine 4.8.2</strong>. 
Make sure you are working on the same version of the engine.</pre>
<p>We should start by creating new Structure &#8211; <strong>WeaponBackpackItem</strong>. It should contain necessary information about our weapon:</p>
<ul>
<li><a href="https://shootertutorial.files.wordpress.com/2015/06/weaponbackpackitemstruc.jpg" target="_blank"><img class="alignleft wp-image-227 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/weaponbackpackitemstruc.jpg?w=300&#038;h=191" alt="weaponbackpackitemstruc" width="300" height="191" /></a><strong>WeaponToSpawn</strong> (Actor) It will be reference to actor that should be spawned in Menu or begin play during gameplay,</li>
<li><strong>BackpackImage</strong> (Texture 2D) texture reference which we will use in UMG inventory,</li>
<li><strong>IsSelected</strong> (bool) This will tell us if this weapon was selected in inventory screen,</li>
<li><strong>InSlot </strong>(int) in which slot weapon was selected,</li>
</ul>
<p>Basically you can add otherdata here like Damage, Price or whatever you like. I&#8217;m not doing this right now because I think I will have menu in 3D and detailed data will be directly in WeaponToSpawn Actor.</p>
<blockquote><p><strong>USEFUL TIP</strong>: When working on structures using blueprints you need to be careful. During this tutorial my editor crashed 3 times when adding new variables to structure or updating arrays which are using structure. There are some known issues with structures that are placed in arrays. Be sure you save your work often! Basically I recommend putting structs and all operations on them to C++.</p></blockquote>
<hr />
<p>Now go to <strong>ShooterGameInstance</strong></p>
<ul>
<li>Create new variable from WeaponBackpackItem and name it Backpack_Weapons. It should be array:<br />
<img class="alignnone wp-image-228 size-full" src="https://shootertutorial.files.wordpress.com/2015/06/makearrayvariable.jpg?w=474&#038;h=114" alt="makearrayvariable" width="474" height="114" /><br />
Now let&#8217;s fill our array. Create couple of elements (I&#8217;ve created 5) and assign different textures. I&#8217;m using icons placeholders <a href="http://klakhan.deviantart.com/art/Borg-green-102687457" target="_blank">from here</a>. Just for test. Make sure you have different textures assigned to each elements.</li>
<li>Create new function: <strong>CanAddWeaponToWeaponSelected<br />
</strong>&#8211; it should have one local variable: HowManySelectedItems.<br />
<a href="https://shootertutorial.files.wordpress.com/2015/06/localvariable.jpg"><img class="alignnone wp-image-230 size-full" src="https://shootertutorial.files.wordpress.com/2015/06/localvariable.jpg?w=283&#038;h=45" alt="localvariable" width="283" height="45" /><br />
</a>&#8211; And it should return CanAdd (bool), HowManyItemsSelected (int)<br />
<img class="alignnone wp-image-231 size-full" src="https://shootertutorial.files.wordpress.com/2015/06/canweaddoutputs.jpg?w=325&#038;h=104" alt="canweaddoutputs" width="325" height="104" /><a href="https://shootertutorial.files.wordpress.com/2015/06/localvariable.jpg"><br />
</a>&#8211; This function will tell us if there is a space in player inventory to select another weapon.<br />
<a href="https://shootertutorial.files.wordpress.com/2015/06/canaddweapontoweaponselected.jpg" target="_blank"><img class="alignnone wp-image-232 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/canaddweapontoweaponselected.jpg?w=300&#038;h=115" alt="CanAddWeaponToWeaponSelected" width="300" height="115" /></a></li>
<li>Create another function: <strong>SetBackpackItemSelected<br />
</strong>&#8211; It should have 3 variables in Input:<br />
BackPackItemIndex (int)<br />
IsSelected (bool)<br />
WhichSlot (int)<br />
<img class="alignnone wp-image-233 size-full" src="https://shootertutorial.files.wordpress.com/2015/06/setisselectedinputs.jpg?w=381&#038;h=184" alt="setisselectedinputs" width="381" height="184" /><br />
This function will update data in Backpack_Weapons. For now it&#8217;s setting only InSlot and IsSelected but it can be modified later. Basically you could just pass WeaponBackpackItem as input but for this tutorial I wanted to break this.<br />
<a href="https://shootertutorial.files.wordpress.com/2015/06/setbackpackitemselected.jpg" target="_blank"><img class="alignnone wp-image-234 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/setbackpackitemselected.jpg?w=300&#038;h=91" alt="SetBackpackItemSelected" width="300" height="91" /></a></li>
</ul>
<p>This is all in ShooterGameInstance. It will store our backpack weapons information so everyone can assign / or modify the data.</p>
<hr />
<p>Now create new User Interface -&gt; Widget Blueprint and name it <strong>UI_Debug_WeaponInventoryItem</strong></p>
<p>Explaining UMG using text is difficult but I will try it <span class='wp-smiley wp-emoji wp-emoji-smile' title=':)'>:)</span> |This UMG widget will be our inventory icon. Let&#8217;s start by creating Hierarchy.<br />
<img class=" alignnone wp-image-236 size-full" src="https://shootertutorial.files.wordpress.com/2015/06/itemiconhierarchy.jpg?w=249&#038;h=266" alt="itemiconhierarchy" width="249" height="266" /></p>
<ul>
<li>Delete CanvasPanel. We will be adding this widget to other widgets.<br />
<blockquote><p><strong>USEFUL TIP</strong>: When doing that you should&#8217;t have CanvasPanel. CanvasPanel have information about anchors and our base widget will have those. Duplicating them can affect weird behavior in different resolutions.</p></blockquote>
</li>
<li>Add <strong>Overlay</strong> on top,</li>
<li>Add <strong>Button</strong> with Horizontal and Vertical Alignment set to center. Go to Style and change Image Size for all of the Brushes to 150&#215;150. Remember to change the Size for all of them! (Normal, Hovered, Pressed etc)<br />
<a href="https://shootertutorial.files.wordpress.com/2015/06/buttonsizeandcenter.jpg" target="_blank"><img class="alignnone wp-image-238 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/buttonsizeandcenter.jpg?w=300&#038;h=138" alt="buttonsizeandcenter" width="300" height="138" /></a></li>
<li>Add <strong>Image </strong>which should have center alignment as well and size 130&#215;130.<br />
&#8211; Remember to set it as variable so we will have access in blueprint graph, <a href="https://shootertutorial.files.wordpress.com/2015/06/isvariable.jpg"><br />
</a><a href="https://shootertutorial.files.wordpress.com/2015/06/isvariable.jpg" target="_blank"><img class="alignnone wp-image-239 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/isvariable.jpg?w=300&#038;h=35" alt="isvariable" width="300" height="35" /><br />
</a>&#8211; Set visibility to HitTestInvisible,<br />
<a href="https://shootertutorial.files.wordpress.com/2015/06/hittestinvisible.jpg" target="_blank"><img class="alignnone wp-image-240 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/hittestinvisible.jpg?w=300&#038;h=69" alt="hittestinvisible" width="300" height="69" /></a><br />
We are making sure that our mouse events will go behind the Image and hit our Button.</li>
</ul>
<hr />
<p>Now let&#8217;s go to Graph.</p>
<ul>
<li><a href="https://shootertutorial.files.wordpress.com/2015/06/variablesanddispacher.jpg"><img class="alignleft wp-image-241 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/variablesanddispacher.jpg?w=228&#038;h=300" alt="variablesanddispacher" width="228" height="300" /></a>Create new Event Dispatcher: <strong>WeaponItemClicked</strong> which should have two inputs: WeaponReferenceIndex (int) and WidgetReference (User Widget)<br />
WeaponReferenceIndex will be just an link to our Backpack_Weapons array index.</li>
<li>Create new variable: <strong>WeaponItemReferenceIndex</strong> (int) which should have ExposeOnSpawn and Editable selected. Thanks to that we will be able to assign something to this variable when spawning Widget!</li>
<li>Create another variable: IsInInventory (bool) and set ExposeOnSpawn / Editable as variable above,</li>
<li>Create new Function: GetGameInstance which will return ShooterGameInstance and will be a pure function.<br />
<a href="https://shootertutorial.files.wordpress.com/2015/06/returngameinstance.jpg" target="_blank"><img class="alignnone wp-image-243 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/returngameinstance.jpg?w=300&#038;h=70" alt="returngameinstance" width="300" height="70" /></a></li>
</ul>
<p>Being in graph create new event &#8211; Event Construct (it should be there already) and let&#8217;s do something with variables that we have set Expose On Spawn.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/itemconstruct.jpg" target="_blank"><img class="alignnone wp-image-247 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/itemconstruct.jpg?w=300&#038;h=59" alt="itemconstruct" width="300" height="59" /></a></p>
<p>We are changing Image texture and we are disabling button if it&#8217;s selected. Simple enough.</p>
<p>Now select Button in Designer view and add OnClicked event.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/onclickedevent.jpg" target="_blank"><img class="alignnone wp-image-249 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/onclickedevent.jpg?w=300&#038;h=120" alt="onclickedevent" width="300" height="120" /></a></p>
<p>In graph let&#8217;s call our WeaponItemClicked dispatcher.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/calldispacher.jpg"><img class="alignnone size-medium wp-image-250" src="https://shootertutorial.files.wordpress.com/2015/06/calldispacher.jpg?w=300&#038;h=102" alt="calldispacher" width="300" height="102" /></a></p>
<p>Thanks to this dispatcher we will know in our base Widget that this button was clicked. This is all in this Widget Blueprint.</p>
<hr />
<p>Now let&#8217;s move to the hardest part. We need to create an UI with all of our inventory items and items that we have selected. We need to implement selection functionality as well.</p>
<p>Create new User Interface Widget Blueprint and name it UI_Debug_WeaponSelection.</p>
<p>I&#8217;ve created <a href="https://www.youtube.com/watch?v=6lyyWNx9gHk&amp;feature=youtu.be" target="_blank">short video demonstrating Designer Hierarchy</a>, you can watch it here, but I&#8217;ll try to explain this by text as well.</p>
<hr />
<ul>
<li>Add Overlay panel. We will show inventory items here so it should be big,</li>
<li>Add Border and insert it to Overlay.<br />
<a href="https://shootertutorial.files.wordpress.com/2015/06/borderdetails1.jpg"><img class=" size-medium wp-image-254 alignleft" src="https://shootertutorial.files.wordpress.com/2015/06/borderdetails1.jpg?w=227&#038;h=300" alt="borderdetails" width="227" height="300" /></a>&#8211; Horizontal and Vertical alignment should be set to FILL.<br />
&#8211; Brush draw as should be set to Border,<br />
&#8211; If border is selected Margin should be set to 1!Thanks to this border we will see where is our inventory.</li>
</ul>
<ul>
<li>Add ScrollBox and insert it to Border<br />
&#8211; Horizontal and Vertical alignment should be set to FILL as Border,</li>
<li>Add UniformGridPanel and name it UniformGridPanel_Backpack. It should be set to FILL as well,</li>
</ul>
<p>That&#8217;s all for this overlay. Let&#8217;s move to another one.</p>
<hr />
<p>Create another overlay and put it on the screen somewhere in bottom.</p>
<ul>
<li>Insert border to the Overlay like we did earlier,</li>
<li>Insert Horizontal Box to Overlay and set alignments as FILL,</li>
<li>Insert 3 Overlays to Horizontal Box and name them: Overlay_Slot_1, Overlay_Slot_2 and Overlay_Slot_3.<br />
&#8211; Size should be set to FILL,<br />
&#8211; Alignments should be set to FILL,<br />
&#8211; They should have &#8220;Is Variable&#8221; so we can mess with them in Blueprint Graph.</li>
</ul>
<p>That&#8217;s all here in designer.</p>
<hr />
<p>Now let&#8217;s move to Blueprint Graph and add some variables and functions.</p>
<ul>
<li>Create three bool variables: FirstSlotTaken, SecondSlotTaken and ThirdSlotTaken &#8211; they all should be set to false by default,</li>
<li>Create new function GetShooterGameInstance which should return ShooterGameInstance and should be pure, we have done this earlier so I won&#8217;t post screen here,</li>
<li>Create new function FindFirstFreeSlot which should have one OUTPUT &#8211; Slot (int) and one local variable CurrentFreeSlot (int),<br />
<a href="https://shootertutorial.files.wordpress.com/2015/06/getfirstfreeslot.jpg"><br />
</a><a href="https://shootertutorial.files.wordpress.com/2015/06/getfirstfreeslot.jpg" target="_blank"><img class=" wp-image-255 size-medium alignnone" src="https://shootertutorial.files.wordpress.com/2015/06/getfirstfreeslot.jpg?w=300&#038;h=87" alt="getfirstfreeslot" width="300" height="87" /><br />
</a>This function will return first free slot that we can use when selecting a weapon.</li>
</ul>
<p>Event graph is to big for one screen. Let&#8217;s move step by step.</p>
<p>Create Custom Event &#8211; CreateBackpack with one input: WithoutSelectedWeapons (bool) try to copy this:</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/createbackpack.jpg" target="_blank"><img class="alignnone wp-image-256 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/createbackpack.jpg?w=300&#038;h=130" alt="createbackpack" width="300" height="130" /></a></p>
<p>What this is doing:</p>
<ul>
<li>It&#8217;s iterating for all inventory items in Backpack_Weapons,</li>
<li>Then it&#8217;s creating new widget &#8220;UI_Debug_InventoryItem&#8221; which we have created earlier. It&#8217;s passing data to the widget,</li>
<li>We are connecting our WeaponItemClicked dispatcher.<br />
<a href="https://shootertutorial.files.wordpress.com/2015/06/howtoassigndispatcher.jpg"><br />
</a><a href="https://shootertutorial.files.wordpress.com/2015/06/howtoassigndispatcher.jpg" target="_blank"><img class="alignnone wp-image-257 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/howtoassigndispatcher.jpg?w=300&#038;h=189" alt="howtoassigndispatcher" width="300" height="189" /></a></li>
<li>After that it&#8217;s adding new widgets to Grid Panel,</li>
<li>And it&#8217;s setting column and row,</li>
<li>Then it&#8217;s checking if the IsSelected true,</li>
<li>If yes create another widget and place it to the Panels,</li>
</ul>
<p>This should run only in the beginning of the widget. Here&#8217;s Construction Event which is firing CreateBackpack and letting us to select something using mouse.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/construct.jpg" target="_blank"><img class="alignnone wp-image-258 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/construct.jpg?w=300&#038;h=74" alt="construct" width="300" height="74" /></a></p>
<blockquote><p><strong>USEFUL TIP</strong>: Event Construct will fire every time the widget will be added to viewport!</p></blockquote>
<p>Now let&#8217;s focus on first dispatcher which was in Sequence 0 of the CreateBackpack function.<br />
<a href="https://shootertutorial.files.wordpress.com/2015/06/focusdisp.jpg" target="_blank"><img class="alignnone wp-image-259 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/focusdisp.jpg?w=300&#038;h=94" alt="focusdisp" width="300" height="94" /></a></p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/iteminbackpack.png" target="_blank"><img class="alignnone wp-image-379 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/iteminbackpack.png?w=300&#038;h=111" alt="iteminbackpack" width="300" height="111" /></a></p>
<p>It&#8217;s setting IsSelected and putting IventoryItem to panel.</p>
<p>The last two dispatchers are connected to the same custom event: ItemClickedInBackpack</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/itemclickedinselectedweapons.jpg" target="_blank"><img class="alignnone wp-image-261 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/itemclickedinselectedweapons.jpg?w=300&#038;h=108" alt="itemclickedinselectedweapons" width="300" height="108" /></a></p>
<p>Which is removing items from selected weapons panel. It&#8217;s really hard to describe those using screenshots so <a href="https://www.dropbox.com/s/yao2f9pv4uha0o7/Blueprints.rar?dl=0" target="_blank">here you can download my Blueprints folder</a>. If you want to test this out just create widget UI_Debug_WeaponSelection.</p>
<hr />
<blockquote><p>Creating ShooterTutorial takes a lot of my free time.<br />
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&amp;hosted_button_id=8UXM3JZGDPPPE"><img src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif" alt="Buy Now Button" /><br />
</a>If you want you can help me out! I will use your donation to buy better assets packs and you will be added to Credits /Backers page as well.</p>
<p>Implementing game is taking time but writing about this is taking much more effort!</p></blockquote>
<hr />
]]></html><thumbnail_url><![CDATA[https://i2.wp.com/shootertutorial.files.wordpress.com/2015/06/iteminbackpack.png?fit=440%2C330]]></thumbnail_url><thumbnail_height><![CDATA[162]]></thumbnail_height><thumbnail_width><![CDATA[440]]></thumbnail_width></oembed>