Page 1 of 1
Quest Item Reward [SOLVED]
Posted: Mon Dec 23, 2019 9:11 am
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?
Re: Quest Item Reward
Posted: Mon Dec 23, 2019 9:28 am
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?
Re: Quest Item Reward
Posted: Mon Dec 23, 2019 11:42 am
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?
Re: Quest Item Reward
Posted: Mon Dec 23, 2019 1:27 pm
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?
Re: Quest Item Reward
Posted: Mon Dec 23, 2019 2:27 pm
by mschoenhals
Yeah, I think you got it.
Inventory is a non-MonoBehavior class. Any suggestions for a work around?
Re: Quest Item Reward
Posted: Mon Dec 23, 2019 2:37 pm
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);
}
Re: Quest Item Reward
Posted: Mon Dec 23, 2019 3:08 pm
by mschoenhals
Ah!
That worked! Perfect, thank you.
Re: Quest Item Reward [SOLVED]
Posted: Mon Dec 23, 2019 4:02 pm
by Tony Li
Happy to help!