0 votes

i'm trying to make a sprite show on the keypress of 1 and then hide when i hit 1 again. i call it a toggle - on/off with the same key.

i worked in director and it used to be a if, else solution, but when i do that in godot, it just turns it on with the 1 keypress but the 2nd press does nothing.

func input(event):
if Input.is

the sprite starts off hidden, and i'm looking for the next part of this code that hides the sprite again with the same key.

asked 6 days ago in Projects by lavinny (12 points)

1 Answer

0 votes

Try this:

func _input(event):
    if event.is_action_pressed("goto.butt"):
        $Butterfly.visible = not $Butterfly.visible

A few comments:

  • When using the _input() function, you are passed an event parameter containing the event that occured. You should test that rather than the Input singleton.
  • I'm assuming that you've added "goto.butt" in the Input Map.
  • I'm using $, which is a shorthand for get_node()
  • Since you just want to toggle visibility, it's most straightforward to use the visible property, which is a Boolean and can therefore be toggled with not.
answered 6 days ago by kidscancode (4,172 points)

that's works for toggling a node, but i have 1 node with about 30 sprites that are .jpg. i want to be able toggle them off or on for which one is needed.

Toggling one sprite on and off is what you asked for.

What do you mean by "one node with about 30 sprites"? How are you deciding which of them are on and off? Do you have 30 different buttons? You'll have to be a lot more specific about what you're actually trying to do.

the code above worked for a control node, but not calling up the image sprite. and i don't use buttons. i use keys. 1-9, q-p, a-l, etc. i know that i can make each image it's own node, but i'm trying to get away from that. i'd prefer to import all my images into 1 control node, and turn the images on/off. but if there is no way to do that, which is surprising, i can just turn each image into it's own node. i'm trying to come up with a more efficient way than that.


what i'm trying to accomplish - press 1, green 10 comes up. press 2, green 20 comes up. press a, green 5 comes up. press w, green 15 comes up. and i have that from 5 to 100, by 5s. this is something i built in adobe director years ago, but that software and all the apps built by it will be dead shortly - not 64bit, so i'm trying to convert what i was doing into code in gdot.

First of all, a Sprite is a Node. The screenshot shows a whole bunch of Sprite nodes. They have a Control node as a parent, but every item in your scene tree is a node.

i got the code to work on the butterfly control node, but it wouldn't work on the butterfly_hd sprite node. the error is - invalid get index "visible" (on base: null instance").

based on my project in the screengrab, i tried visible on my sprite node:

func _input(event):
if event.is_action_pressed("goto.butt"):
    $butterfly_hd.visible = not $butterfly_hd.visible

"null instance" messages happen when your node path is incorrect. When you use get_node() or the $ shortcut, the name must exactly match the path to the node, relative to the script.

In your screenshot above, "butterfly_hd" is a child of "Butterfly", so the correct node reference would be $Butterfly/butterfly_hd. Note that these node paths also appear in the autosuggest popup as you're typing.

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.