0
Answered

C# Complete Action

moreiramario 5 months ago in Game Creator updated by Marti (Lead Developer) 5 months ago 4

Hi,

Whenever I create a C# Complete Action I have the following behaviour:

1 - First time I add the action into the inspector, all is fine and I can set its properties;

2 - When I press play in Unity, the action in the inspector starts saying "Undefined Action" and no code runs;

3 - When I stop running Unity, the action in the inspector continues saying "Undefined Action".

Here the code for this action. 

Basically what I want to do is to play an animation when a (dialog) message is triggered.

However the behaviour and error shown above happen, whatever the code, i.e. even if I don't add any code to new created complete action.

namespace GameCreator.Messages
{
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Audio;
using GameCreator.Core;
using GameCreator.Localization;

#if UNITY_EDITOR
using UnityEditor;
#endif

[AddComponentMenu("")]
public class ActionFloatingMessage : IAction
{
public AudioClip audioClip;

[LocStringNoPostProcess]
public LocString message = new LocString();

public Color color = Color.white;
public float time = 2.0f;

public TargetGameObject target = new TargetGameObject(TargetGameObject.Target.GameObject);
public Vector3 offset = new Vector3(0, 2, 0);

private bool forceStop = false;


//animation part
public TargetGameObject animator = new TargetGameObject(TargetGameObject.Target.GameObject);

public string parameterName = "Parameter Name";
public AnimatorControllerParameterType parameterType = AnimatorControllerParameterType.Trigger;

public int parameterInteger = 1;
public float parameterFloat = 1.0f;
public bool parameterBool = true;




// EXECUTABLE: ----------------------------------------------------------------------------

public override IEnumerator Execute(GameObject target, IAction[] actions, int index)
//public override bool InstantExecute(GameObject target, IAction[] actions, int index)
{
Transform targetTransform = this.target.GetTransform(target);
if (targetTransform != null)
{
if (this.audioClip != null)
{
AudioMixerGroup voiceMixer = DatabaseGeneral.Load().voiceAudioMixer;
AudioManager.Instance.PlayVoice(this.audioClip, 0f, 1f, voiceMixer);
}


Animator anim = this.animator.GetComponent(target);

if (anim != null)
{
switch (this.parameterType)
{
case AnimatorControllerParameterType.Trigger:
anim.SetTrigger(this.parameterName);
break;

case AnimatorControllerParameterType.Int:
anim.SetInteger(this.parameterName, this.parameterInteger);
break;

case AnimatorControllerParameterType.Float:
anim.SetFloat(this.parameterName, this.parameterFloat);
break;

case AnimatorControllerParameterType.Bool:
anim.SetBool(this.parameterName, this.parameterBool);
break;
}
}

FloatingMessageManager.Show(
this.message.GetText(), this.color,
targetTransform, this.offset, this.time
);

float waitTime = Time.time + this.time;
WaitUntil waitUntil = new WaitUntil(() => Time.time > waitTime || this.forceStop);
yield return waitUntil;

if (this.audioClip != null)
{
AudioManager.Instance.StopVoice(this.audioClip);
}
}

yield return 0;
}

public override void Stop()
{
this.forceStop = true;
}

#if UNITY_EDITOR

private static readonly GUIContent GUICONTENT_ANIMATOR = new GUIContent("Animator");
private static readonly GUIContent GUICONTENT_PARAM_NAME = new GUIContent("Parameter Name");
private static readonly GUIContent GUICONTENT_PARAM_TYPE = new GUIContent("Parameter Type");

private static readonly GUIContent GUICONTENT_PARAM_INT = new GUIContent("Integer");
private static readonly GUIContent GUICONTENT_PARAM_FLO = new GUIContent("Float");
private static readonly GUIContent GUICONTENT_PARAM_BOL = new GUIContent("Bool");

public static new string NAME = "Messages/Floating Message and Animation";
private const string NODE_TITLE = "Show message: {0}";

// PROPERTIES: ----------------------------------------------------------------------------

private SerializedProperty spAudioClip;
private SerializedProperty spMessage;
private SerializedProperty spColor;
private SerializedProperty spTime;
private SerializedProperty spTarget;
private SerializedProperty spOffset;

private SerializedProperty spAnimator;
private SerializedProperty spParameterName;
private SerializedProperty spParameterType;
private SerializedProperty spParameterInteger;
private SerializedProperty spParameterFloat;
private SerializedProperty spParameterBool;


// INSPECTOR METHODS: ---------------------------------------------------------------------

public override string GetNodeTitle()
{
return string.Format(
NODE_TITLE,
(this.message.content.Length > 23
? this.message.content.Substring(0, 20) + "..."
: this.message.content
)
);
}

protected override void OnEnableEditorChild()
{
this.spAudioClip = this.serializedObject.FindProperty("audioClip");
this.spMessage = this.serializedObject.FindProperty("message");
this.spColor = this.serializedObject.FindProperty("color");
this.spTime = this.serializedObject.FindProperty("time");

this.spTarget = this.serializedObject.FindProperty("target");
this.spOffset = this.serializedObject.FindProperty("offset");

this.spAnimator = this.serializedObject.FindProperty("animator");
this.spParameterName = this.serializedObject.FindProperty("parameterName");
this.spParameterType = this.serializedObject.FindProperty("parameterType");

this.spParameterInteger = this.serializedObject.FindProperty("parameterInteger");
this.spParameterFloat = this.serializedObject.FindProperty("parameterFloat");
this.spParameterBool = this.serializedObject.FindProperty("parameterBool");
}

public override void OnInspectorGUI()
{
this.serializedObject.Update();

EditorGUILayout.PropertyField(this.spMessage);
EditorGUILayout.PropertyField(this.spAudioClip);
EditorGUILayout.PropertyField(this.spColor);

EditorGUILayout.Space();
EditorGUILayout.PropertyField(this.spTime);

if (this.spAudioClip.objectReferenceValue != null)
{
AudioClip clip = (AudioClip)this.spAudioClip.objectReferenceValue;
if (!Mathf.Approximately(clip.length, this.spTime.floatValue))
{
Rect btnRect = GUILayoutUtility.GetRect(GUIContent.none, EditorStyles.miniButton);
btnRect = new Rect(
btnRect.x + EditorGUIUtility.labelWidth,
btnRect.y,
btnRect.width - EditorGUIUtility.labelWidth,
btnRect.height
);

if (GUI.Button(btnRect, "Use Audio Length", EditorStyles.miniButton))
{
this.spTime.floatValue = clip.length;
}
}
}

EditorGUILayout.Space();
EditorGUILayout.PropertyField(this.spTarget);
EditorGUILayout.PropertyField(this.spOffset);
EditorGUILayout.Space();

EditorGUILayout.PropertyField(this.spAnimator, GUICONTENT_ANIMATOR);
EditorGUILayout.Space();

EditorGUILayout.PropertyField(this.spParameterName, GUICONTENT_PARAM_NAME);
EditorGUILayout.PropertyField(this.spParameterType, GUICONTENT_PARAM_TYPE);
int paramTypeInt = this.spParameterType.intValue;
AnimatorControllerParameterType paramType = (AnimatorControllerParameterType)paramTypeInt;

EditorGUI.indentLevel++;
switch (paramType)
{
case AnimatorControllerParameterType.Int :
EditorGUILayout.PropertyField(this.spParameterInteger, GUICONTENT_PARAM_INT);
break;

case AnimatorControllerParameterType.Float :
EditorGUILayout.PropertyField(this.spParameterFloat, GUICONTENT_PARAM_FLO);
break;

case AnimatorControllerParameterType.Bool :
EditorGUILayout.PropertyField(this.spParameterBool, GUICONTENT_PARAM_BOL);
break;
}
EditorGUI.indentLevel--;


this.serializedObject.ApplyModifiedProperties();
}
#endif
}
}

Thank you in advance for your advise.

Unity version:
2019.2 Personal
Game Creator version:
1.1.6

Answer

+1
Answer

There is already an ActionFloatingMessage script in the GC core. You need to change the name of your script (both file name and public class name) to something unique, or change the name space.

This should help.

+1
Answer

There is already an ActionFloatingMessage script in the GC core. You need to change the name of your script (both file name and public class name) to something unique, or change the name space.

This should help.

Your welcome, hope it now works.