0

I'm making a Platform-Maker and I need to get the position of the mouse, then I place a object in there. Then I need to store it into a array so I can convert it to code later on. Right now I have to store it into a array, but when I do that it Gives me an empty array:enter image description here Edit On Image: This array is suppose to be containing A Vector3 Position where the mouse was clicked.

from ursina import *
from ursina.prefabs.sky import Sky
from ursina.shaders import basic_lighting_shader

app = Ursina(borderless = False)

window.fullscreen = True
window.fps_counter.enabled = False



editor = EditorCamera()
camera.fov = 90
class StartPlatform(Entity):
    def __init__(self):
        super().__init__(
            parent = scene,
            model = "cube",
            position = (0, 0, 0),
            scale = (10, 2, 10),
            collider = "mesh",
            shader = basic_lighting_shader,
            texture = "white_cube",
            color = color.lime
        )

class AddPlatform(Entity):
    def __init__(self, position = (0, 0, 0)):
        super().__init__(
            parent = scene,
            model = "cube",
            position = position,
            scale = (10, 2, 10),
            collider = "mesh",
            shader = basic_lighting_shader,
            texture = "white_cube",
            color = color.azure
        )
def input(key):
    if held_keys["escape"]:
        quit()

def update():
    if held_keys["left mouse"]:
        platform= AddPlatform(position = (mouse.world_point, mouse.world_point, mouse.world_point))
        platforms = []
        for i in platforms:
            platform[i].append(platforms)
        print(platforms)
Sky()
StartPlatform()
app.run()
DonYeet46
  • 13
  • 6

1 Answers1

1

Firt off: py position = (mouse.world_point, mouse.world_point, mouse.world_point) is wrong, it should be

position = mouse.world_point

Secondly, this part doesn't make sense.

platforms = []
for i in platforms:
    platform[i].append(platforms)
print(platforms)

Here's what it does:

platforms = [] Every frame, create a new list called platforms

for i in platforms: loop through every element. However you just set it to be an empty list, so this will just be skipped since there's nothing there.

platform[i].append(platforms) platform is a Entity you instantiated earlier. What is [i] of that? You can't get a member of a an entity that way.

print(platforms) This would return [] since it's not been changed.

If what you want to do is to add the platform you created to a list, you'd define the list outside of the function and add the append the platform to it. You're also using update for this, which gets called every frame. This means you'll probably be creating about 60 platforms every second you're holding the mouse button down. You probably want to use input instead.

platforms = []
def input(key):
    if key == 'left mouse down' and mouse.world_point:
        platform = AddPlatform(position=mouse.world_point)
        platforms.append(platform)
pokepetter
  • 1,383
  • 5
  • 8
  • i still don't see it – DonYeet46 Aug 26 '22 at 12:27
  • ok I see it now, but every click is suppose to be right where the mouse pointer is, when I tried it out, it only let me click on top of the platforms, but they need to be spaced out, for a platformer. side note: i'm gonna try to make this turn into code, so I can copy the positions for my game. – DonYeet46 Aug 26 '22 at 12:32
  • If you're going to use `mouse.world_point`, you'll need a collider for the mouse ray to hit. – pokepetter Aug 26 '22 at 13:40
  • can we use any other ways like ```mouse.position.x``` or something, or can you make the world a collider so we can place it where ever we want. – DonYeet46 Aug 26 '22 at 14:03