Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Ethan |
(while I wait to be accepted onto the godot developers forum, thought I would ask)
I want to spawn sprites onto the 2d canvas of my game, but I ALSO want to do this using node names or number names drawn randomly from an external json file–to allow my players to add custom game objects with their own graphics and json edits.
Can anyone recommend how I would go about selecting a random node from the game library as I instance it?
and if that’s even possible by drawing the node’s name based on a string from a json file?
thank you SO much for any ideas or leads!
Don’t know really where to start. What have you done so far?
Random numbers can be got using randi()%20 (or an other value). More information to that here.
Could you save the names and numbers from your json file in a list or a dict or so and then select random items to span them on random locations?
Jowan-Spooner | 2018-12-22 10:54
thanks for the response, Jowan!
and I had figured one way to do it using an array of Node2D scenes/graphics icons:
extends Node2D
var enviroArray =
func _ready():
randomize()
enviroArray.append(preload(“res://Enviro NodeScenes/enviro001.tscn”).instance())
enviroArray.append(preload(“res://Enviro NodeScenes/enviro002.tscn”).instance())
enviroArray.append(preload(“res://Enviro NodeScenes/enviro003.tscn”).instance())
enviroArray.append(preload(“res://Enviro NodeScenes/enviro004.tscn”).instance())
#this will go on to 300 lines, don’t know a quicker way.
func _on_randomEnviroBtn_button_down():
var spriteIcon = enviroArray[randi()% enviroArray.size()]
spriteIcon.position = Vector2(get_viewport().size.x/2, get_viewport().size.y/2)
add_child(spriteIcon)
Ethan | 2018-12-24 19:29