Adam's Lair Forum

game development and casual madness
It is currently 2018/04/25, 07:10

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: 2017/08/18, 21:37 
Newbie
Newbie

Joined: 2017/08/13, 16:30
Posts: 5
Role: Hobbyist
I have a class SharedData whoose purpose is to store information that is shared between scenes. Just a simple class with a property Difficulty that is an enum.

Code:
    public enum Difficulties
    {
        Easy,
        Normal,
        Hard
    }

    public class SharedData
    {
        public Difficulties Difficulty { get; set; }
    }


In the editor, I set the CustomData property in AppData to this class, by clicking the green button with the plus-sign, and choosing my SharedData class from the list.

Then in GameController OnInit-method I try to set the difficulty to a variable like this:

Code:
public class GameController : Component, ICmpUpdatable, ICmpInitializable
    {
        public ContentRef<Scene> MenuScene { get; set; }
        public ContentRef<Scene> GameScene { get; set; }
        public ContentRef<Prefab> EnemyPrefab { get; set; }
        public TextRenderer TextRendererDebug { get; set; }

        private Difficulties difficulty;

        public void OnInit(InitContext context)
        {
            difficulty = ((SharedData)DualityApp.AppData.CustomData).Difficulty;                       
        }


And that last line causes problem for the Editor. It crashes after I rebuild, or I get this error in the log:

Quote:
[Core] Error: Can't load Resource from 'Data\Shmup\Scenes\GameScene.Scene.res', because an error occurred:
InvalidCastException: [A]Shmup.SharedData cannot be cast to [B]Shmup.SharedData. Type A originates from 'GamePlugin.core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' in a byte array. Type B originates from 'GamePlugin.core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' in a byte array.
CallStack:
at Shmup.GameScene.GameController.OnInit(InitContext context) in C:\Users\Andreas\Desktop\Shmups\Shmup\Source\Code\CorePlugin\GameScene\GameController.cs:line 25
at Duality.Resources.Scene.OnLoaded() in c:\projects\duality\Source\Core\Duality\Resources\Scene.cs:line 1128
at Duality.Resource.Init(Resource res) in c:\projects\duality\Source\Core\Duality\Resource.cs:line 445
at Duality.Resource.Load[T](Serializer formatter, String resPath, Action`1 loadCallback, Boolean initResource) in c:\projects\duality\Source\Core\Duality\Resource.cs:line 422


Anyone know what's causing this error? Should I store game global information in some other way?


Top
 Profile  
 
PostPosted: 2017/08/19, 10:01 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2042
Location: Germany
Role: Professional
tufduck01 wrote:
And that last line causes problem for the Editor. It crashes after I rebuild, or I get this error in the log:

Quote:
[Core] Error: Can't load Resource from 'Data\Shmup\Scenes\GameScene.Scene.res', because an error occurred:
InvalidCastException: [A]Shmup.SharedData cannot be cast to [B]Shmup.SharedData. Type A originates from 'GamePlugin.core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' in a byte array. Type B originates from 'GamePlugin.core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' in a byte array.
CallStack:
at Shmup.GameScene.GameController.OnInit(InitContext context) in C:\Users\Andreas\Desktop\Shmups\Shmup\Source\Code\CorePlugin\GameScene\GameController.cs:line 25
at Duality.Resources.Scene.OnLoaded() in c:\projects\duality\Source\Core\Duality\Resources\Scene.cs:line 1128
at Duality.Resource.Init(Resource res) in c:\projects\duality\Source\Core\Duality\Resource.cs:line 445
at Duality.Resource.Load(Serializer formatter, String resPath, Action`1 loadCallback, Boolean initResource) in c:\projects\duality\Source\Core\Duality\Resource.cs:line 422


Anyone know what's causing this error?


This error is a tricky one. When Duality reloads core plugins on compile, it will serialize all the existing objects, discard them, put the old plugin binaries that are still in memory on a blacklist, and reload all the objects with types resolved to the new plugin. If an object someone can make it past the discard-and-reload barrier, that would cause exactly this kind of problem, where there is an invalid cast between two seemingly identical types.

It seems like AppData and UserData are exempt from reload, which is definitely a bug to be fixed on Duality-side, and which is likely the culprit here. I'll take care of this.

tufduck01 wrote:
I have a class [t]SharedData whoose purpose is to store information that is shared between scenes.
tufduck01 wrote:
Should I store game global information in some other way?


In general, the way to go to store information that is shared between scenes is defining a custom Resource to do that. Setting a difficulty level would actually be something to store in UserData, but when it comes to game-related stuff, like game rules or blueprints or things like that, I'd definitely recommend a custom Resource.

Anyway, a custom Resource would also be a workaround for the issue you had ^^

Edit: Should be fixed in Duality 2.12.15 and upwards.

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group