–2 votes

I actually have problem for adding random blocks in my game, I'll will be happy to help me with the code.

Godot version 3.4
in Engine by (10 points)

Let me explain why you are receiving only down votes on this question: Do not expect someone to just provide ready-made code to do what you ask. Instead show some effort. What have you tried so far? Share the code you are currently trying to make add random blocks. Tell us what's working and what's not. Have you tried to implement some random-block-algorithm based on some tutorial on the webs? Which one?

I'm sorry if this comes out too blunt, but you won't be able to make the next Minecraft by asking this kind of questions and gluing the answers together. You'll have to go to the documentation and read how RandomNumberGenerator works in GDScript and then ask questions if/when you get stuck.

Thanks for your tips, I'm sorry It was my first time. I will be edit this and share my code as soon as possible.

1 Answer

+1 vote

You can set a var as your location like

onready var Sprite_position = Vector2()

set your sprite for an instance like

Sprite_inst = preload("res://Sprite.tscn")

and then you can use the randrange to set its positions for example

Sprite_position.x = rand_range(1, 100)
Sprite_position.y = rand_range(1, 100)

create an instance of your sprite like this

Var New_sprite = Sprite_inst.instance()

and then put it in the random position you created with

New_sprite.position = Sprite_position

finally add it to the scene


Hope this helps.

by (1,635 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.