Script problem in my Minecraft-like game.

Hello, first, sorry for my english, im french.
Im making my own minecraft game, but i have a issue with my script, i started python scripting 2 weeks ago and i need your help.

I explain me:

I have a “lastblock” string property on my Camera, and when i look at a block, and i leftclick on mouse, the “lastblock” property take the name of the looked object (Exemple: Bloc-Grass).

But i want if you look another object, or the sky or anything… the last object name in the property “lastblock” , i want my script take his name and set his own “Default” property to his own “Solidite” property. after that, the “lastblock” property can be set to nothing.

I just want make this:

lastblock = se.objects[the string value in “lastblock” property/the block name in my “lastblock” prop]

i try with different way but nothing work.

I paste you the script (i use # for show you what i want):

cible = Ray.hitObject

if own[‘lastblock’] != cible:
print("!= cible")
proplastblock = own[“lastblock”]
print(proplastblock)
#lastblock = se.objects… #HERE IS MY PROBLEM
#lastblock[“Solidite”] = lastblock[“Default”]
#degats.visible = 0
#own[‘lastblock’] = “”

I give you a picture:


If you need any other details, tell me.

I really need your help, please help me.

Have a nice day!

If you are trying to find an object with the same name as stored in the game object property “lastblock”, then


last_block_name = own['last_block']
last_block = own.scene.objects[last_block_name]

However, if the blocks are instanced (using addObject / the add object actuator), then you can’t use names, because they’re not unique.
Instead, you can store the block object reference in the game property, rather than its name, or you can store its Python ID, and use scene.objects.from_id(SOME_ID) to retrieve it.

Thank you agoose77 for your reply, but i dont know if i did it correctly, i give you a picture (There is a issue in python console):


The name of my objects are “Bloc-Grass” or “Bloc-Sand”… it is ok or i have to change their name?

I think i have tu use your second method because all the blocs are edited (terrain generator, when player put a bloc…).

I have 2 other questions:

  1. If i want to reset my string property “lastblock” (Set it empty), does this line is ok:

own[‘lastblock’] = “”

2. I made my “lastblock” property of my camera visible in the bge (you can see on the picture), but when the string property is changed, the property disappear of the Debug property.
I want do this for look what object is in my property in-game, without the python console.

Please help me, thank you and have a nice day :wink:

I made a mistake in my code, which I’ve corrected.
The problem is that when you add objects using the actuator or Python, the added objects all have the same name, so you can’t easily find the correct object.

Instead, save the object reference itself, not its name, and use that directly.


def look_at(cont):
    own = cont.owner

    ray_sensor = cont.sensors['Ray']
    mouse_sensor = cont.sensors['Mouse']

    if not (ray_sensor.positive and mouse_sensor.positive):
        return

    seen_object = ray_sensor.hitObject
    own['last_block'] = seen_object

Here, you can use own[‘last_block’] directly, it’s a KX_GameObject reference.

Thank you agoose, but i think im bad:

How i can save the object reference?

What is this: def look_at(cont):

And why: if not (ray_sensor.positive and mouse_sensor.positive):
and not: if not ray_sensor.positive and mouse_sensor.positive: ?

What is the function of: return ?

Your code seem to be equivalent to the mine:

Ray = cont.sensors[‘Ray’]
clicg = cont.sensors[‘clicg’]

cible = Ray.hitObject
own[‘lastblock’] = cible

Where is the problem?

Have a nice day.

Please help me.