0 votes

Hello, how could I create a random scene system that does this?

The scenes would represent the levels.

There must be two types of scenes; the complete and the incomplete. At first all scenes are of the incomplete type.

So there must be a function that randomly selects them.

To then press a button ... call the function with the random scene and load it with ... gettree (). Changescene.
Finally the shit scene must be removed with from the list of incomplete scenes and go to full

I have tried to do this ... with the following code

var imcompletas: Array = [
        "res://Scenas/One.tscn",
        "res://Scenas/Two.tscn"
    ]

var completas: Array = [

    ]

func ob_escena() -> String:
    randomize()
    return imcompletas[int(rand_range(-1, imcompletas.size()))]



func _on_Play_pressed():
    get_tree().change_scene(ob_escena())
    completas.append(ob_escena())
    imcompletas.erase(ob_escena())
    print(completas)
    print(imcompletas)

It works very well ... but the problem is that when printing the complete ones I have incomplete ... errors arise starting with the printing of the scene loaded with
print (ob_escena) .. to know which scene was changed ... the name of another scene is printed and not the name of the loaded scene ... and obviously when printing the complete and incomplete scenes the same arise

asked May 3 in Projects by Yamz (73 points)

1 Answer

0 votes

In your _on_Play_pressed function, you call ob_escena 3 different times, which (potentially) returns 3 different scenes. I assume that's not what you want.

Instead, you probably want something like this:

func _on_Play_pressed():
    var myScene = ob_escena() # get a single, random scene
    get_tree().change_scene(myScene) # change to the selected scene
    completas.append(myScene) # append the selected scene to the complete array
    imcompletas.erase(myScene) # remove the selected scene from the incomplete array
    print(completas)
    print(imcompletas)
answered May 3 by jgodfrey (6,576 points)
edited May 3 by jgodfrey
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.