Jump to content
ErhanK

Spawning Player

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

Share this post


Link to post

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!

Share this post


Link to post

Of course! Thanks. Because this is a loop and the last element in the list is not "info_player_start". understood :) I should practice more.

  • Haha 1

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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