How to spawn something at a specific position? (SOLVED)

Great! So, what was the problem? :wink:

Note that the code in the screenshot is incorrect. It should be:

function init(self)
    factory.create("factory#snigel_factory", vmath.vector3(500, 170, 2))
end

Without the )?


It doesn’t work

1 Like

Use vmath.vector3(x,y,z) instead

Funkade ändå inte, ska jag ta bort min karaktär från main.collection först?


This is right, right?

No no no. This is your code:

function init(self)
	local p = go.get_position()
	go.set_position(vmath.vector3(500, 170, 10))
	factory.create("factory#snigel_factory", p())
end

Let’s break it down line by line:

	local p = go.get_position()

The above line gets the position of the current game object and stores it in the variable p. The position is of type vector3.

	go.set_position(vmath.vector3(500, 170, 10))

This line sets the position of the current game object to 500, 170, 10.

	factory.create("factory#snigel_factory", p())

This line creates a game object from the factory with id snigel_factory on the game object with id factory. The second argument should be a vector3 position.

But what actually happens is that you use the variable p from the first line as if it was a function (you “call” the function by the () immediately after it). The variable p contains a value of type vector3. It is not a function and you can’t call it.

If you want to spawn a game object from the snigel_factory on position 500,170,10 you should do as in my first post:

function init(self)
	factory.create("factory#snigel_factory", vmath.vector3(500, 170, 10))
end
3 Likes

Ok, will try soon thank you!

Why don’t you save files?

1 Like

Still doesn’t work

You need to provide more information. For instance just zip your project and upload it here.

But here is an example I wrote for someone else, but maybe it can help you too.
In it you can see how to randomly spawn a game object. Please check how the different parts are setup as that’s probably where something is wrong for you.

example.zip (3.1 KB)

function init(self)
factory.create(“factory#snigel_factory”, vmath.vector3(500, 170, 10))
end
here is the code, it doesnt work

The same code works for me. Have you added the script to the game?

Depending on your render script the z value (the 10) could be outside of your frustrum. Set it to 0 to be sure it’s actually being rendered.

1 Like

There’s more stuff going on - looking at the bottom of the screenshot (why are you taking pictures of your screen, @daniel06061811, and not simply screenshots?) it looks like the sprite buffer is full and the maximum number of collisions is being reached.

I think it would be best if @daniel06061811 took a step back for a bit and had a look at some of the tutorials and examples before trying to make something from scratch. That’s just my suggestion.

5 Likes

Yea sure but I the code is right so I am just trying to figure out what’s wrong. I wasn’t taking screenshot because I was writing to you on the phone.

Right, that line of code is correct now. But it took a lot of time for many people to get you there. I think it’s great that you’re learning and people here are super helpful, but I would really recommend you try and learn how to properly ask a question. It will be really useful, because:

a) It will make it easier for the helpers to understand what you need help with, giving you a better answer; and
b) You might often end up answering the question yourself while preparing the question.

Take a look at these examples:

They are great because they tell a story, and include pictures and detailed descriptions. You should describe your project, what you want to happen, what actually happens, what you have done so far, what you think might be wrong, etc. Include screenshots and code snippets. If that feels like too much effort, then think about the people that have to answer your question by guessing about all of those points.

My suggestion would be to take some time to write out a proper question about your situation, because the people helping you here are still guessing about your issues.

6 Likes

Ok, great!
Thank you for the links and advices!

1 Like

@Alex_8BitSkull is right. We’d love to help you but we don’t have enough information. You need to provide more information about your problem because the line of code to spawn game objects is correct. The problem is elsewhere in your project.

Doesn’t your laptop have an internet connection? Why do you type your questions on your phone and not on the laptop? It’s easier to write a good question if you do it on your laptop and you will be able to provide proper screenshots from the laptop.