FileDialog on godot 3.1

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Agre

Hello All,

im new in godot,and im having some doubts regarding how to use the got Filedialog node.

My currently objective is to create a texture from an image selected by the file Dialog.

basically, i have the main scene, and i instance the filedialog by script.

onready var caregar_popup = preload('res://Scenes/FileDialog.tscn')
func chamar_pop_up():  
#var caregar_popup = load('res://Scenes/FileDialog.tscn')
var instancia_fileDialog = caregar_popup.instance()
instancia_fileDialog.set_name('FileDialog')
add_child(instancia_fileDialog)
instancia_fileDialog.popup()
pass

Function to convert the image to texture:

func get_image(var imagem): 
var img = Image.new()
var tex = ImageTexture.new()

#----------------------------
var imagem_path = imagem
print("path é ", imagem_path)
img = load(imagem_path)
#----------------------------
tex.create_from_image(img)
self.texture = tex

pass

FileDialog script:

func _on_FileDialog_file_selected(path):
get_tree().get_root().get_node("Background").get_image(path)
pass

Im calling the function get_image() on the fileDialog script file, however, doing the get_tree().get_root().get_node(“node”).function() doesn’t appears to work. Note that the node i’m talking is the parent for the entire application, and it is supposed to be already on the scene.

It basically works on this estructure:

	Background <-node
-> main()
-> Get_image(var imagem)
-> chamar_pop_up()

FileDialog <- Node
->_on_FileDialog_file_selected(path)

I also tried some thing similar to return the string you get from the file dialog an use it on the get_image(), however, it don’t work as well.

Maybe im just doing it wrong, or there is a simple way to do it.

Can someone help me with this one?

(copied from comment to answer)

wombatstampede | 2019-03-22 08:51

This actually worked!

Thanks a LOT!!!

Just one thing i noticed, there is one line that needto be changed on the get_image() function:

This is how it was:

img = load(imagem_path)

This is how it should be:

img.load(imagem_path)

Thanks a lot once again!!!

Agre | 2019-03-23 20:49

:bust_in_silhouette: Reply From: wombatstampede

(copied from comment to answer)
About node adressing.
get_tree().get_root()
gets the root viewport.
If you do get_node("Background") then the root node of the calling scene needs to be called “Background”.

It may be easier to simply pass the node for callback on opening the file dialog. (There are many ways (signals etc.) indeed, this is just one.)
Declare a function my_popup(callnode) the the Filedialog scene script:

var callingNode = null  #global in scene

func my_popup(pCallNode):
  callingNode=pCallNode
  print("fileDialogue debug: called by node"+callingNode.name)
  popup()

func _on_FileDialog_file_selected(path):
  callingNode.get_image(path)

(See also the comment by Agre above.)