Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8541941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:56:41+00:00 2026-06-11T11:56:41+00:00

I’m trying to implement a way to clone elements in my application. These elements

  • 0

I’m trying to implement a way to clone elements in my application. These elements are custom and designed by me (and so far, they aren’t really that complicated). Each item as a property called Children that has many more instances listed inside of it (imagine it as a tree view with multiple sub branches).

I have an object called “Environment” that contained, among other things, a reference to all of these children. Think of it like a orchard that holds all of my trees. When I want to copy everything, I want to be able to just call “clone” on my “Environment” class and have it give me a copy of everything.

It all worked for a while, but after some changes and code refactoring, it doesn’t seem to work anymore and I have no clue as to why. I can follow a stack trace pretty well, but in this case that isn’t possible. Here is a screen capture of my error.

enter image description here

I will also be using serialization for the actual saving my my “Environment” to a file. Before I can do that, however, I have to get serialization in general to work.

Anyways, I’ve checked my individual tree/branch/limb objects individually and they all clone successfully. The only thing I can’t seem to get to clone is the “Environment” class (my orchard). Here is the source of my “Environment” class:

Imports gE.gEngine
Imports LuaInterface
Imports gE.gEngine.Generic
Imports Microsoft.Xna.Framework
Imports System.Runtime.Serialization
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports gE.gEngine.WorldObjects
Imports System.Windows.Forms
Imports Microsoft.Xna.Framework.Input
Imports System.Threading.Tasks
Imports System.Threading

<Serializable()> _
Public Class Environment

    Public World As WorldServices.World
    Public Players As WorldServices.Players
    Public Lighting As WorldServices.Lighting
    Public Environment As WorldServices.Environment
    Public GenericsLibrary As WorldServices.GenericsLibrary

    <NonSerialized()> _
    Public Lua As New LuaInterface.Lua
    <NonSerialized()> _
    Public SafetyContext As CommonEnum.SafetyContext = CommonEnum.SafetyContext.Safe

    Public Event EnvironmentChanged(ByVal sender As Object, ByVal e As GenericEventArgument)

    Public Sub call_EnvironmentChanged(ByVal sender As Object, ByVal e As GenericEventArgument)
        RaiseEvent EnvironmentChanged(sender, e)
    End Sub

    Public Sub New()
        GlobalShare.Environment = Me
        InitializeLua()

        Lighting = New WorldServices.Lighting()
        World = New WorldServices.World()
        Players = New WorldServices.Players()
        Environment = New WorldServices.Environment()
        GenericsLibrary = New WorldServices.GenericsLibrary()

        Dim ConstraintesGroup As New WorldObjects.Group With {
            .Name = "Constraintes",
            .Parent = GenericsLibrary
        }

        Dim ordsc As List(Of gEngine.WorldObjects.Lua) = Environment.GetScripts()
        ordsc.Sort(Function(x, y) x.RunIndex.CompareTo(y.RunIndex))
        For Each script As gEngine.WorldObjects.Lua In ordsc
            GlobalShare.Environment.RunScript(script.Source, script.Name)
        Next
    End Sub

    Public Sub SetSafetyContext(ByVal e As CommonEnum.SafetyContext)
        If e <> CommonEnum.SafetyContext.Unlocked Then
            SafetyContext = e
            Exit Sub
        End If
        Throw New Exception("Attempt to unlock safety context from unsafe Lua script!")
    End Sub

    Public Sub InitializeLua()
        Lua("Game") = GlobalShare.Game
        Lua("__Environment") = Me
        Lua("gel") = New gELua(Me)
        Lua.RegisterFunction("Print", Lua("gel"), Lua("gel").GetType().GetMethod("Print"))
        Lua.RegisterFunction("print", Lua("gel"), Lua("gel").GetType().GetMethod("Print"))
        Lua.RegisterFunction("SetSafetyContext", Lua("gel"), Me.GetType().GetMethod("SetSafetyContext"))

        Lua("Vector2") = New Lua_.Lua_Vector2
        Lua("Vector3") = New Lua_.Lua_Vector3
        Lua("Mouse") = New Lua_.Lua_Mouse

        Lua.NewTable("Helpers")
        Lua("Helpers.EnumHelper") = New Helpers.EnumHelper

        Dim GameTimer As New Stopwatch
        Lua("Time") = GameTimer
        GameTimer.Start()

        Lua.NewTable("__X")
    End Sub

    Public Function Clone() As Environment
        If [Object].ReferenceEquals(Me, Nothing) Then
            Return Nothing
        End If

        Dim formatter As IFormatter = New BinaryFormatter()
        Dim stream As IO.Stream = New MemoryStream()
        Using stream
            formatter.Serialize(stream, Me)
            stream.Seek(0, SeekOrigin.Begin)
            Return DirectCast(formatter.Deserialize(stream), [Environment])
        End Using
    End Function

    Public Sub RunScript(ByVal scr As String, ByVal name As String)
        Try
            Lua.DoString(scr, name)
        Catch ex As Exception
            Game.CallLogEvent(Me, New GenericEventArgument("LogEvent", "Error", ex.Message))
        End Try
    End Sub

End Class

The code is a bit messy because I haven’t messed with this particular class very much because of this particular issue (I want to know why it won’t serialize before I continue to change it around – I don’t want to cause any more issues in the meantime).

According to the error, it appears as if it’s trying to serialize my applications window (StudioWindow), however, I can’t find any references to it in my application.

So I’m not really asking for you guys to find the reference for me (for all I know, it could be hidden deep in some other parts of my project), but I would love to find a way to narrow down the issue. I’ve been commenting parts out and trying to eliminate certain parts of my project as being the problem, but it’s kind of hard because some aspects of my application won’t run without other parts running.

Any help is appreciated. If you would like to know more information, just ask. I can probably get you any information you ask for. My code is mostly VB.NET (although, it’s a hybrid project so many other parts are in C#) so any .NET examples/solutions/help are very much appreciated and equally helpful (I can read both so don’t hesitate).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:56:42+00:00Added an answer on June 11, 2026 at 11:56 am

    You have different options to prevent the serialization of the subscribers. Each of them have advantages or disadvantages. Depending of your architecture and your needs:

    1. Use the attribute for the event. for events there is an additional “field” needed. In C#: [field:NonSerialized].

    2. Set the events to null, to delete all subscriptions before the serialization.

    3. Sometimes it is a good solution to keep all the data in a own element. Keep this data object itself very simple and easy to serialize. If you go this way you don’t have to take care about all fields and delegates you don’t want to serialize

    4. Implement ISerializable

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.