1
\$\begingroup\$

I wrote this script to design countdown in my game:

extends Container

export (int) var minutes = 0
export (int) var seconds = 0
var dsec = 0

func _physics_process(delta):

    if seconds > 0 and dsec <= 0 :
        seconds <= 1
        dsec = 10

    if minutes > 0 and seconds <= 0:
        minutes -= 1
        seconds = 60

    if seconds >= 10:
        $sec.set_text(str(seconds))
    else:
        $sec.set_text("0"+str(seconds))

    if dsec >= 10:
        $dsec.set_text(str(dsec))
    else:
        $dsec.set_text("0"+str(dsec))

    if minutes >= 10:
        $minutes.set_text(str(minutes))
    else:
        $minutes.set_text("0"+str(minutes))

func _on_Timer_timeout():
    dsec -= 1

Here's what the UI section looks like:

UI node hierarchy

When I play I get this error:

Error: attempt to call function 'set_text' in base 'null instance' on a null instance.

Where is the problem with my script?

And I also wrote 00 in the text section of the minutes inspector, 00 in the seconds inspector, and 0 in the dsec inspector.

When I want to move them separately, I can't and they are all stacked on top of each other.

I want to know how I can move them so that 00:00:0 can be seen next to each other?

Stacked text fields

\$\endgroup\$
4
  • 1
    \$\begingroup\$ The picture shows that your don't have a Node with the name minutes, but you try to use it form code $minutes. Similarly you have a Node called min that you don't use. Which leads me to believe this is caused by a typo. \$\endgroup\$
    – Theraot
    Commented Apr 19, 2023 at 15:46
  • \$\begingroup\$ @DMGregory I'm guessing that OP only gets connectivity from a smartphone. \$\endgroup\$
    – Theraot
    Commented Apr 19, 2023 at 16:04
  • \$\begingroup\$ Yes, it is true. Thank you. Now my scene is played, but it shows the seconds, minutes and dsec numbers on top of each other. I tried, it does not allow me to move each one separately. I put a picture of it. \$\endgroup\$
    – Nina Monti
    Commented Apr 19, 2023 at 16:05
  • \$\begingroup\$ I write correctly formatted code from a smartphone daily. The mobile version of the site has all the tools you need to write your code in a way that is legible to others, if you take time to do that. I've cleaned up the formatting this time, and corrected the image that you broke on your previous edit, but please take care in the future to check the post preview and ensure your post reads correctly before clicking the button to post it/save your edits. \$\endgroup\$
    – DMGregory
    Commented Apr 19, 2023 at 16:28

1 Answer 1

1
\$\begingroup\$

Containers control how the children nodes are positioned. The base Container class does nothing special, it simply leaves the children in a default position.

You could try a HBoxContainer if you want a Container that positions the children in a row (Please notice that you would require some control having the ":"). I describe how some of the Containers work elsewhere.

Or if you don't want the Container positioning the children, don't use a Container. Controls that are not Container can also have children, which would be positioned by their anchors and margins. I have an explanation of the different ways things are positioned in Godot elsewhere.

I would have tackled your problem with a single Label and String concatenation. See also GDScript format strings.

\$\endgroup\$
2
  • \$\begingroup\$ I edited everything you said. In the inspector at containe,at variable scripts, I put minutes = 0 and seconds = 60. But when I play, the number of seconds stops at 60 and only the number of the dsec is continuously played from 0 to 10. Seconds do not decrease ... \$\endgroup\$
    – Nina Monti
    Commented Apr 19, 2023 at 18:38
  • 1
    \$\begingroup\$ @NinaMonti You can take a break, you can read your code. I'd say it is probably this: seconds <= 1. \$\endgroup\$
    – Theraot
    Commented Apr 19, 2023 at 19:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .