How to randomly choose a path2D node and then make it play?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By spicy
:warning: Old Version Published before Godot 3 was released.

Hello everyone.

I’m making a Donkey Kong Clone and for the barrel system I have a Path2D that has a sprite on top of it, it goes wherever the path follows and then loops when it reaches the end.

I’ve also got other Path2D’s that lead to different areas of the game map (so different barrels can take different routes randomly but I don’t know how to do that… which leads to my question)

My question is this, how would I make it so that the game randomly chooses one of these Path2D routes and then plays them? I’d like it to be timed so, something like this:

Every 10 seconds
Randomly choose Path2D
Play randomly selected Path2D node

Does anyone know how would I go about doing something like this?

If the previous Path wasn’t completed within 10 seconds and then the next path randomly starts to play, I wouldn’t want the older one to then disappear because a new one starts to play.

:bust_in_silhouette: Reply From: Freeman

Ad a timer node to your nodes tree. Name it Timer for the purpose of this answer.
Set it to Wait Time 10, Autostart On and e sure not to check One Shot because cause it will count to 10 only once if checked.
Then connect the timer with the timeout() to your script node (in Edit Connection dialog).

You will see

func _on_Timer_timeout(): 

created in the code.

Then add there something like:

var rndvalue = null   #<-- this has to be put at start of the script, where the variables are set.

func _on_Timer_timeout(): 
	randomize()          #<- this resets the seed of the random number generator with a new, different one.
	rndvalue = randi() % 2 #<-- this will select random value 0 or 1 (if you want to have 3 paths for example, you should set it to % 4 etc.)

# and now the time for the if then statements.	
	if rndvalue == 0:
		take path 1   	#<- you have to put real code here

	else :
		take path 2	 #<- you have to put real code here

You can check this example to learn how to work with timer in Godot:

It’s enough to call randomize() only once on start up of the game. The function simply seeds the RNG using the current time, since the game otherwise always starts with the same seed (something like 1234… iirc)

eska | 2016-04-02 22:57

eska is right, so randomize() should be put in func _ready(): instead.

Freeman | 2016-04-02 23:14

Hey.

I haven’t tried your method yet because I’m unable to get my path to play from my main script. Before, I was following this tutorial Godot Pathfinding Tutorial and put my code in the sprite node.

But if I want the random stuff to work which you provided, I’ll need to put all my code together in one script. So I figured I’d use a Node2D for this (which I named BarrelPaths, which also has all my Path2D stuff under it).

I thought something like get_node would work in order to play the selected path2D but it didn’t.

extends Node

func _ready():
	set_fixed_process(true)
	
func _fixed_process(delta):
	get_node("BarrelPaths/BarrelPath/BarrelFollowPath").get_parent().set_offset(get_parent().get_offset() + (100*delta))

Here is an image of my nodes: Image here

Do you know what I’m doing wrong?

EDIT: Never mind, I fixed it. Still working on doing the randomising part though!

spicy | 2016-04-03 14:35