Used common script for duplicated coins, but need to assign different score value for each coin when collected

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

Used common script for different duplicated coins with modified scene name & sprite, need to assign different score values for each coin, how to script it

This is my script:

extends Area2D

onready var animatesprite = $AnimationPlayer
var score

#want it this way:
#if spirt or scene == “citrine”:

score = 5

#elif spirt or scene == “diamond”:

score = 10

#elif spirt or scene == “ruby”

score = 6

func _on_body_entered(body: Node) → void:
queue_free()

Need to script so that respective score gets updated when player collects that respective coin

I’m not really sure what you’re asking but export variables may help.

exuin | 2021-09-04 04:09

:bust_in_silhouette: Reply From: Wakatta
extends Area2D

onready var animatesprite = $AnimationPlayer
var score = 0

func _ready():
    if "Citrine" in name:
        score = 5
   elif "Diamond" in name:
        score = 10
   elif "Ruby" in name:
        score = 6

func onbodyentered(body: Node) -> void:
    queue_free()

Know that when comparing a string this way that it is case sensitive and must exactly match

Thanks it works :slight_smile:

Sunil B | 2021-09-07 13:06

That may be related to the queue_free() if the engine still uses that node when its being freed.

Use call_deferred(“queue_free”) this is just an assumption and may be something else entirely so you can set break points to find the root of that problem

Wakatta | 2021-09-07 13:42

#Was using this which caused the game crash:
func _on_body_entered(body: Node) → void:
PlayerData.score += _ready()
queue_free()

#Solved when changed to this:
func _on_body_entered(body: Node) → void:
PlayerData.score += score
queue_free()

Thanks a lot for the solution :slight_smile:

Sunil B | 2021-09-07 13:48