Jump to content

Spawning Player


ErhanK
 Share

Recommended Posts

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

Link to comment
Share on other sites

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!

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...