+1 vote

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 onbodyentered(body: Node) -> void:
queue
free()

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

Godot version v3.2.3
in Engine by (16 points)

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

1 Answer

+1 vote
Best answer
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

by (4,638 points)
selected by

Thanks it works :)

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

Use calldeferred("queuefree") this is just an assumption and may be something else entirely so you can set break points to find the root of that problem

Was using this which caused the game crash:

func onbodyentered(body: Node) -> void:
PlayerData.score += _ready()
queue
free()

Solved when changed to this:

func onbodyentered(body: Node) -> void:
PlayerData.score += score
queue
free()

Thanks a lot for the solution :)

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.
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 webmaster@godotengine.org with your username.