Jump to content

Model::Copy/Load Crashes after a large mount of entities.


martyj
 Share

Recommended Posts

In Leadwerks 4.4 when running Model::Copy on an entity several times the game will crash.
 

The code:

World* world = World::GetCurrent();
	if (world->terrain == NULL)
	{
		return;
	}

	std::map<std::string, std::string> vegNameEntMap = {
		{ "fallowdeer", "Deer" },
	};

	int vegCount = world->terrain->CountVegetationLayers();
	for (int i = 0; i < vegCount; i++)
	{
		VegetationLayer* layer = world->terrain->GetVegetationLayer(i);
		std::string modelPath = layer->modelpath;

		std::string parentName = "";
		System::Print(modelPath);
		for (auto it = vegNameEntMap.begin(); it != vegNameEntMap.end(); it++)
		{
			if (modelPath.find(it->first) != std::string::npos)
			{
				parentName = it->second;
				break;
			}
		}

		if (parentName.size() == 0)
		{
			continue;
		}

		// Hide Layer
		layer->viewrange = 0;

		Entity* model = world->FindEntity(parentName);
		if (model == NULL)
		{
			continue;
		}

		Vec3 scale = model->GetScale();

		AABB aabb = world->aabb;
		std::vector<Mat4> instances;
		layer->GetInstancesInAABB(aabb, instances);
		System::Print("Loading Layer: " + parentName + " of Size: " + std::to_string(instances.size()));
		for (unsigned int j = 0; j < instances.size(); j++)
		{
			Mat4 instance = instances[j];
			Entity* copy = model->Copy(true, false);
			copy->SetMatrix(instance, true);

			copy->SetScale(scale);
		}
	}

The program seems to consistently crash around 1.7 GB of RAM used.

If I replace the code in the loop with this it also crashes:

Entity* copy = Model::Load(modelPath);
copy->SetMatrix(instance, true);

copy->SetScale(scale);

You can download my project here: (200Mb)

(Removed)

 

I plan to use this code for having wild animals roam my land. Using the vegetation system to place different interactable entities such as Trees or Animals.

 

Link to comment
Share on other sites

Several? ?. From looking at your code it is likely you are running out of memory.

Use Instance() instead of Copy().  The latter will make a new copy of all the mesh data.

Also understand the vegetation system can create millions of instances...which is why it is designed to not store anything in memory.

guess what would happen if I decided I wanted an infinite terrain system?  We already have an infinite vegetation system. ?

  • Like 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

Ah very well could be hitting the memory limit.

I didn't think of the process as being 32 bit only having access to 2.3 Gb of ram.

I'll try Instance instead.

So as far as the vegetation system goes, for these items I'm only using the vegetation system for layout. Instead of manually copying an entity all over my map, I'll use the easy vegetation system to layout my entities.

I'm running a 1024x1024 terrain size.

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