0 votes

Every explosion should leave a crater after itself. I click, explosion animation plays, so far so good. I have a sprite (crater) which I want to duplicate every time I explode (click) some place to appear at the place of a click and stay there. How can I do it by a script? Can you please, show me an example? Thank you for your time!

in Engine by (137 points)

1 Answer

0 votes
Best answer

Here's one way...

To use it, make sure you've added an event named click to your input map and associate it with the left-mouse button...

Next, create a separate scene named sprite that contains your crater sprite.

Finally, create a new 2D scene with the following script attached to the root node:

extends Node2D

var sprite = preload("res://sprite.tscn")

func _process(delta):
    if Input.is_action_just_pressed("click"):
        var s = sprite.instance()
        s.position = get_global_mouse_position()

That will create a new instance of the sprite scene (your crater sprite) at every position where the mouse is clicked.

by (10,756 points)
selected by

I highly appreciate your time! I did exactly what you asked, and it returned error on "var s = sprite.instance()" line saying "Nonexistent function ''instance" in base "string"". I am using Godot v3.2.3 Stable.

I think you have an error somewhere. sprite is not a string, but a reference to the sprite scene you should have created.

  • Did you create a scene containing your sprite?
  • Is that scene named sprite?
  • Is it in the root of your project? If not, change the path as needed on the var sprite = ... line of code

Otherwise, if you've done all of the above, maybe post your code? I'm confident that what I did works...

I have CRATER.tscn that contains my crater sprite saved in PREFAB folder.

Then I have LEVEL_01.tscn that contains the main stuff.

I don't have CRATER.tscn instance node in LEVEL_01 scene.

In LEVEL_01 scene I have a script attached to the root node that goes like:

extends Node2D

var Crater = ("res://PREFAB/CRATER.tscn")

func _ready():

func _process(delta):
    if Input.is_action_pressed("MLB"):
        var s = Crater.instance()
        s.position = get_global_mouse_position()

You're missing the preload() call where you define Crater. That should read:

var Crater = preload("res://PREFAB/CRATER.tscn")

I missed "preload", my bad. Works perfectly! Huge thanks!

There's one small problem remaining maybe you have suggestion for too. Now Explosion plays and Crater is instanced every time I click perfectly. But Crater appears on top of the explosion when it needs to be the other way round. No matter where I move my AnimatedSprite (Explosion), the problem remains.

Problem Solved! I created a child node to the root and in the root script I added "getnode(childnodepath)." before "addchild(s)".

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.