0 votes

Hello, my doubt now is about range:

I have 3 tilemaps in 3 scenes:
map1.tscn
map2.tscn
map3.tscn

I have a collisionshape at the end of my tilemaps
when my player collides to it, I want to create randomly map1,or 2 or 3.

How do I get it?

in Engine by (35 points)

1 Answer

0 votes

If you're familiar with the method of load and instance, you can do something like:

var alpha = load("res://yourpath/map" + str(randi()%3 + 1) + ".tscn")
var alphainstance = alpha.instance()
add_child(alphainstance)

I hope it's a solution to your problem

by (86 points)

thanks, it worked!!

hello Ells , it's me again

when I write the code in func _ready it woks only three times

I want its works infinitelly

it's an infinity runner.

I create a collision shape (segmented), when my player collides on it
I want it creates ramdonly.

Hi CB

I'm not sure I understand your problem. If I'm right, you want to create a game with kind of the same mechanic as "Jetpack Joyride", which create random scenes/environments every few screens. When working on a game, I would advise you to visualize what you want to do.

(Please keep in mind that it's only how I would do it, but you can make your own better solution).

Visual
(It's through the editor, not though)

I believe that with this way of doing it, your problem of the generation only working sometimes should disappear.

Does it seem right to you?
Btw, if you're discovering the randi()% function, I would advise you to call randomize() at the start of your game, it will create a random set of number for your randi()% to use.

Hello Elis I've tried to do the way you've said

it worked

but each time the scene is created, it increases the velocity

forgive me, my english is very poor

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 Frequently asked questions and 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.