Write a Dynamic Loading system

So I have a system I wish to implement,

if player is inside a volume (using player.worldPosition)


if player.worldPosition.x<RoomStartX and  player.worldPosition.x>RoomEndX and  player.worldPosition.y<RoomStartY and  player.worldPosition.y>RoomEndY and  player.worldPosition.z<RoomStartZ and  player.worldPosition.z> RoomEndZ:

    obj.replaceMesh('Room1')

else:
     obj.replaceMesh('EmptyMesh')

however I don’t want to check every room every frame, as it would use a large amount of logic when you get a big level,

is there any way have a function span over x frames?

like do 12% of list in one frame

the next 12% the next frame

and so on?

I was thinking the same system could add/remove lights

To check every object in a volume, use an invisible cube with box bounds set to ‘ghost’, then give it a property that changes when the player collides with it and changes back when not colliding.

Then with Python, iterate over every object just in that space by making use of the hitObjectList property.

So you might have…


hitList = collision.hitObjectList

if ob['active'] == 1:
    ob['tic'] = 0
    
    for objects in hitObjectList:
        objects.replaceMesh(objects['myPhysicalMesh'])
        
else:
    if ob['tic'] == 0:
        for objects in hitObjectList:
            objects.replaceMesh('myEmptyMesh')
        
        ob['tic'] = 1 #because we just want to do one iteration after the player leaves the volume

Also, if you wanted, you could replace the use of an additional ‘tic’ property with the use of states if you needed.

yeah, but then all the cubes would collide, as there will be a “intersection” between each zone boundary,

The next example is ripped our game (it is part of the bigger system). This is only example use only to show how this kind may do. It dont work without inspecting.

How it works:
Every object may have ‘hideat’ distance with is distance from camera where object disappears.
Objects may have ‘lodhi’ and ‘lodlow’ to change simplier or highter mesh and ‘lodlev’ for distance change.
Main (globalDict(‘lodlevel’) is multiplier for distances.

It solves 90 objects per framecount as you see loop in (call it main function).

I hope to show how this kind system may to do.


import bge

scene=bge.logic.getCurrentScene()
vmem=bge.logic.globalDict
obs=scene.objects
lodlevel=int(vmem['detlevel'])
lodlev=1
vmem['lampcount']=0
def leveling(ob):
    try:
        lodlow=ob['lodlow']
        lodhi=ob['lodhi']
        lodlev=0.4
        if 'lodlev' in ob:
            lodlev=ob['lodlev']
        dist=ob.getDistanceTo(obs['Playercam'])

        if lodlev<.1: 
            lodlev=1

        if dist>lodlevel*lodlev:

            ob['lodset']=lodlow
            ob.replaceMesh(lodlow,True)

        else:
            
            ob['lodset']=lodhi
            ob.replaceMesh(lodhi,True)
            if 'animhi' in ob:
                ob.playAction(ob['animhi'],0,ob['animlen'],0,0,5,1)
            if 'personal' in ob:
                ob.playAction(ob['personal'],0,1,1,0,0,1)
                   
    except:
        lodlow=''
        
    if 'hideat' in ob:
        hid=ob['hideat']
        dist=ob.getDistanceTo(obs['Playercam'])
        if dist>hid*lodlevel:
            ob.visible=False
        else:
            ob.visible=True

def main():
    c=0
    while c<90:
        c+=1
        vmem['obj']+=1
        obscount=vmem['obj']            
        #print (obs[obscount].name)
        #print (len(obs))
        if obscount>=len(obs)-1:
            vmem['obj']=0
            print ('LM ',obscount)
            obscount=0
            #vmem['lampcount']=0
        ob=obs[obscount]
    
        leveling(ob)

Here is a simple idea. Define a room’s existence grid system (for the given level), given… your rooms will not move with time.

rooms = {
‘x’: {-1: True, 0: True, 1: False, 2: False, 3: True, 4: True, 5: True},
‘y’: {-1: True, -2: True, 0: True, 1: False, 2: False, 3: False, 4: True, 5: True}
}

here you would have the objects world position instead

character = {
‘x’: 2.3,
‘y’: 1.1
}

Then just check (rooms[‘x’][int(round(character[‘x’]))] and rooms[‘y’][int(round(character[‘y’]))]) to see if the character is in that room… Obviously this isn’t necessarily to be implemented with integer numbers (and the round function), it really depends on how fine your grid needs to be.

edit:
Oh yeah… sorry, this isn’t for spanning the function over x frames, this is just for… low overhead :slight_smile:

If your rooms are axis aligned, you’d only need to check a subset of the rooms as you can discard all rooms with a positional axis value outside the valid range.

Might someone with more programing knowledge help write this?

for module mode?

I can script, and hack, but I am still not yet a code ninja like agoose, or monster or mobius or (everyone else ) or sdfgeoff