Why am i not able to see the blocks im placing? (Ursina)

27 Views Asked by At

Im trying to make an sandbox game where you can place blocks but the texture for the Block does not appear and neither does the block. Before i added the second def add_box_wood it all worked just fine.

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController

app = Ursina()
player = FirstPersonController()
Sky()

boxes = []

def add_box(position):
    boxes.append(
        Button(
        parent=scene,
        model="cube",
        origin=0.5,
        color="green",
        position=position,
        texture="grass"
      )
    )

def add_box_wood(position):
    boxes.append(
        Button(
        parent=scene,
        model="cube",
        origin=0.5,
        color="brown",
        position=position,
        texture='texture/wood.jpg'
      )
    )

for x in range(20):
  for y in range(20):
    add_box( (x, -4, y) )

def input(key):
    for box in boxes:
        if box.hovered:
            if key == "right mouse down":
                add_box_wood(box.position + mouse.normal)
            if key == "left mouse down":
                boxes.remove(box)
                destroy(box)


app.run()

I tried to just color it brown but that didnt work.

1

There are 1 best solutions below

0
Alderven On

I noticed few issues:

  • Wood texture is missing. You need to add wood.png image to the script folder
  • You also removing grass by mouse click. Need to add additional condition to check that you're removing only wood
  • You're using wildcard import: from ursina import *. It's a bad practice

Here is the fixed code:

import ursina
from ursina.prefabs.first_person_controller import FirstPersonController

app = ursina.Ursina()
player = FirstPersonController()
ursina.Sky()
boxes = []
for x in range(20):
    for y in range(20):
        boxes.append(ursina.Button(parent=ursina.scene, model='cube', origin=0.5, color='green', position=(x, -4, y), texture='grass'))


def input(key):
    for box in boxes:
        if box.hovered:
            if key == 'right mouse down':
                boxes.append(ursina.Button(parent=ursina.scene, model='cube', origin=0.5, color='brown', position=box.position+ursina.mouse.normal, texture='wood'))
            elif key == 'left mouse down' and box.texture.name == 'wood.png':
                boxes.remove(box)
                ursina.destroy(box)


app.run()

Output:

ursina