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?
Quest Item Reward [SOLVED]
-
- Posts: 118
- Joined: Thu Dec 19, 2019 7:38 am
Quest Item Reward [SOLVED]
Last edited by mschoenhals on Mon Dec 23, 2019 3:08 pm, edited 1 time in total.
Re: Quest Item Reward
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?
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?
-
- Posts: 118
- Joined: Thu Dec 19, 2019 7:38 am
Re: Quest Item Reward
Here's the basics of my code:
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?
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.
}
}
}
Re: Quest Item Reward
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?
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?
-
- Posts: 118
- Joined: Thu Dec 19, 2019 7:38 am
Re: Quest Item Reward
Yeah, I think you got it.
Inventory is a non-MonoBehavior class. Any suggestions for a work around?
Inventory is a non-MonoBehavior class. Any suggestions for a work around?
Re: Quest Item Reward
If Inventory is a static class, you can access it directly in your custom action. Maybe something like:
Otherwise some other class probably references it. For example, maybe you have a Player class like this:
In this case, you can find the Player class and get to the inventory that way:
Code: Select all
public override void Execute()
{
base.Execute();
Inventory.AddItem(item);
}
Code: Select all
public class Player : MonoBehaviour
{
public Inventory inventory;
Code: Select all
public override void Execute()
{
var player = FindObjectOfType<Player>();
var inventory = player.inventory;
Inventory.AddItem(item);
}
-
- Posts: 118
- Joined: Thu Dec 19, 2019 7:38 am
Re: Quest Item Reward
Ah!
That worked! Perfect, thank you.
That worked! Perfect, thank you.
Re: Quest Item Reward [SOLVED]
Happy to help!