private void ExcludeAndIncludeGlueVariables(NamedObjectSave instance)
        {

            bool shouldIncludeSourceClassType = true;
            bool shouldIncludeSourceFile = true;
            bool shouldIncludeSourceName = true;
            bool shouldIncludeSourceClassGenericType = true;
            bool shouldShowCurrentState = true;
            bool shouldIncludeIncludeInIVisible = true;
            bool shouldIncludeIncludeInIClickable = true;
            bool shouldIncludeIsContainer = true;
            bool shouldShowIsZBuffered = false;
            bool shouldIncludeSetByContainer = true;

            ExcludeMember("InstructionSaves");
            ExcludeMember("Properties");
            ExcludeMember("FileCreatedBy");
            ExcludeMember("FulfillsRequirement");
            ExcludeMember("IsNodeHidden");


            if (DisplayMode == DisplayModes.VariablesOnly)
            {
                ExcludeAllMembers();
            }
            else
            {

                if (DisplayMode != DisplayModes.Debug)
                {
                    ExcludeMember("InstantiatedByBase");
                }

                var containerType = instance.GetContainerType();

                // Screens can't be IVisible/IClickable so no need to show these properties
                // in screens
                shouldIncludeIncludeInIVisible = containerType == ContainerType.Entity;
                shouldIncludeIncludeInIClickable = containerType == ContainerType.Entity;

                bool shouldShowAttachToContainer = containerType == ContainerType.Entity &&
                    instance.IsList == false;
                // Not sure if we want to keep this or not, but currently objects in Entities can't be attached to the Camera
                bool shouldShowAttachToCamera = instance.GetContainerType() == ContainerType.Screen;

                if (!shouldShowAttachToContainer)
                {
                    this.ExcludeMember("AttachToContainer");
                }


                if (!shouldShowAttachToCamera)
                {
                    ExcludeMember("AttachToCamera");
                }

                if (instance.SourceType == SaveClasses.SourceType.FlatRedBallType && instance.SourceClassType == "Layer")
                {

                }

                // We used to not show the AddToManagers property for objects inside Entities, but as I worked on SteamBirds
                // I found myself needing it.
                //else if (ContainerType == ContainerType.Entity)
                //{
                //    ExcludeMember("AddToManagers");
                //}

                if (instance.SetByDerived)
                {
                    ExcludeMember("AttachToContainer");
                }
                if (instance.InstantiatedByBase)
                {
                    ExcludeMember("SourceType");
                    shouldIncludeSourceClassType = false;
                    shouldIncludeSourceClassGenericType = false;

                    ExcludeMember("InstanceName");
                    ExcludeMember("CallActivity");
                    ExcludeMember("IgnoresPausing");
                    ExcludeMember("HasPublicProperty");
                    ExcludeMember("ExposedInDerived");

                    ExcludeMember("SetByDerived");
                    ExcludeMember("SetByContainer");
                }

                bool shouldIncludeAddToManagers = !instance.InstantiatedByBase && !instance.IsList;
                if (!shouldIncludeAddToManagers)
                {
                    ExcludeMember("AddToManagers");
                }

                UpdateLayerIncludeAndExclude(instance);

                if (containerType != ContainerType.Entity)
                {
                    shouldIncludeIsContainer = false;
                    shouldIncludeSetByContainer = false;
                }

                #region Camera-related properties

                if (instance.SourceType != SaveClasses.SourceType.FlatRedBallType || instance.SourceClassType != "Camera")
                {
                    ExcludeMember("IsNewCamera");
                }

                #endregion

                #region Text-related properties

                if (instance.SourceType != SaveClasses.SourceType.FlatRedBallType || instance.SourceClassType != "Text")
                {
                    ExcludeMember("IsPixelPerfect");
                }

                #endregion

                
                                        // we don't show this on files because Sprites from file will be put on the z buffer according to the
                                        // file.
                shouldShowIsZBuffered = instance.SourceType == SourceType.FlatRedBallType &&
                    (instance.SourceClassType != "Sprite" || instance.SourceClassType != "SpriteFrame");


                #region Remove based off of SourceType

                if (instance.SourceType == SourceType.FlatRedBallType)
                {
                    shouldShowCurrentState = false;
                    shouldIncludeSourceFile = false;
                    shouldIncludeSourceName = false;

                    if (!instance.IsGenericType)
                    {
                        shouldIncludeSourceClassGenericType = false;
                    }
                }
                else if (instance.SourceType == SourceType.File)
                {
                    shouldShowCurrentState = false;
                    shouldIncludeSourceClassType = false;
                    shouldIncludeSourceClassGenericType = false;

                }
                else if (instance.SourceType == SourceType.Entity)
                {
                    shouldIncludeSourceFile = false;
                    shouldIncludeSourceName = false;
                    shouldIncludeSourceClassGenericType = false;



                    shouldShowCurrentState = DetermineIfShouldShowStates(instance);
                }

                #endregion


                if (shouldIncludeSourceClassType)
                {
                    IncludeMember("SourceClassType", typeof(NamedObjectSave), new AvailableClassTypeConverter(instance));
                }
                else
                {
                    ExcludeMember("SourceClassType");
                }

                if (shouldIncludeSourceFile)
                {
                    IncludeMember("SourceFile", typeof(NamedObjectSave), new AvailableFileStringConverter(CurrentElement));
                }
                else
                {
                    ExcludeMember("SourceFile");
                }


                if (shouldIncludeSourceName)
                {
                    IncludeMember("SourceName", typeof(NamedObjectSave), new AvailableNameablesStringConverter(instance));
                }
                else
                {
                    ExcludeMember("SourceName");
                }

                if (shouldIncludeSourceClassGenericType)
                {
                    IncludeMember("SourceClassGenericType", typeof(NamedObjectSave), new AvailableClassGenericTypeConverter());
                }
                else
                {
                    ExcludeMember("SourceClassGenericType");
                }

                if (shouldShowCurrentState)
                {
                    IncludeMember("CurrentState", typeof(NamedObjectSave),
                        new AvailableStates(CurrentNamedObject, CurrentElement, CurrentCustomVariable, CurrentStateSave));
                }
                else
                {
                    ExcludeMember("CurrentState");
                }

                if (!shouldIncludeIncludeInIClickable)
                {
                    ExcludeMember("IncludeInIClickable");
                }
                if (!shouldIncludeIncludeInIVisible)
                {
                    ExcludeMember("IncludeInIVisible");
                }

                if (!shouldShowIsZBuffered)
                {
                    ExcludeMember("IsZBuffered");
                }
                if (!shouldIncludeSetByContainer)
                {
                    ExcludeMember("SetByContainer");
                }
                //else if (this.SourceType == SourceType.SetByParentContainer)
                //{
                //    ExcludeMember("SourceFile");
                //    ExcludeMember("SourceName");
                //    ExcludeMember("SourceClassGenericType");
                //    ExcludeMember("AddToManagers");
                //}
            }
        }
        private static void WriteAttachTo(NamedObjectSave namedObject, ICodeBlock codeBlock, List<string[]> referencedFilesAlreadyUsingFullFile, ReferencedFileSave rfs)
        {

            string objectName = namedObject.FieldName;
            AssetTypeInfo ati = namedObject.GetAssetTypeInfo();

            if ((namedObject.GetContainerType() == ContainerType.Entity && namedObject.AttachToContainer) || namedObject.AttachToCamera)
            {
                bool shouldAttach = true;



                if (ati != null)
                {
                    shouldAttach = ati.ShouldAttach;
                }
                if (namedObject.IsList)
                {
                    shouldAttach = false;
                }

                if (shouldAttach)
                {
                    string whatToAttachTo = "this";

                    if (namedObject.AttachToCamera)
                    {
                        whatToAttachTo = "FlatRedBall.Camera.Main";
                    }


                    // Files (like .scnx) can
                    // contain objects which can
                    // be attached to other objects
                    // in the file.  In some cases this
                    // hierarchy is important.  Therefore,
                    // the generated code will 
                    string attachMethodCall = "AttachTo";
                    bool wrapInIf = true;
                    if (ati != null && !string.IsNullOrEmpty(ati.AttachToNullOnlyMethod))
                    {
                        attachMethodCall = ati.AttachToNullOnlyMethod;
                        wrapInIf = false;
                    }

                    string tabs = "\t\t\t";

                    if (namedObject.ClassType == "SpriteRig")
                    {
                        codeBlock.Line(objectName + ".Root.AttachTo(" + whatToAttachTo + ", true);");

                    }
                    else
                    {
                        var currentBlock = codeBlock;

                        if (wrapInIf)
                        {
                            currentBlock = currentBlock
                                .If(objectName + ".Parent == null");
                        }


                        // March 26, 2012
                        // We used to attach
                        // objects to their parents
                        // by using absolute positions.
                        // The problem is that this means
                        // that if a script ran before the
                        // attachment happened (when a variable
                        // was set) then the script may run before
                        // attachment happened and sometimes after.
                        // That means users would have to handle both
                        // relative and absoltue cases.  We're going to
                        // make attachments happen first so that attachment
                        // happens right away - then the user can always write
                        // scripts using relative values.
                        WriteCopyToAbsoluteInInitializeCode(namedObject, currentBlock, referencedFilesAlreadyUsingFullFile, ati, objectName, rfs);

                        // March 27, 2012
                        // We used to attach
                        // by passing 'true' as
                        // the 2nd argument meaning
                        // that the relative values were
                        // set from absolute.  Now we use
                        // the relative values so that the
                        // script can simply set Relative values
                        // always whether it's before or after attachment
                        // and it'll just work.
                        if (namedObject.AttachToCamera)
                        {
                            string adjustRelativeZLine = null;

                            if (ati != null && !string.IsNullOrEmpty(ati.AdjustRelativeZ))
                            {
                                adjustRelativeZLine = ati.AdjustRelativeZ;
                            }
                            else
                            {
                                adjustRelativeZLine = "this.RelativeZ += value";
                            }

                            adjustRelativeZLine = adjustRelativeZLine.Replace("this", "{0}").Replace("value", "{1}");

                            currentBlock.Line(string.Format(adjustRelativeZLine, objectName, "-40") + ";");
                        }
                        currentBlock.Line(objectName + "." + attachMethodCall + "(" + whatToAttachTo + ", false);");
                    }
                }
            }

        }