Home > Software design >  What causes a check of str == str to loop inside ursina input func?
What causes a check of str == str to loop inside ursina input func?

Time:06-22

Situation

I am checking if mouse is hovering ursina two specific entity inside input function within a <Button> type class. For some reason, when doing left click inside that input checking function, it just loops what is inside key == "left mouse down".

What is suppost to do?

So, it should check if mouse is hovering 2 specific entities and if so, check if user left or right click on it.

Code



    class Voxel(Button):
        def __init__(self, model, position = (0,0,0), texture = blocks[block_id]):
            super().__init__(
                parent=voxel_scene,
                model=model,
                color=color.white,
                highlight_color=color.lime,
                texture=texture,
                position=position,
                origin_y=0.5,
                shader=lit_with_shadows_shader,
                collider="mesh"
            )



        def input(self, key):
            def detect():
                if key == "left mouse down":
                    print(blocks[block_id])
                    model = cube

                    voxel = Voxel(model, position=mouse.point, texture = blocks[block_id])

                    pos = mouse.point
                    game_data.append([model, (pos.x,pos.y,pos.z),blocks[block_id]])
                            
                if key == 'right mouse down':
                    destroy(self)


            if self.hovered:
                detect()
                
            elif str(mouse.hovered_entity) == "world_mesh":
                detect()

Where is the problem?

So, if I remove elif str(mouse.hovered_entity) == "world_mesh": it doesn't loops detect func, otherwise it does. But I need it to check the specified entities.

Thanks, Luís

CodePudding user response:

What do you need to do

Loop can be solved by moving input to outside Voxel. Do isInstance (raycast, Voxel).

Why?

if you click WorldMesh, every Voxel you have will add a new Voxel | pokepetter#2702

What does the code look like?

def input(key):
            if key == 'left mouse down':
                output_value = 0
                hit_info = raycast(camera.world_position, camera.forward)
                if isinstance(hit_info.entity, WorldMesh):
                    model = cube
                    print("Creating Entity - on Voxel")
                    Voxel(model, position=mouse.point, texture = blocks[block_id])
                elif isinstance(hit_info.entity, Voxel):
                    model = cube
                    print("Creating Entity - on Terrain")
                    Voxel(model, position=hit_info.entity.position   hit_info.normal, texture = blocks[block_id])
                    
            if key == 'right mouse down':
                hit_info = raycast(camera.world_position, camera.forward)
                if isinstance(hit_info.entity, Voxel):
                    destroy(hit_info.entity)





    class Voxel(Button):
        def __init__(self, model, position = (0,0,0), texture = blocks[block_id]):
            super().__init__(
                parent=voxel_scene,
                model=model,
                color=color.white,
                highlight_color=color.lime,
                texture=texture,
                position=position,
                origin_y=0.5,
                shader=lit_with_shadows_shader,
                collider="mesh"
            )

Credits

Pokepetter

Lixt

  • Related