0 votes

the root is Swim_demo
Platforms
Area2D
Door
Key
Exit
HUD
COINS/coin1 coin2
Adventure(camer2D)
COCONUTS/coconut

Swim_demo Script
onready var coconuts = 0
onready var hud2 = preload("res://Scene/HUD2.tscn")

func onBluekeybodyentered(body):
$Door/Blue
door.queuefree()
$Key/Blue
key.queue_free()

func onGoldkeybodyentered(body):
$Door/Gold
door.queuefree()
$Key/Gold
key.queuefree()
add
hud()
remove_hud()

func onGreenkeybodyentered(body):
$Door/Green
door.queuefree()
$Key/Green
key.queue_free()

func onOrangekeybodyentered(body):
$Door/Orange
door.queuefree()
$Key/Orange
key.queue_free()

func removehud():
$HUD.queue
free()

func addhud():
var h = hud2.instance()
add
child(h)

coin1 (and coin2) script
signal coin_collected

func oncoinbodyentered(body):
$AnimationPlayer.play("bounce")
emitsignal("coincollected")
setcollisionmask_bit(0, false)

func onAnimationPlayeranimationfinished(animname):
queue
free()

coconut script
signal coconut_collected

func onAnimationPlayeranimationfinished(animname):
queue
free()

func onCoCONUTbodyentered(body):
$AnimationPlayer.play("bounce")
emitsignal("coconutcollected")
setcollisionmask_bit(0, false)

HUD 2 script
var coconuts = 0
var rng = RandomNumberGenerator.new()

func _ready():
$Coconut.text = String(coconuts)

func physicsprocess(delta):
if coconuts == 3:
pass

func oncoconut_collected():
coconuts = coconuts + 1
_ready()

here is what i need help with (please please include code)
so far i have $COCONUTS/coconut1.connect("coconut_collected")

i have tried every way i can think to get to the oncoconut_collected (which is in HUD script)

Godot version 3.4.4 or 3.5.5 or 3.5.1
in Engine by (25 points)

2 Answers

0 votes

You dont seem to have really explained your problem so I am assuming what you are trying to do is get the function on_coconut_collected in the hud script to run when the signal coconut_collected is sent.

If this is correct what you need to do is connect the signal you are emitting in the hud script, you would do it by putting this in the ready function of the hud script

connect("coconut_collected", self, "on_coconut_collected")

what this will do is when the signal is sent the hud script will listen out for it and connect to the function you specify.

by (3,321 points)
0 votes

$COCONUTS/coconut1/connect("coconut_collected", self, "_on_coconut_collected")

In the future please try to summarize your problem in the question title, and make your code easier to read by highlighting it and pressing Ctrl+K or clicking the {} button at the top of the text box.

You're probably better off calling connect inside the coconut script, so that HUD doesn't have to find all the coconuts itself.

connect("coconut_collected", hud, "_on_coconut_collected") where hud is the reference to your HUD node

by (185 points)
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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.