Jump to content

Recommended Posts

Posted

Hi. I want to load the player entity position from an object in the editor. It works very well but; I want to spawn in the center of the scene when there is no object named "info_player_start". When I remove the comment lines, it always spawns at the center of the scene. I don't understand why. Here is my code...

#include "Game.h"

Game::Game()
{
	loadMap();
	loadEntities();
	player = new Player(playerStartPosition);
}

Game::~Game()
{
	delete player;
}

void Game::loadMap()
{
	std::string mapname = System::GetProperty("map", "Maps/start.map");
	if (!Map::Load(mapname)) Debug::Error("Failed to load map \"" + mapname + "\".");
}

void Game::loadEntities()
{
	for (const auto e : World::GetCurrent()->entities) 
	{
		if (e->GetKeyValue("name") == "info_player_start") {
			playerStartPosition = e->GetPosition();
		}
		/*else {
			playerStartPosition = Vec3(0.0f, 0.0f, 0.0f);
		}*/
	}
}

void Game::Update()
{
	player->Update();
}

 

Capture.PNG

Posted

Do this:

void Game::loadEntities()
{
	playerStartPosition = Vec3(0.0f, 0.0f, 0.0f);
	for (const auto e : World::GetCurrent()->entities) 
	{
		if (e->GetKeyValue("name") == "info_player_start") {
			playerStartPosition = e->GetPosition();
        		break;
		}
	}
}

And please make sure you understand why this works!

Let's build cool stuff and have fun. :)

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.

×
×
  • Create New...