FlatRedBall.Glue.ElementRuntime.SetVariablesForNamedObject C# (CSharp) Method

SetVariablesForNamedObject() private method

private SetVariablesForNamedObject ( object newlyCreatedElementRuntime, NamedObjectSave n ) : void
newlyCreatedElementRuntime object
n FlatRedBall.Glue.SaveClasses.NamedObjectSave
return void
        private void SetVariablesForNamedObject(object newlyCreatedElementRuntime, NamedObjectSave n)
        {

            if (newlyCreatedElementRuntime != null)
            {
                ElementRuntime containedElementRuntime = GetContainedElementRuntime(n);

                Type typeOfNewObject = newlyCreatedElementRuntime.GetType();

                // As of June 24, 2012
                // States are set before
                // CustomVariables:
                if (!string.IsNullOrEmpty(n.CurrentState))
                {
                    containedElementRuntime.SetState(n.CurrentState, false);
                }


                // If it's null, this means it's a default value so don't set anything               
                foreach (CustomVariableInNamedObject cvino in n.InstructionSaves.Where(cvino=>cvino.Value != null))
                {
                    // If there isn't a TypedMember for the variable, that means Glue won't generate code for it, so we shouln't be applying it.
                    if (!IsVariableExposed(cvino, n))
                    {

                        continue;
                    }

                    try
                    {
                        // We used to execute 
                        // the Instructions right
                        // on the element runtime itself
                        // but I think it's best if we use
                        // the CustomVariable code so that we
                        // get all the benefit of CustomVariables
                        // like loading of files, and calling events.
                        //cvino.ToInstruction(newlyCreatedElementRuntime).Execute();

                        // Update May 25, 2012
                        // We used to get the ElementRuntime
                        // for the NamedObjectSave and have it
                        // set the CustomVariable - however this
                        // doesn't work because it tries to access
                        // files within its own scope, instead of files
                        // that belong to "this".
                        //CustomVariable customVariable = new CustomVariable();
                        //customVariable.Type = cvino.Type;
                        ////customVariable.SourceObject = n.InstanceName;
                        ////customVariable.SourceObjectProperty = cvino.Member;
                        //customVariable.DefaultValue = cvino.Value;
                        //customVariable.Name = cvino.Member;
                        //containedElementRuntime.SetCustomVariable(customVariable, customVariable.DefaultValue, false);

                        CustomVariable customVariable = new CustomVariable();
                        customVariable.Type = cvino.Type;
                        customVariable.SourceObject = n.InstanceName;
                        customVariable.SourceObjectProperty = cvino.Member;
                        customVariable.DefaultValue = cvino.Value;
                        customVariable.Name = cvino.Member;
                        if (cvino.Value is string && customVariable.GetIsFile())
                        {
                            // We want to load the file at this level and pass the result down:
                            ReferencedFileSave rfs = GetReferencedFileFromName(cvino.Value);
                            object fileRuntime = null;

                            if (rfs == null)
                            {
                                fileRuntime = null;
                            }
                            else
                            {
                                fileRuntime = LoadReferencedFileSave(rfs, true, this.AssociatedIElement);
                            }

                            customVariable.DefaultValue = fileRuntime;
                        }
                        if (customVariable.DefaultValue is float && customVariable.SourceObjectProperty == "Z" &&
                            newlyCreatedElementRuntime is PositionedObject && ((PositionedObject)newlyCreatedElementRuntime).Parent == Camera.Main)
                        {
                            float value = (float)customVariable.DefaultValue - 40;

                            customVariable.DefaultValue = value;

                        }
                        SetCustomVariable(customVariable, mAssociatedIElement, customVariable.DefaultValue, false);
                        
                    }
                    catch(Exception e)
                    {
                        int m = 3;
                        m++;
                        // for now, do nothing
                    }

                }
            }
        }