Forum rules - please read before posting.

Dynamic ActionList creation from script - issues with Object.sendMessage calls

Looks like I'm doing something nobody has ever done with AdventureCreator? I've placed a number of GameObjects in my game at fixed positions which will be replaced randomly (and not all of them, just a certain percentage) with other GameObjects that can be picked up.
As I have three different GameObject types that can be picked up which will be placed at those positions randomly (and the non-used position will be hidden) and I still want to use the hotspot functionality of AC I have to create ActionsLists dynamically using a script and attach one of those custom action lists to each of those objects.

The problem occurs when I want to execute an Object.sendMessage call in the ActionList. My pickup function to be called when an item is picked up requires two parameters and as sendMessage only supports one parameter I see two options:
1. Create an object derived from UnityEngine.Object and pass it to the SendMessage call and extract the two parameters when it is called
2. Creating a list with unique ids for each of those objects and only use that id to pick the correct GameObject from the list

I tried 1. but the problem is that this error occurs:

Failed to call function handlePickupItem of class TMGlobalScript
Calling function handlePickupItem with no parameters but the function requires 1.
UnityEngine.GameObject:SendMessage (string,UnityEngine.SendMessageOptions)

This is the corresponding code:

public class ParamObject : Object
{
    public int inventoryId;
    public GameObject obj;

    public ParamObject(int inventoryId, GameObject obj)
    {
        this.inventoryId = inventoryId;
        this.obj = obj;
    }
}

public class TMGlobalScript : MonoBehaviour
{
        ...

        // 8. Call pickup function
        Action pickupAction = ActionSendMessage.CreateNew(gameObject, "handlePickupItem");

        List<ActionParameter> parameters = new List<ActionParameter>();

        ParamObject info = new ParamObject(itemId, cloneObj);
        ActionParameter p = new ActionParameter(0);
        p.SetValue(info);
        parameters.Add(p);
        pickupAction.AssignValues(parameters);
        actionList.Add(pickupAction);

        ...

        // Helper function because SendMessage only supports one param
        void handlePickupItem(ParamObject info)
        {
            pickupItem(info.inventoryId, info.obj);
        }

        // Adds the item to the inventory and hides the picked up object - and plays a sound
        void pickupItem(int inventoryId, GameObject obj)
        {
            Debug.Log("***** PICKUP ITEM #"+inventoryId);
            ...
        }

        ...
}

What I find a bit weird is that there is no constructor with an object as a parameter so I have to assign an integer first and then override it with an object.

Everything else in the code is working (not shown here: the GameObject is hidden when picked up, the inventory count is increased).

Comments

  • I just tried this using a dictionary and thought that would work directly, but it didn't. However, I then found the error.
    Instead of using AssignValues you obviously have to set the values directly using the public attributes:

        DictionaryPickupItem pickupItm = new DictionaryPickupItem(itemId, cloneObj);
        int dictionaryId = addPickupToDict(pickupItm);
        ActionSendMessage pickupAction = ActionSendMessage.CreateNew(gameObject, "handlePickupItemDict");
        pickupAction.sendValue = true;
        pickupAction.customValue = dictionaryId;
        actionList.Add(pickupAction);
    

    That way I could just pass a single integer value to the method called via sendMessage!

  • There's an alternative CreateNew function for this Action that you should be able to use to avoid having to modify the new instance after declaration:

    ActionSendMessage pickupAction = ActionSendMessage.CreateNew(gameObject, "handlePickupItemDict", dictionaryId);
    
  • Thanks, Chris! Looks like I have missed that one!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.