Trying to spawn item from item action. Examine item

aphixe

New member
I'm trying to spawn in a prefab onto the camera as a child. So i can examine it. I am using an Item Action.

this is string I am able to get. in this example a "Assault Riffle"
String SpawnItem = itemInfo.Item.name;

I see i am able to get the name of the item, also id too but i don't know if its better for what i am trying to do. but now trying to spawn via txt file isn't working. also I noticed I can not instantiate inside this script. what should I do.. is there a way to get the game object instead so i can spawn it onto my Main camera?

Someone said online if i wanted to spawn as a string, i would do a Resource load. A step in the right direction would help

using Opsive.UltimateInventorySystem.Core;
using Opsive.UltimateInventorySystem.Core.InventoryCollections;
using HutongGames.PlayMaker;
using UnityEngine;
namespace Opsive.UltimateInventorySystem.ItemActions
{
using System;
using Opsive.Shared.Game;
using Opsive.UltimateCharacterController.Integrations.Playmaker;
using Opsive.UltimateInventorySystem.Core.DataStructures;
using Opsive.UltimateInventorySystem.DropsAndPickups;
using Unity.VisualScripting;
using UnityEngine;
/// <summary>
/// Simple item action used to examine items.
/// </summary>
[System.Serializable]
public class ActionExamine : ItemAction
{
private const string GlobalEventName = "ExamineMe";
private const string FsmName = "CallExamineFSM";
private const string FsmVariableName = "SpawnItem"; // Name of the FSM variable
[SerializeField] protected GameObject m_PickUpItemPrefab;
public GameObject PickupPrefab { get => m_PickUpItemPrefab; set => m_PickUpItemPrefab = value; }
protected GameObject m_PickUpGameObject;
public GameObject PickUpGameObject => m_PickUpGameObject;
protected override bool CanInvokeInternal(ItemInfo itemInfo, ItemUser itemUser)
{
return true;
}
/// <summary>
/// Invoke the action.
/// </summary>
/// <param name="itemInfo">The item.</param>
/// <param name="itemUser">The item user (can be null).</param>
protected override void InvokeActionInternal(ItemInfo itemInfo, ItemUser itemUser)
{
String SpawnItem = itemInfo.Item.name;

// Use GameObject.FindObjectsOfType to find all PlayMakerFSM components in the scene
PlayMakerFSM[] fsms = GameObject.FindObjectsOfType<PlayMakerFSM>();
// Iterate through each FSM and send the event if it matches the specified FSM name
foreach (PlayMakerFSM fsm in fsms)
{
if (fsm.FsmName == FsmName)
{
fsm.Fsm.Event(GlobalEventName);
}
}
}
}
}
 
I believe I already answered your question in the discord. I'll copy paste my answer and update it such that it can spawn under the camera:

Yes neither of those are close to what you want.As I mentioned, the prefab you want to spawn is inside the item as an Attribute. So the steps are.
  1. Get the transform where you want to spawn the prefab, we'll call it "parent"
  2. Get the prefab from an item attribute
  3. Spawn the prefab inside the parent.

You can add the following code in your custom ItemAction. and call it in "InvokeActionInternal"
Code:
public void SpawnPrefabAttribute(ItemInfo itemInfo)
{
    Transform parent = Camera.main.transform;

    string m_AttributeName ="Prefabs";//Double check it matches your attribute name

    if (itemInfo.Item == null) {
    Debug.LogWarning("Item is null");
    return;
    }
    
    if (itemInfo.Item.TryGetAttributeValue(m_AttributeName, out GameObject prefab) == false) {
    Debug.LogWarning($"Prefab attribute value not found for attribute name {m_AttributeName}");
    return;
    }
    
    if (prefab == null) {
    Debug.LogWarning("Attribute exists, but no prefab was assigned.");
    return;
    }
    
    GameObject.Instantiate(prefab, parent);
}

The prefab is an attribute
1719315154085.png

So to answer your other questions:

also I noticed I can not instantiate inside this script.
That's incorrect. You can instantiate objects from an ItemAction script. You just need to add "Object" or "GameObject" before the instantiate function as you are in a ScriptableObject and not a Monobehaviour script.
Example:
Code:
GameObject.Instantiate(prefab, parent);

Someone said online if i wanted to spawn as a string, i would do a Resource load
That is half right/half wrong. Spawning objects with a string can be done via the Resources or addressables or any other custom system you write. But in your case you don't need to spawn by string at all. You can spawn the prefab from the attribute. and you get the attribute from the item, not the string

I hope that points you in the right direction
 
Back
Top