How to get a specific Object through ID and it's methods??

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

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!

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

hinasis | 2018-09-12 07:40

:bust_in_silhouette: Reply From: Xrayez

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)"my_func", params)

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

Hey Xrayez,

first of all thank you for your quick answer!

Yeah at some point i got kinda confused :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.

Faxikon | 2018-09-11 18:09

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() :expressionless:

But thank you very much!!

Faxikon | 2018-09-16 21:46