fenomas / noa

Experimental voxel game engine.
MIT License
616 stars 91 forks source link

Calling game.pick in the development branch #20

Closed terrac closed 7 years ago

terrac commented 7 years ago

On line 387 in index.js in the development branch it looks like an internal object is being returned from the pick function.

I think it should do something like var result = {} Object.assign({},_hitResult) result.hit = hit return result

Though I am not sure of the best optimized way to do that.

fenomas commented 7 years ago

Hi,

Yes, pick reuses the result object on purpose - you always get back the same object, with new position values. The idea is that some games will need to do lots of picking, and this way the engine doesn't create lots of extra objects to be cleaned up. But this means the client will have to cache any data it wants to save for later.

It could be changed to what you're saying, I guess it's unlikely to measurably affect performance, but anyway that's the thinking behind the current functionality.

terrac commented 7 years ago

That makes sense.

I noticed that when I initially was trying out pick in the console that I wouldn't see the right answer. Maybe it should have the ability to pass in an object so you can keep from creating a bunch of objects, but that would keep someone from accidentally using something that could have been changed.

Thanks

fenomas commented 7 years ago

Thanks, I will leave this on the back burner for now but keep it in mind. There are various places around where the engine passes back objects that it intends to reuse, as a way for me to (hopefully) avoid abusing the GC. But it would be more user friendly to create new objects, and the performance difference may well not be detectable..