How to add and delete objects based on distance from the player?

Basically, what I would like to chieve is to add static objects for collisions if the player is close to an empty object and delete those static objects when the player is far away from the empty.
Any reasonable way to code this with python?
I have an idea of how to do it but I fear it wouldn’t be well optimized.

take a look in this tread for ideas.

1 Like

What You could try is. Add armature Bone1 or A, then press shift + D in edit mode to make Bone2 or B and place them in Your desired distance.
Then add a long thin plane from A to B for armature bones.
Shift click mouse2 on Plane and then on the armature. Hit Ctrl+P and select With automatic weights.
Weight paint the plane accordingly to those two armature bones so that in pose mode
once the bone is selected aswell as the plane in weight paint mode it should be red for the selected bone
if weight painting Plane for bone A or Bone1, plane on side of Bone2 or B should be dark Blue and do the same but opposite for Bone2 or B so that for it plane on Bone1 or A is dark blue.

Leave origin point of the armature by A or bone1 or parent it to a cube which is parented to player.
In logic, add actuator as ADD OBJECT and choose TRACK TO
You should set up a big circle if its a flat level enviroment or use a sunshine with dense rays of thin planes
if it is 3D enviroment in which characters jump and fall or climb… parent Cube or Armature on characters bits to level integer property or poperties
once circle or sunshine is colliding with NPC or enemy i guess. ( it could also work as a trigger for
player notifications of their 6th sense if you like depending on what you implement or some skill values or intuition to notify with “Hmm i wonder if i would dig here, would i find anything?” or “I see the last apple on the tree”
Leveled integer should send messages to a cube or parented armature to start tracking while malfunctions could be detected due to movements but hey its better than nothing… while notification buttons can appear for 2 seconds and then slowly fade away to transparency and then just end objects to save ram.
The plane between A and B for armature can be divided with loop cuts by hitting CTRL+R in edit mode like a measuring ruler.
By selecting loop cut vertex lines of ruler you can extrude them up by pressing E once they are all selected, and give every L sheets
a seperate material which can play out or send messages for actions upon collisions for armature with A and B and the Plane stretching between
and all of the materials in them to adjust depending on the setup of Your logic.

I did my best to express myself to help
You with this from what i understand
reading what You’ve wrote.

It’s a bit tricky but it could give You a hell load of ideas towards implementations for AI.
After applying weight paint to plane and loop cutting them with many cuts in between
if you’ll move bone b in distance, gaps between those extruded “ruler marks” will increase or decrease
depending which way you move the Bone B like a stretching rubber which wont rip or
drag on the ground as the distance between them decreases.
This could also work great for project increasing gaps after something like reset
or reaching max amount of levels of the player as a principle of something being pushed through
those ruler marks if player does something once to apply a chance of pushing the object through that ruler
or just trigger animations or any overlay scenes with many different and cool colorful option buttons.
Imagine Echolocation of a flying Bat!

Peace :laughing:

Adding and removing individual objects will likely cost more than you’re saving. Have you considered making simpler collision meshes instead? It’s common to have an invisible object with very few verticies that has collision on, while the detailed object has no collision.

To answer your actual question, you can write a Python script to check the distance to the player and use a boolean property to keep track of having spawned the collision object. You’d also have a script on the object that was spawned which would also check the distance from the player and remove itself if the player got too far away.

You can setup a loading grid using a kdtree.

import bge
from mathutils import kdtree, Vector 

grid = [ ]
for x in range(width):
    for y in range(width) :
       x1 = (-.5*width)*scale + (x*scale) 
       y1 = (-.5*width) *scale + (y*scale)
       Point = (x1, y1, 0)

 Tree = kdtree.KDTree.size(len(grid))
for point in grid:
    Tree.insert(point  ,i)

I can’t debug / test this as I am on a phone atm

The collision meshes are boxes.
My friend is testing this on an older notebook. Without many static objects on the screen, it runs very well. The thing is when there are around 100 of these static box objects, the fps goes down 10+ frames.
Weirdly, it runs 60 fps on my friend’s notebook while using Ubuntu and below 50 fps on Windows.
I was wondering if I can optimize it.
I’ve already done it to create physics objects in a specific place when needed and then delete them afterward if they are not necessary, but not by distance.

Interesting, I don’t know how to really use it but it looks that it could help by calculating the grid.

This looks great. I’ve tried the blend file but I can’t find the object with the properties that use the I’m not entirely sure how it works.

it is on the hemi lamp

Huh!? xD What beautiful arcana is this!? The terain mesh, it is just a quad with a multi res modifier - and yet, somehow it has a defined shape? This file is looking very interesting.

Edit: Hmmm I figured it out, it’s hidden in the sculpt mode! That’s a very interesting approach, I don’t think I would have considered in a million years xD Why did you make it like that?

I did this for my game demo, maybe is not the best aproach but it works, at least for me… hope it helps you out.

There is some blend file in the thread

There is a logic culling / physics culling setting/lod…

the idea where to make LOD easy on the ground planes.

Oh, I see. I didn’t know you could create an object’s properties in-game.
That’s why I wasn’t sure where to find it.

How did you set up the appearance and disappearance of the objects in one of your videos?
The one where it looks like the world is creating while you walk.
is it based on distance?

Yes, everything is based in distances (near sensor)

For this you need a putter object (empty) and the cube (hidden because is going to be added with “add object”) and build something with the empties and try with the main character to see how it works :slight_smile:

In my game demo is a bit more elaborated because I can create the game level in real time by adding cubes whenever I want, and by using logic culling and physics culling I was a ble to manage more than 800 objects.

So I wanted to experiment by replacing the same cube but with another one with no collisions and simpler logic bricks, and it works just nice.

In that video I just deactivate the logic bricks that add the cube… so they just dissapear.

How does the grid work?
I have to set the width and scale myself?
It also gives me this error:

AttributeError: type object 'KDTree' has no attribute 'size'

KD_Tree_example.blend (754.5 KB)

so this builds a grid - and then draws all points in a radius

what you need is to add objects inside a radius if they are not added, and remove them past a certain radius if they are loaded.

“loading bubble”

How does it work?
From what point does it create a grid? From the own object?
Can I specify with this which objects to load?

this is just creating the grid - and accessing it - this is not loading / unloading objects yet.

I will make a better demo one sec