summaryrefslogtreecommitdiff
path: root/Client/Assets/Behavior Designer/Runtime/Basic Tasks/String/Format.cs
blob: 96e57f85d6445ad4e798c3ed668991dc06de0199 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using UnityEngine;
using System;

namespace BehaviorDesigner.Runtime.Tasks.Basic.UnityString
{
    [TaskCategory("Basic/String")]
    [TaskDescription("Stores a string with the specified format.")]
    public class Format : Action
    {
        [Tooltip("The format of the string")]
        public SharedString format;
        [Tooltip("Any variables to appear in the string")]
        public SharedGenericVariable[] variables;
        [Tooltip("The result of the format")]
        [RequiredField]
        public SharedString storeResult;

        private object[] variableValues;

        public override void OnAwake()
        {
            variableValues = new object[variables.Length];
        }

        public override TaskStatus OnUpdate()
        {
            for (int i = 0; i < variableValues.Length; ++i) {
                variableValues[i] = variables[i].Value.value.GetValue();
            }

            try {
                storeResult.Value = string.Format(format.Value, variableValues);
            } catch (Exception e) {
                Debug.LogError(e.Message);
                return TaskStatus.Failure;
            }
            return TaskStatus.Success;
        }

        public override void OnReset()
        {
            format = "";
            variables = null;
            storeResult = null;
        }
    }
}