ObjectsInRadius & Instances

RawrRawr REGISTERED, Tester Posts: 506 Seed
edited March 2015 in Code
Hey there,

I'm having a bit of trouble working my way around one of the API functions.
The following code is an example of what I'd like to do.
local gameobjects = EternusEngine.GameObjectsSystem:NKGetGameObjectsInRadius(pos, 1, "all", false)
for objID, objData in pairs(gameobjects) do
	if objData:NKGetName() == "My Object" then
		self.m_reference = objData:NKGetInstance()
	end
end
I'm doing this because I want to be able to re-establish a reference to an object that was previously created but could not be saved between sessions.
When originally creating this reference I passed args.targetObj:NKGetInstance() through an event, so I'm wondering why the call hasn't worked for me this time and/or whether there's a different way that I should be going about this.

In my code, I know the objData:NKGetName() works correctly as my print statements have reproduced the correct object name. So I'm left scratching my head as to why NKGetInstance() didn't...
Programmer, designer, artist.

Comments

  • RawrRawr REGISTERED, Tester Posts: 506 Seed
    UPDATE for v0.8.3:
    Looks like most of my code is still good. The exception being the following function: NKGetGameObjectsInRadius() which is causing a crash each time I load a world with my object. Also, could there be an addition in the API doc's of the match making categories for the function please.

    here's my restore function if it helps someone.
    -------------------------------------------------------------------------------
    function GourdFence:Restore( inData, version )
    	if inData.linked then
    		self.linked = true
    		self.linkedposition = vec3.new(inData.linkedposition.x, 
    			inData.linkedposition.y, 
    			inData.linkedposition.z)
    		local gameobjects = Eternus.GameObjectSystem:NKGetGameObjectsInRadius(linkedposition, 1.0, "all")
    		--for objID, objData in pairs(gameobjects) do
    		--	local objName = objData:NKGetName()
    		--	if objName == "Gourd Fence" then
    		--		self.linkedobject = objData:NKGetInstance()
    		--		return
    		--	end
    		--end
    	end
    end
    -------------------------------------------------------------------------------
    
    Programmer, designer, artist.
  • johnycilohoklajohnycilohokla REGISTERED, Developers Posts: 283 Developer
    edited March 2015
    Not sure what could be causing the crash but you might want to wait for few ticks before you reconnect the objects, to give them time to load.

    Since Eternus.GameObjectSystem:NKGetGameObjectsInRadius is deprecated you might want to use NKPhysics.AABBOverlapCollect, I'm still using NKGetGameObjectsInRadius in SteamPower and it works fine, but I will be replacing it as well.

    To use NKPhysics you will need to include it locally like this: (somewhere at the top of the file)
    local NKPhysics = include("Scripts/Core/NKPhysics.lua")
    

    Then replace the NKGetGameObjectsInRadius with this:
    	--NKPhysics.AABBOverlapCollect(min, max, origin, ignoreObjects)
    	local minOffset = vec3(-0.5)
    	local maxOffset = vec3(0.5)
    	local gameobjects = NKPhysics.AABBOverlapCollect(minOffset , maxOffset, position)
    

    you should also do a nil check on the gameobjects
    	if gameobjects then
    		-- the rest of your code
    	end
    

    If you could zip the crashing one and send it to me I'll check what exactly is crashing it.
    My Blog
    jc.png
      yt.png  t.png  
  • RawrRawr REGISTERED, Tester Posts: 506 Seed
    So AABBOverlapCollect is going off of object positions between min and max spheres?
    Programmer, designer, artist.
  • johnycilohoklajohnycilohokla REGISTERED, Developers Posts: 283 Developer
    Nope, AABB is axis aligned bounding box, so it's a cube.
    The min is the lowest point on all 3 axis. vec3(-0.5,-0.5,-0.5)
    The max is the highest point on all 3 axis. vec3(0.5,0.5,0.5)
    The origin is the center point.
    My Blog
    jc.png
      yt.png  t.png  
  • RawrRawr REGISTERED, Tester Posts: 506 Seed
    min Vec = how far in the negative direction you'd like the box to go.
    max vec = how far in the positive direction you'd like the box to go.
    Position = creates the centre for which the min and max vectors get added to.

    example:
    position = (0,0)
    min vec = (-1, -1)
    max vec = (1, 1)
    AABB, axis aligned bounding box = (picture below)

    All objects within that grey box get added to an array.
    The concept extends into 3D for TUG but if you understand the 2D you've pretty much got the 3D :D
    Programmer, designer, artist.
  • RawrRawr REGISTERED, Tester Posts: 506 Seed
    GOT IT! My objects now save the links between sessions! last steps before I post the mod up:
    1 - make portal and wand models + textures + icons.
    2 - parse in crafting recipes
    Programmer, designer, artist.
Sign In or Register to comment.