If Sprite is Touched =... in Codes

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

I’m Stuck please help.

I need the main function of the Game and i don’t know how to do that.
I watched many tutorials tried many things, but i can’t do it.
I need a Touch function. with that function i want to controll the score,time and level up.
I can’t go forway on my Game without that function.
Please explain as simple as possible my English is not the best :confused: with script if possible :confused:

Have you looked at the official documentation? Try to find the documentation in a language you can fully understand. Look on the documentation page here. Scroll down to the “Translations” section on the right side of the page to find a suitable language. Then learn about how scenes are setup to function (the article “Scenes and nodes” describes this). After reading that article, read the articles on scripting (the English articles are here and here).

Ertain | 2020-11-11 16:53

Hello and sorry i’m really bad at telling my problems.

func _process(delta: float) → void:
if Input.is_action_just_pressed(“ui_touch”):
Score += 1
$ScoreCount.text = str(Score)

(this is the script from the Player Scene, The Player is loaded to the Main Scene) but i can’t connect this to the Main Scene where The ScoreCount is.
Is it not possible to connect a Signal or Input from 1 Scene to another?. I tried to connect a Texturebutton before but it wouldn’t let me connect either. So i thought i try it out with Touch input. i hope you can understand my problem.

Rayu | 2020-11-12 10:40

:bust_in_silhouette: Reply From: AndyCampbell

Hi, Yes, you can connect from one Scene to another. There are many ways to do that.

In your case, it might be useful to do this with a signal. This means when you get a ui_touch event you can do your local processing to update the Score and then Emit a ScoreUpdated signal which you can connect to a function in Main Scene to update the score.

More details on signals here

Another way to do that is to have your Player scene reach out and update the Main scene using a nodepath. To do that, Player needs to know the nodepath to reach Main. You can read up about nodepaths in the docs. These can be fragile if you change the design of your node tree, so be careful when designing this :slight_smile:

There is an end to end example of that approach here or another one here

I struggle so so hard man please help,

So i have 4 Players Scenes.
One of them is loaded randomly in the Main Scene.
How do i make that when the Player (RigidBody2D,CollisionShape2D,Sprite) is clicked or touched (i have created “ui_touch” input) that the Score gets + 1 (on the Main Script).
signal my_signal,emit_signal(), i have no idea how to use them.
I tried, of course, but failed very hard.

maybe i can change the Sprite with a TextureButton if its easier.

:confused: Lost

Rayu | 2020-11-12 18:31

Hi Rayu. I don’t have a touch device where I can try this, however this question seems to have a working solution for triggering an action when something is touched

In your project, have you confirmed that your touch event is actually being called? i mean, is your problem only getting that touch event from the Player script to your Main script?

Or are you not even getting a touch event?

AndyCampbell | 2020-11-12 20:39

Thank you for your effort,

I’ve found this tutorial on YT


i searched before but somehow didn’t find that. 2 days later YT recommended it to me

Rayu | 2020-11-14 15:31