public override UniTask ExecuteAsync(CancellationToken cancellationToken)
        {
            var value = CustomVariablesConfiguration.ParseVariableValue(VariableValue);

            if (Assigned(ArrayIndex))
            {
                var var = FsmVariables.GlobalVariables.FindFsmArray(VariableName);
                if (var is null)
                {
                    LogErrorWithPosition($"PlayMaker global array variable with name `{VariableName}` not found."); return(UniTask.CompletedTask);
                }
                var.Set(ArrayIndex, value);
            }
            else
            {
                var var = FsmVariables.GlobalVariables.FindVariable(VariableName);
                if (var is null)
                {
                    LogErrorWithPosition($"PlayMaker global variable with name `{VariableName}` not found."); return(UniTask.CompletedTask);
                }
                var.RawValue = value;
            }

            return(UniTask.CompletedTask);
        }
Esempio n. 2
0
        public override async UniTask ExecuteAsync(CancellationToken cancellationToken = default)
        {
            var saveStatePending = false;
            var expressions      = Expression.Value.Split(separatorLiteral[0]);

            for (int i = 0; i < expressions.Length; i++)
            {
                var expression = expressions[i];
                if (string.IsNullOrEmpty(expression))
                {
                    continue;
                }

                if (expression.EndsWithFast(incrementLiteral))
                {
                    expression = expression.Replace(incrementLiteral, $"={expression.GetBefore(incrementLiteral)}+1");
                }
                else if (expression.EndsWithFast(decrementLiteral))
                {
                    expression = expression.Replace(decrementLiteral, $"={expression.GetBefore(decrementLiteral)}-1");
                }

                var variableName   = expression.GetBefore(assignmentLiteral)?.TrimFull();
                var expressionBody = expression.GetAfterFirst(assignmentLiteral)?.TrimFull();
                if (string.IsNullOrWhiteSpace(variableName) || string.IsNullOrWhiteSpace(expressionBody))
                {
                    LogErrorMsg("Failed to extract variable name and expression body. Make sure the expression starts with a variable name followed by assignment operator `=`.");
                    continue;
                }

                var result = ExpressionEvaluator.Evaluate <string>(expressionBody, LogErrorMsg);
                if (result is null)
                {
                    continue;
                }

                VariableManager.SetVariableValue(variableName, result);
                saveStatePending = saveStatePending || CustomVariablesConfiguration.IsGlobalVariable(variableName);
            }

            if (saveStatePending)
            {
                await StateManager.SaveGlobalAsync();
            }
        }