LOD script for Existence

Check out, and critic my LOD script on my next post.

Old post

I am trying to get a meshes name and assign it to a property, I would also like to remove the first 2 letters (ME) but I ran into a couple problems, when I assign the mesh name to the property, the property then disappears from the debug list. Secondly when I try to remove the first 2 letters, it removes all of them, or that’s what it appears to do. I keep track by print

Any help is much appreciated.

Heres the code, and I attached a blend.


###Get stuff
import GameLogic as G

cont = G.getCurrentController()
own = cont.getOwner()
objList = G.getCurrentScene().getObjectList()
scene = GameLogic.getCurrentScene()

############
mesh = own.meshes

own.mesh = mesh
own.object = mesh
    
print own.mesh

Attachments

Mesh Name.blend (143 KB)

import GameLogic as G

cont = G.getCurrentController()
own = cont.getOwner()
objList = G.getCurrentScene().getObjectList()
scene = GameLogic.getCurrentScene()

############
mesh = str(own.meshes[0])

assign to the property "mesh

own.mesh = mesh[2:]

##print
print own.mesh

Thanks man! My LOD script is done!

It is really simple to use, and set up.
I attached a simple blend with the set-up.

Please critic on the script. I would like this as fast as possible, for my game Existence.

EDIT: Simplified the script
EDIT: Simplified the script again, and added an updated version of the blend



### LOD script 0.7#####################
# Credits to Excalaberr and Josip Kladaric
#
#######################################
# Mini tut:
# Each object needs 3 properties
#    1st.  An empty string named mesh
#    2nd.  A float named distance
#    3rd.  A string named replace (this tells the script
#          what to replace the mesh with)
########################################

import GameLogic as G

cont = G.getCurrentController()
own = cont.getOwner()
objList = G.getCurrentScene().getObjectList()
scene = GameLogic.getCurrentScene()

objList = scene.objects

##################################################

# Get the actuators and sensors
rep = cont.actuators['Replace']
once = cont.sensors['once']


# Have no idea what this does!
mesh = str(own.meshes[0])



# Get the camera or object
cam = objList["OBCamera"]


# get the distance
own.distance = own.getDistanceTo(cam)



# assigns the mesh name to property
if once.positive:
    own.mesh = mesh[2:]


### Replace the mesh

if own.distance >= 10:
    rep.mesh = "replace"
    rep.instantReplaceMesh()
    
else:
    rep.mesh = own.mesh
    rep.instantReplaceMesh()

Attachments

LOD.blend (166 KB)LOD Updated.blend (161 KB)

the script looks good and simple , and thanks for the credit. there is one thing you might want to change:
the always sensor that triggers the script has the frequency set to 0. that meens the script is executed 60 times in one second , and you dont need that. you can set it to 30 or 60. i think this will increase the frame rate when you put the script on many objects.
And here a made a example how easy it is to add more levels of detail.

Attachments

LOD edited.blend (189 KB)

Thats pretty cool with the different versions, I tried to make this script as simple as possible so things like that could be added easily

And about the pulse settings that was just an example, I have it set at 120 on my game.

I have one problem with the script, and with a few other LOD scripts I tested, when you apply it to a large amount of objects it freezes for about 1-3 seconds when it updates. Anyway to fix this?

Just tested the latest version of your script, seems to work perfectly!

By the way, it’s just a supposition, but the freezing could be caused by the stress of loading too many objects at the same time. You can implement a small delay between object replacement if the object count exceeds a certain amount, or just manage the placing of objects in your game carefully, and you wouldn’t need to actually rewrite the script.

the freezing is because all always sensors are triggered at the same time. try using near sensors

Have no idea what this does!

mesh = str(own.meshes[0])

that line takes the first mesh from the list of meshes and converts it to a string

My suggestion is that you only replace the mesh if it needs to be replaced. Right now it seems you are replacing the mesh constantly which is why it is slower to run the script than to leave it off. At least for me.

I subdivide the LOD1 a few times and the frame rate difference is:

With script: 43fps
without script: 600fps