News Forums RAIN General Discussion and Troubleshooting Using Sendmessage and behavior trees

This topic contains 5 replies, has 2 voices, and was last updated by  prime 2 months, 2 weeks ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #34789

    Jsmucker
    Participant

    I am trying to send a message using a custom action and making a new class. Here is what I have so far in the class I created:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using RAIN.Action;
    using RAIN.Core;
    [RAINAction]
    public class TakeDamage : RAINAction
    {
        public override void Start(AI ai)
        {
    		ai.gameObject.SendMessage("Damage", 1.0f, SendMessageOptions.DontRequireReceiver);
            base.Start(ai);
        }
        public override ActionResult Execute(AI ai)
        {
            return ActionResult.SUCCESS;
        }
        public override void Stop(AI ai)
        {
            base.Stop(ai);
        }
    }

    I’ve tried using all of these instead of the sendmessage in the code.
    GameObject.SendMessage: Returns error “An object reference is required to access non-static member”
    Component.SendMessage: Returns error “An object reference is required to access non-static member”
    gameObject.SendMessage: Returns error “The name `gameObject’ does not exist in the current context”
    SendMessage: Returns error “The name `SendMessage’ does not exist in the current context”
    ai.gameObject: returns error “RAIN.Core.AI’ does not contain a definition for `gameObject’ and no extension method `gameObject’ of type”
    Any ways to do this successfully?

    #34790

    prime
    Keymaster

    ai.Body.SendMessage is what you want.

    The “Body” of the AI is the parent game object that the AI controls.

    #34795

    Jsmucker
    Participant

    Awesome, that fixed the error, but for some reason the send message is going to the wrong gameobject. I am very inexperienced in c# and don’t know what I am doing wrong. The sendmessage is sending a damage function to the enemy instead of the player for some reason. I originally got the function to work when I put it in a script attached to the enemy and used this format to get the message to the player(“Hero”):

    void OnTriggerEnter(Collider other) {
        if (other.name == "Hero")
            other.SendMessage("Damage", 1.0f, SendMessageOptions.DontRequireReceiver);
    }
    #34798

    prime
    Keymaster

    So Hero is the player? I’m a little confused because your custom action is called TakeDamage. Is it applying damage to itself or to some other object?

    #34813

    Jsmucker
    Participant

    It should be applying damage to some other object.

    #34835

    prime
    Keymaster

    ai.Body will be the game object of the AI itself, so you don’t want that. If you are applying damage to an object that you detected with a sensor, and you are setting the Form Variable through the sensor, then

    GameObject tSensedObject = ai.WorkingMemory.GetItem<GameObject>("myFormVariable"); //whatever your variable name is, which should be used in the Form Variable field of the sensor
    tSensedObject.SendMessage("Damage", 1.0f, SendMessageOptions.DontRequireReceiver);
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.