Quest Item Reward [SOLVED]

Announcements, support questions, and discussion for Quest Machine.
Post Reply
mschoenhals
Posts: 118
Joined: Thu Dec 19, 2019 7:38 am

Quest Item Reward [SOLVED]

Post by mschoenhals »

Hi,
I'm trying to get a Quest Giver to pass an item on to the player. I'm trying to use the starter template to pass the item on to the inventory. I can't seem to find the inventory (type mismatch). I've also tried to instatiate the item but there's no transform to position it. What is the best way to give item rewards using Quest Machine?
Last edited by mschoenhals on Mon Dec 23, 2019 3:08 pm, edited 1 time in total.
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Quest Item Reward

Post by Tony Li »

Hi,

Are you using an inventory system from the Asset Store, such as Inventory Engine or Inventory Pro? If so, import the integration package for that asset and use the included custom quest action.

Or are you using your own inventory system? If so, duplicate the starter template and fill in the code to add an item to your inventory system. If this is where the error is occurring, can you please post your custom quest action code?

Or are you using something else?
mschoenhals
Posts: 118
Joined: Thu Dec 19, 2019 7:38 am

Re: Quest Item Reward

Post by mschoenhals »

Here's the basics of my code:

Code: Select all

using UnityEngine;
using System;

namespace PixelCrushers.QuestMachine
{
    public class QuestItem : QuestAction // Rename this class.
    {

        [SerializeField] Item item;
        [SerializeField] Inventory inventory;

        public override void Execute()
        {
            base.Execute();

            if (item != null)
            {
                inventory.AddItem(item);
            }
            // Add your code here. This code will run when the action runs.
        }

    }

}
So when I try and add the inventory in the inspector, it can't be found. I've also tried finding it using a tag, name, etc. Any suggestions?
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Quest Item Reward

Post by Tony Li »

Hi,

Keep in mind that quests are assets, so they exist outside of any particular scene. If Inventory is a MonoBehaviour on a GameObject in your scene, you'll need to get a reference a different way. What is Inventory?
mschoenhals
Posts: 118
Joined: Thu Dec 19, 2019 7:38 am

Re: Quest Item Reward

Post by mschoenhals »

Yeah, I think you got it.

Inventory is a non-MonoBehavior class. Any suggestions for a work around?
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Quest Item Reward

Post by Tony Li »

If Inventory is a static class, you can access it directly in your custom action. Maybe something like:

Code: Select all

public override void Execute()
{
    base.Execute();
    Inventory.AddItem(item);
}
Otherwise some other class probably references it. For example, maybe you have a Player class like this:

Code: Select all

public class Player : MonoBehaviour
{
    public Inventory inventory;
In this case, you can find the Player class and get to the inventory that way:

Code: Select all

public override void Execute()
{
    var player = FindObjectOfType<Player>();
    var inventory = player.inventory;
    Inventory.AddItem(item);
}
mschoenhals
Posts: 118
Joined: Thu Dec 19, 2019 7:38 am

Re: Quest Item Reward

Post by mschoenhals »

Ah!
That worked! Perfect, thank you. :D
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Quest Item Reward [SOLVED]

Post by Tony Li »

Happy to help! :-)
Post Reply