GlueViewOfficialPlugins.Scripting.ScriptParsingPlugin.CreateCustomVariableFor C# (CSharp) Method

CreateCustomVariableFor() public method

public CreateCustomVariableFor ( string newVariableDeclarationType, string leftOfEquals, string rightOfEquals, ElementRuntime elementRuntime, CodeContext codeContext ) : CustomVariable
newVariableDeclarationType string
leftOfEquals string
rightOfEquals string
elementRuntime FlatRedBall.Glue.ElementRuntime
codeContext GlueView.Scripting.CodeContext
return FlatRedBall.Glue.SaveClasses.CustomVariable
        public CustomVariable CreateCustomVariableFor(string newVariableDeclarationType, string leftOfEquals, string rightOfEquals, ElementRuntime elementRuntime, CodeContext codeContext)
        {
            IElement element = null;
            CustomVariable toReturn = null;
            if (elementRuntime != null)
            {
                element = elementRuntime.AssociatedIElement;
                toReturn = element.GetCustomVariableRecursively(leftOfEquals);
            }
            // See if there is already a CustomVariable for this:

            if (toReturn != null)
            {
                toReturn = toReturn.Clone();
            }

            if (toReturn == null)
            {
                // If there's no event, we gotta create one
                toReturn = new CustomVariable();
                toReturn.Name = leftOfEquals;

                // If the left side has a period, that means the user is setting a variable on a contained object (because this. will already have been stripped)
                if (leftOfEquals.Contains('.'))
                {
                    int indexOfDot = leftOfEquals.IndexOf('.');
                    toReturn.SourceObject = leftOfEquals.Substring(0, indexOfDot);
                    toReturn.SourceObjectProperty = leftOfEquals.Substring(indexOfDot + 1, leftOfEquals.Length - (indexOfDot + 1));
                }


                if (newVariableDeclarationType == null)
                {
                    Type throwaway = null;
                    toReturn.Type = GetTypeStringForValue(leftOfEquals, elementRuntime, codeContext, out throwaway);
                }
                else
                {
                    toReturn.Type = newVariableDeclarationType;
                }

            }

            object valueToAssign = null;

            if (!string.IsNullOrEmpty(rightOfEquals))
            {
                valueToAssign = mExpressionParser.EvaluateExpression(rightOfEquals, codeContext);
            }

            if (toReturn.Type == null && valueToAssign != null)
            {
                toReturn.Type = valueToAssign.GetType().FullName;
            }

            if (toReturn.Type != null)
            {


                if (toReturn.GetRuntimeType() == typeof(float))
                {
                    if (valueToAssign is double)
                    {
                        valueToAssign = (float)((double)valueToAssign);
                    }
                    else if (valueToAssign is int)
                    {
                        valueToAssign = (float)((int)valueToAssign);
                    }
                }


                if (valueToAssign is float && float.IsNaN( (float)valueToAssign ))
                {
                    int m = 3;
                }

                toReturn.DefaultValue = valueToAssign;
            }

            return toReturn;
        }