+1 vote

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 getimage() on the fileDialog script file, however, doing the gettree().getroot().getnode("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?

asked Mar 21, 2019 in Engine by Agre (18 points)

(copied from comment to answer)

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!!!

1 Answer

+1 vote

(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.)

answered Mar 24, 2019 by wombatstampede (3,153 points)
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.