CommonLibSSE icon indicating copy to clipboard operation
CommonLibSSE copied to clipboard

RE::TESObjectCELL::ForEachReferenceInRange

Open Elephant42 opened this issue 3 years ago • 1 comments

Hello,

I've been hammering away at an SKSE plugin for a couple of weeks using your excellent library and have managed to get most of what I need working but have struck a brick wall on one feature. Would you have any suggestions on how to use the method RE::TESObjectCELL::ForEachReferenceInRange. I'm trying to get a list of RE::TESObjectREFR* that are within a certain radius of a target Actor.

TIA for any help you care to offer.

Elephant42 avatar Nov 05 '21 10:11 Elephant42

I think this should be return distance <= (a_radius * a_radius) ? since it's comparing to the square of the distance.

	void TESObjectCELL::ForEachReferenceInRange(const NiPoint3& a_origin, float a_radius, std::function<bool(TESObjectREFR&)> a_callback) const
	{
		ForEachReference([&](TESObjectREFR& ref) {
			const auto distance = a_origin.GetSquaredDistance(ref.GetPosition());
			return distance <= a_radius ?
						 a_callback(ref) :
						 true;
		});
	}

It's also possible to optimize by not doing any floating point mult if any of dx/dy/dz are > radius from ref

SteveTownsend avatar Nov 23 '21 16:11 SteveTownsend