Jump to content

LoadMap does not load saved in program code map and even remove light


Dreikblack
 Share

Recommended Posts

After save:

image.thumb.png.10b7727763f037b7873e4eb250254f4e.png

After load:

image.thumb.png.859e2d7cdd7737608981a5499bd45b5d.png

#include "UltraEngine.h"

using namespace UltraEngine;

int main(int argc, const char* argv[])
{
    //Get the displays
    auto displays = GetDisplays();

    //Create a window
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);

    //Create a world
    auto world = CreateWorld();

    //Create a framebuffer
    auto framebuffer = CreateFramebuffer(window);

    //Create a camera    
    auto camera = CreateCamera(world);
    camera->SetClearColor(0.125);
    camera->SetPosition(0, 1, -4);

    //Create light
    auto light = CreateBoxLight(world);
    light->SetRange(-10, 10);
    light->SetArea(15, 15);
    light->SetRotation(45, 35, 0);
    light->SetColor(2);

    //Create the ground
    auto ground = CreateBox(world, 20, 1, 20);
    ground->SetPosition(0, -0.5, 0);
    ground->SetColor(0, 1, 0);

    //Create a scene
    auto scene0 = CreateMap();
    scene0->AddEntity(ground);
    scene0->AddEntity(light);
    ground = NULL;
    light = NULL;

    auto scene = CreateMap();
    //Add some boxes
    for (int n = 0; n < 2; ++n)
    {
        auto box = CreateBox(world);
        box->SetColor(0, 0, 1);
        box->SetPosition(Random(-5, 5), Random(5, 10), Random(-5, 5));
        box->SetMass(1);
        scene->AddEntity(box);
    }

    //Main loop
    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        if (window->KeyHit(KEY_A))
        {
            auto box = CreateBox(world);
            box->SetColor(1, 0, 1);
            box->SetPosition(1, 1, 1);
            box->SetMass(1);
            scene->AddEntity(box);
        }
        if (window->KeyHit(KEY_D))
		{
			for (auto box : scene->entities)
			{
                if (box->As<Model>())
                    scene->RemoveEntity(box);
                break;
			}
        }
        if (window->KeyHit(KEY_S))
        {
            //Save the scene to a file
            scene->Save("game.sav");
        }
        //Load the scene when space key is pressed
        if (window->KeyHit(KEY_SPACE))
        {
            scene = LoadMap(world, "game.sav");
        }

        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}

 

Link to comment
Share on other sites

  • 4 months later...
  • 1 month later...

I do not understand what the purpose of scene0 in the above example is. I changed the code and it now works as I would expect.

The scene file format currently does not embed procedurally created model geometry into files. All models must be loaded from a file. This might be something we change in the future, but for now that is part of the design.

#include "UltraEngine.h"

using namespace UltraEngine;

int main(int argc, const char* argv[])
{
    //Get the displays
    auto displays = GetDisplays();

    //Create a window
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);

    //Create a world
    auto world = CreateWorld();

    //Create a framebuffer
    auto framebuffer = CreateFramebuffer(window);

    //Create a camera    
    auto camera = CreateCamera(world);
    camera->SetClearColor(0.125);
    camera->SetPosition(0, 1, -4);

    //Create light
    auto light = CreateBoxLight(world);
    light->SetRange(-10, 10);
    light->SetArea(15, 15);
    light->SetRotation(45, 35, 0);
    light->SetColor(2);

    //Create the ground
    auto ground = CreateBox(world, 20, 1, 20);
    ground->SetPosition(0, -0.5, 0);
    ground->SetColor(0, 1, 0);

    //Create a scene
    auto scene = CreateMap();
    scene->AddEntity(ground);
    scene->AddEntity(light);
    ground = NULL;
    light = NULL;

    //Add some boxes
    for (int n = 0; n < 2; ++n)
    {
        auto box = CreateBox(world);
        box->SetColor(0, 0, 1);
        box->SetPosition(Random(-5, 5), Random(5, 10), Random(-5, 5));
        box->SetMass(1);
        scene->AddEntity(box);
    }

    //Main loop
    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        if (window->KeyHit(KEY_A))
        {
            auto box = CreateBox(world);
            box->SetColor(1, 0, 1);
            box->SetPosition(1, 1, 1);
            box->SetMass(1);
            scene->AddEntity(box);
        }
        if (window->KeyHit(KEY_D))
        {
            for (auto box : scene->entities)
            {
                if (box->As<Model>())
                    scene->RemoveEntity(box);
                break;
            }
        }
        if (window->KeyHit(KEY_S))
        {
            //Save the scene to a file
            scene->Save(GetPath(PATH_DESKTOP) + "/game.sav");
        }
        //Load the scene when space key is pressed
        if (window->KeyHit(KEY_SPACE))
        {
            scene = LoadMap(world, GetPath(PATH_DESKTOP) + "/game.sav");
        }

        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

10 hours ago, Josh said:

I changed the code and it now works as I would expect.

By saving nothing? :D

If i change boxes to brushes, they fall through ground brush but now being saved, but lightning is broken after load.

Before load:

image.thumb.png.cfb74adc2ca50ee3067fc63f14acf2bd.png

After load

image.thumb.png.34e60a3458465fcac025c896e48f90e6.png

 

#include "UltraEngine.h"

using namespace UltraEngine;

int main(int argc, const char* argv[]) {
    auto displays = GetDisplays();
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);
    auto world = CreateWorld();
    auto framebuffer = CreateFramebuffer(window);

    auto camera = CreateCamera(world);
    camera->SetClearColor(0.125);
    camera->SetPosition(0, 15, -8);
    camera->SetRotation(70, 0, 0);

    auto light = CreateBoxLight(world);
    light->SetRange(-10, 10);
    light->SetArea(30, 30);
    light->SetRotation(45, 45, 0);
    light->SetColor(2);

    //Create the ground
    auto ground = CreateBoxBrush(world, 30, 1, 30);
    ground->SetPosition(0, -0.5, 0);
    ground->SetColor(0, 1, 0);

    //Create a scene
    auto scene = CreateMap();
    scene->AddEntity(ground);
    scene->AddEntity(light);
    //ground = NULL;
    light = NULL;

    //Add some boxes
    for (int n = 0; n < 5; ++n) {
        auto box = CreateBoxBrush(world, 0.5, 0.5, 0.5);
        box->SetColor(0, 0, 1);
        box->SetPosition(Random(-10, 10), Random(1, 5), Random(-10, 10));
        //box->SetMass(1);
        scene->AddEntity(box);
    }

    WString filePath = GetPath(PATH_DESKTOP) + "/game.sav";

    //Main loop
    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false) {
        if (window->KeyHit(KEY_A)) {
            auto box = CreateBoxBrush(world, 0.5, 0.5, 0.5);
            box->SetColor(1, 0, 1);
           // box->SetMass(1);
            box->SetPosition(Random(-10, 10), Random(1, 2), Random(-10, 10));
            scene->AddEntity(box);
        }
        if (window->KeyHit(KEY_D)) {
            for (auto box : scene->entities) {
                if (box->As<Brush>() && box != ground) {
                    scene->RemoveEntity(box);
                    break;
                }             
            }
        }
        if (window->KeyHit(KEY_S)) {
            //Save the scene to a file
            scene->Save(filePath);
        }
        if (window->KeyHit(KEY_R)) {
            //Save the scene to a file
            if (scene->Reload(filePath)) {
                Print("Reloaded");
            }           
        }

        //Load the scene when space key is pressed
        if (window->KeyHit(KEY_SPACE)) {
            scene = LoadMap(world, filePath);
        }

        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}

 

  • Sad 1
Link to comment
Share on other sites

I think we probably need a flag or save command that saves the state of objects without resaving the brush and other data. It seems like saving a scene is not exactly like saving a scene...

  • Confused 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

  • 2 weeks later...
On 8/18/2024 at 5:39 PM, Josh said:

I think we probably need a flag or save command that saves the state of objects without resaving the brush and other data. It seems like saving a scene is not exactly like saving a scene...

What I meant to say is that saving a game state is not exactly the same thing as saving a scene. :blink:

  • Haha 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

  • 6 months later...

Coming back to this now. I am testing this code and after I press S and then R, there is no visible change. Is there still an error in this behavior?

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

9 hours ago, Josh said:

Coming back to this now. I am testing this code and after I press S and then R, there is no visible change.

Reload on R. Load on Space. But now in this example scene can't be loaded from saved file O_o 

In console "Error: Failed to load scene..."

  • Thanks 1
Link to comment
Share on other sites

Okay, the LoadScene function doesn't know what a .sav file is, but if you change it to .map the space key will cause the scene to be loaded:

WString filePath = GetPath(PATH_DESKTOP) + "/game.map";

I see the areas outside the box light turn black. It looks like the ambient light level is 0.25 by default, but 0,0,0 is being saved to the scene file:

		"environment": {
			"ambientlight": [
				0.0,
				0.0,
				0.0
			],
			"fog": false,
			"fogangle": [
				0.0,
				15.0
			],
			"fogcolor": [
				1.0,
				1.0,
				1.0,
				1.0
			],
			"fogrange": [
				0.0,
				1000.0
			],
			"gitransmission": 2.0,
			"iblintensity": 1.0,
			"sunangle": [
				0.0,
				0.0
			],
			"suncolor": [
				0.0,
				0.0,
				0.0
			]
		}

image.thumb.png.e845f74b85b889afdfd32dec9b3befff.png

Is that the only thing wrong? Reload should not restore deleted entities, but LoadScene will.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Okay, I need to redo how the editor world settings are stored. The way I did it is a little weird, and it doesn't translate well into game saving / loading...

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...