Question about using instanced functions in other functions

Godot Version

4.2.1 stable

Question

Hey all, so I have a state machine to handle player mechanics and movement, i added an attack state which needs to get the position of the mouse to aim but when i tried to add a variable which uses get_viewport().get_mouse_position() it was returning values that were not useful so I tried to use get_global_mouse_position() and it said it was not found in the base shelf, so to solve this I defined that variable in the player script which all state scripts have access to, and it works perfectly but the concern I have is that it is checking the position of the mouse every frame since i ran it in the physics process, so my question is that is there a way to have this only run when the attack script is called. I am assuming i can create a canvasitem or layer instance within the attack script but im not sure, thank you in advance

If the nodes have access to the player script, why not call player.get_global_mouse_position() from the attack script directly when you need it?

To be fair, though, this is such a tiny added computation per frame that the way you currently have it set up should be fine, too.

In any case, it would be computationally cheaper to access the player script than to keep an extra CanvasItem node in the scene just for this purpose.

1 Like

I can’t believe I didn’t try player.get_global_mouse_position(), it works perfectly thank you, though now I’ll probably have to have the calculations every frame anyways because I think I’m going to add an arm that constantly rotates towards cursor position lol

1 Like