+1 vote

Hello peeps,

I am quite new to Godot and am making a 2D Grid based Game kind of like a chess game.
Everything worked quite well until i stomped over a problem i really can't figure out the solution for.

So first of all i am creating a instance and add this as a child to my scene, when i use the left mouse button.
But also creating a dictionary with the intend to get ahold of that object later through the dictionary.

func _input(event):
    if event is InputEventMouseButton:
        match event.button_index:   

                var new_obj = Class.instance()

                var pos = calc_mouse_to_grid()  

                obj_position_dict[pos] = new_obj

But when i want to actually get ahold of the object through the dictionary i dont really know how to call the methods of this object.

            var pos = calc_mouse_to_grid()

            if obj_position_dict.has(pos):

                """place where i want to call the methods
                   of the Class the obj 
                   (accessed through obj_position_dict[pos])
                   belongs to.

                print(obj_position_dict[pos])#testing purpose

For testing purpose i added print(obj_position_dict[pos]),
which prints something like this [Node2D:22091]. I figured, that the number is the individual instance ID of this object, but i didn't jet figured how to use it, because i always get [Node2D:22091] returned and therefore start at the beginning.

I hope someone understands what the problem is and how to solve it or at least has a work around for this.
But i want to have it as dynamic as it can get. So no pre defining arrays or something like that.

Thank you!

in Engine by (13 points)

It's printing its ID Because it's the correct object.
Now obj_position_dict[pos].the_method_you_want_to_call(and_its_arguments).

1 Answer

+1 vote

What you get through obj_position_dict[pos] returns the actual node that you inserted into your dictionary! You just seem to be confused as to how to use that object. To make it more easier to understand:

var node = obj_position_dict[pos]

So the node variable now contains the inserted node. In order to call a method for this node, you just do it the normal way:


else it would be like:


If you're not sure what type of node you inserted before, you can check it like so before calling a method on it:

 if node is Node2D:
    node.look_at(Vector2(10, 10))

You can also ensure that the method you want to call on a node exists:

if node.has_method("my_func"):

or even call by function name (something you might want to do)

 node.call("my_func", params)

though for this case you would actually better of using funcrefs.

by (1,356 points)

Hey Xrayez,

first of all thank you for your quick answer!

Yeah at some point i got kinda confused :/
Sadly, i can’t check if what you suggested would work right away.
It won’t be until In 3 days , that i can actually check this.

Ok, so i checked it and it works perfectly. But first i was very sceptical about your suggestion, because i thought i had already tryed this. And that it didn't work for me originally got me so confused. As it turns out the actual problem was that i spelled my method wrong. resize_sprite() insted ofresize_Sprite() :|

But thank you very much!!

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.