Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Martik |
I’m working on a level generator node. I have a function which performs weighted choosing with given dictionaries based on their key values.
var obstacles = {"obstacle01": 15, "obstacle02": 4, "obstacle03": 1}
So basically my function returns the string “obstacle01” 15 times out of 20. I want to instance scenes based on function’s picks. How can convert these strings to a path variable?
I’m not sure I understand your question. If the strings represent scene resources that already exist in your file system, say:
- res://
- obstacles
- obstacle01.tscn
- obstacle02.tscn
you can construct your paths as a string from the fixed location where they’re saved:
var path = 'res://obstacles/%s.tscn' % obstacle_name
pouing | 2021-10-24 01:16
Yes, that’s exactly what I was asking. Although this method works for me, it takes too much memory to reload the path for each selection. It would be nice to match the outputs of the function with a predetermined constant.
onready var Obstacle01 = preload("res://Resources/Floaters/rock01.tscn")
onready var Obstacle02 = preload("res://Resources/Floaters/rock02.tscn")
onready var Obstacle03 = preload("res://Resources/Floaters/rock03.tscn")
I have these in my hand. But I can’t I can’t perform the conversion from String to variable name. str2var doesn’t work.
Martik | 2021-10-24 10:14