How do I implement Resource Gathering from Trees and Rocks

I am trying to figure a way to implement “resource gathering”. For example, in order for me to build a hut or structure, I need wood. I need to gather that resource from the trees.

How would I script that so that when I hit the tree, it gives me wood. Then eventually the tree runs out of wood and falls over and then runs a destroy object on itself. The same thing with rocks, for mining.

I tried entering this question into the forums different ways and was not able to find an answer.

Thanks for your help.

You need an inventory or backpack.

The legendary Tom Looman has a great post about this!

There’s a C++ version and a Blueprints version.

For implementing resource gathering, the simplest approach will be to have a distance check and a raytrace to check if the player has “hit” a resource structure.
If they hit a resource structure, then add a number of resources to their inventory as guided by Looman’s tutorial.

  1. Data Assets: When should I use Data Asset and Object Library? - Programming & Scripting - Unreal Engine Forums There are differing schools of thought on how to handle inventory, but I would go data assets. As you’re probably thinking about a multiplayer game, data assets can be very useful for binding ID’s to database entries for persistence, AND you can put them in UE datatables for easy spreadsheet import/export!
  2. Get some tree models, populate map. When the player inputs at the tree, spawn logs. This is vague, but there are many implementations. For a action-combat model you would run a line trace from the camera forward vector, and if the tree was hit in that line trace (Check hit Actor class) while left mouse button was held down, you’d know the player wanted wood from that tree.
  3. For the tree running out of resources and “dying”. I would have a variable set up in a blueprint: resourceAmount. Whenever the hit detection is done from 2, call a method on that tree object (call it updateResource or whatever). In that updateResource method if the new amount is <= 0, then play the dying animation for the skeletal mesh.

Pally QLE does a great job explaining how to implement resource gathering. What’s even better - he provides you with the entire project that you can pick apart that shows you how to do it. Check it out.