'Python Ursina : destroy an Entity via its coordinates
I'd like to have a function, say destruction(x,y,z) which destroy the Entity placed at coordinates (x,y,z) (if there is one). Of course, the problem is to have a getter which returns the Entity corresponding to some coordinates.
Any idea to do this ?
Solution 1:[1]
Just apply a logic in it
coordinates = "10,10,10"
blocks = ["block1","block2","block3"]
blockspos = ["5,1,5", "10,10,10", "6,1,6"]
for b in blocks:
if blockspos[blocks.index(b)] == coordinates:
print('the block at coordinates is ' + b)
idk how to destroy block via string name but you can use entity.disable or destroy(entity) to destroy an entity so if you have idea to destroy entity via string name so apply these code. Hope this work. Sorry about answer late.
Solution 2:[2]
This is a really elegant solution that I found while trying to fix my own issue:
Here's the code that I have used in my program:
# suppose you are generating lots of entities:
for z in range(16):
for y in range(7):
for x in range(16):
if y >= 6:
#voxel is just an entity
voxel = Voxel(position=(x, y, z), texture=grass_texture)
if x == 8 and y == 6 and z == 8: """just check the coordinates of your entity while generating the entities themselves"""
destroy(voxel)#you can also save the entity in a variable if you don't want to destroy it while generating
elif y >= 4:
voxel = Voxel(position=(x, y, z), texture=dirt_texture)
elif y >= 2:
voxel = Voxel(position=(x, y, z), texture=stone_texture)
else:
voxel = Voxel(position=(x, y, z), texture=deepslate_texture)
but if you want to destroy it by input you can add an input function in the entity class definition like this:
#my entity or button (both count as entities)
class Voxel(Button):
def __init__(self, position=(0, 0, 0), texture=grass_texture):
super().__init__(
parent=scene,
position=position,
model="assets/block",
origin_y=0.5,
texture=texture,
color=color.color(0, 0, random.uniform(0.9, 1)),
scale=0.5,
collider="box",
)
def input(self, key):
if self.hovered:
if key == "left mouse down":
punch_sound.play()
if block_pick == 1:
voxel = Voxel(
position=self.position + mouse.normal, texture=grass_texture
)
if block_pick == 2:
voxel = Voxel(
position=self.position + mouse.normal, texture=stone_texture
)
if block_pick == 3:
voxel = Voxel(
position=self.position + mouse.normal, texture=brick_texture
)
if block_pick == 4:
voxel = Voxel(
position=self.position + mouse.normal, texture=dirt_texture
)
if block_pick == 5:
voxel = Voxel(
position=self.position + mouse.normal, texture=glass_texture
)
if block_pick == 6:
voxel = Voxel(
position=self.position + mouse.normal, texture=deepslate_texture
)
if block_pick == 7:
voxel = Voxel(
position=self.position + mouse.normal, texture=granite_texture
)
if block_pick == 8:
voxel = Voxel(
position=self.position + mouse.normal, texture=log_texture
)
if block_pick == 9:
voxel = Voxel(
position=self.position + mouse.normal, texture=planks_texture
)
#you can follow this example using the self argument to destroy it:
if key == "right mouse down":
punch_sound.play()
destroy(self)
Solution 3:[3]
You could do something like this:
(Keep in mind that the entity you want to destroy must have a collider)
def destruction(position: Vec3):
collider_entity = Entity(
model = “cube”,
collider = “box”,
visible = False,
# The scale of the collider entity must be smaller than the scale of the entity you want to destroy, otherwise the collider entity might detect other entities that you don’t want to destroy. In this case, I’m assuming the entity you want to destroy has a scale of Vec3(1, 1, 1), so I’ll make the collider entity have a scale of Vec3(0.5, 0.5, 0.5). If you want, you can pass the size of the entity you want to destroy as an argument to the function, then set the size of the collider entity to the size of the entity divided by 2.
scale = Vec3(0.5, 0.5, 0.5),
position = position
)
destroy(collider_entity.intersects(ignore = [collider_entity]).entity)
destroy(collider_entity)
Solution 4:[4]
Partial answer : i found a way by using a global dict(), say BLOCKS, of every blocks created by my script :
BLOCKS = dict()
def build_block(x, y, z):
BLOCKS[(x,y,z)] = Voxel(position = (x, y, z), ...)
def destroy_block(x, y, z):
if (x,y,z) in BLOCKS:
destroy(BLOCKS[(x,y,z)])
del(BLOCKS[(x,y,z)])
Still have to find how to destroy blocks created with a left click of the mouse.
Solution 5:[5]
You can use custom style in admin area like this -
add_action('admin_head', 'my_custom_css');
function my_custom_css() {
echo '<style>
#_shipping_field {
color: green;
font-weight: bold;
}
</style>';
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Newbie |
| Solution 2 | Sreeking |
| Solution 3 | |
| Solution 4 | Yves |
| Solution 5 | Siddharth |

