Forums

OverviewV-Play 1 Support › Change CustomData of JSON Level

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #6861

    wimmer92

    Hello everybody,

     

    I’ve got a simple demo project, where i want to create a json level, load this json level and then modify entries in this level.

    Here is the Code of the main.qml:

     

    import VPlay 1.0
    import QtQuick 1.1
    import Box2D 1.0
    
    GameWindow {
        id: window
    
        Scene
        {
            id:mainScene
            anchors.fill: parent
    
            EntityManager
            {
                id:entityManager
            }
    
            LevelEditor
            {
                id:levelEditor
                applicationJSONLevelsDirectory: "levels/"
                entityManagerItem: entityManager
            }
    
            SimpleButton
            {
                anchors.horizontalCenter: parent.horizontalCenter
                text:"Override existing levelData"
                onClicked:
                {
                   var customValue = 5;
                   levelEditor.saveCurrentLevel({customData: { customValue: customValue}});
                }
            }
    
            SimpleButton
            {
                anchors.left: parent.left
                text:"SAVE"
                onClicked:
                {
                    levelEditor.clearAllLevels();
    
                    var levelId = "1"
                    var levelName ="TestLevel";
    
                    var customValue = 10;
    
                    var saveProperties  = {levelMetaData:{ levelId: levelId, levelName: levelName}, customData: { customValue: customValue}};
    
                    levelEditor.createNewLevel()
                    levelEditor.saveCurrentLevel(saveProperties);
                    levelEditor.exportLevelAsFile();
                }
            }
    
            SimpleButton
            {
                anchors.right: parent.right
                text:"LOAD"
                onClicked:
                {
                    levelEditor.loadSingleLevel(levelEditor.applicationJSONLevels[0]);
    
                    console.log(
                                "LevelId: "+levelEditor.currentLevelData.levelMetaData.levelId+"\n"+
                                "LevelName: "+levelEditor.currentLevelData.levelMetaData.levelName+"\n"+
                                "CustomValue: "+levelEditor.currentLevelData.customData.customValue);
                }
            }
    
            Text
            {
                id:textOfGround
                anchors.centerIn: parent
                text:ground
            }
        }
    }
    

     

    *First of all I export the level to a json file -> This works

    *Then i copy this json file into my levels-folder and the load the level and log all the informations -> This works

     

    Now i want to change the value of a custom value by calling:

    levelEditor.saveCurrentLevel({customData: { customValue: customValue}});

    …where customValue have been changed now.

     

    My question now: How is it possible to change the customData of a .json Level at runtime ?

     

    Greets,

    Thomas

    #6891

    Christian
    V-Play Team

    Hi Thomas,

    you can either extend your level from LevelBase and use levelData.customData.

    Or you can create your own property variant customData and set this one when you are loading the level, and when you are calling the saveLevel you provide the value of this property to the saveLevel() function.

    Cheers, Chris

    #6910

    wimmer92

    Thanks for your Answer Chris !,

     

    in my case i don’t need a Levelbase. I basically just want to override an existing entry in a json level with a new one.

    For Example:

     

    I have a json level called TestLevel.json which looks like: {“entityData”:{},”editableComponentData”:{},”levelMetaData”:…..,”customData”:{“customValue”:10}}

    Now i want to override the value of customValue by calling with 5 so that my TestLevel.json looks like the following after this call:

    {“entityData”:{},”editableComponentData”:{},”levelMetaData”:…..,”customData”:{“customValue”:5}}

     

    Is there a way to that ? Cause the following code, just overrides the authorgenerated levels.

    levelEditor.saveCurrentLevel({customData: { customValue: customValue}});

     

    For authorgenerated levels this works perfectly, but I want to override a real entry in my json file

     

    Cheers,

    Thomas

    #6913

    Christian
    V-Play Team

    Please see here:

    Christian said:
    Or you can create your own property variant customData and set this one when you are loading the level, and when you are calling the saveLevel you provide the value of this property to the saveLevel() function.

     

    #6926

    wimmer92
    import VPlay 1.0
    import QtQuick 1.1
    
    GameWindow {
        activeScene: scene
        
        Scene {
            id: scene
    
            property variant customData;
    
            EntityManager
            {
                id:entityManager
            }
    
            LevelEditor
            {
                id:levelEditor
                applicationJSONLevelsDirectory: "levels/"
                entityManagerItem: entityManager
            }
    
            SimpleButton
            {
                anchors.centerIn: parent
                text:"DO IT"
                onClicked:
                {
                    //Load the json level from the levels folder
                    levelEditor.loadSingleLevel(levelEditor.applicationJSONLevels[0]);
    
                    //Save the customData in the property
                    scene.customData = levelEditor.currentLevelData.customData;
    
                    //Print actual value of customData
                    console.log(scene.customData.customValue)
    
                    //Modify customData
                    var newValue = 1234567890;
                    scene.customData = {"customValue":newValue};
    
                    //Print actual value of customData
                    console.log(scene.customData.customValue)
    
                    var saveProperties = { levelMetaData: levelEditor.currentLevelData.levelMetaData, customData: scene.customData };
                    levelEditor.saveCurrentLevel(saveProperties);
    
                    //The json file haven't changed now !
                }
            }
        }
    }
    

    My .json File:

    {“levelMetaData”:{“levelId”:”1″,”levelName”:”TestLevel”,”storageLocation”:”applicationJSON”},”customData”:{“customValue”:10}}

     

    The value called “customValue” does not change in the file, but in the authorgenerated level it does.

     

    What I’m doing wrong ?

    #6929

    Christian
    V-Play Team

    Ahh, now I see what you are trying to do. 😉

    A call of saveLevel() only modifies the authorGeneratedLevels storage. If you want to store your new level as a JSON file, call exportLevelAsFile(). You can also find this explained in the doc here.

    Cheers, Chris

Viewing 6 posts - 1 through 6 (of 6 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support