Leaving dead grass when swiping grass with a sword

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

i have grass and i can hit it. i also made it collide-able so that you cant just run through it. i wanted it to have a remanence of the grass after i cut it, like on zelda a link to the past so i added this script

func create_dead_grass():
var DeadGrass = load(“res://World/DeadGrass.tscn”)
var deadGrass = DeadGrass.instance()
var world = get_tree().current_scene
world.add_child(deadGrass)
deadGrass.global_position = global_position

now when i cut the grass it leaves the deadgrass node but i appears infront of the player when he walks by it. how do i get it to sit in the background? im guessing it has to do with “deadGrass.global_position = global_position” but im not sure where it should go… i am new to GODOT and game creation. please go easy on me.

this is the entire script for my grass node. extends Node2D

func create_grass_effect():
var GrassEffect = load(“res://Effects/GrassEffect.tscn”)
var grassEffect = GrassEffect.instance()
var world = get_tree().current_scene
world.add_child(grassEffect)
grassEffect.global_position = global_position

func create_dead_grass():
var DeadGrass = load(“res://World/DeadGrass.tscn”)
var deadGrass = DeadGrass.instance()
var world = get_tree().current_scene
world.add_child(deadGrass)
deadGrass.global_position = global_position

func _on_HurtBox_area_entered(_area):
create_grass_effect()
queue_free()
create_dead_grass()

thans for any help

I dont thin k thats it since dead grass does not exist as a scene in this game. i would send a picture but this forum does not allow it i guess??? enter image description here

sCabab1359 | 2020-11-08 20:34

:bust_in_silhouette: Reply From: aipie

It probably has to do with your dead-grass node to be added after the player in the scene.
Nodes are drawn from top to bottom.

If you would look in the remote scene tree (when your game is running), it will be the last in the tree.

Suppose your ‘real’ grass is added to a regular node.
When you put in the ‘dead’ grass, instead of adding it to the ‘world’, add it to that that node instead. This will make sure it is still above the player node.

Redirecting...

i dont think thats it as the deadgeass.tscn is not present in the game until it is pulled up after the grass is “frss”

sCabab1359 | 2020-11-08 20:37

Yes, but you add your deadgrass to the world.
world.addchild(…)

world here is the toplevel node of the current running scene.

I suggest adding your grass to a subnode.
Right now I assume you have
world (node2d)
|_ live-grass (sprite)

Change it to this
world(node2d)
|dummy (node)
|
live-grass (sprite)

Later on instead of doing
var world = gettree().currentscene
world.addchild(grassEffect)

change it to
get_parent().addchild(grasseffect)
This way you add it to dummy instead of world and it should stay ‘after’ your player

aipie | 2020-11-09 19:33

so like this?
extends Node2D

func create_grass_effect():
var GrassEffect = load(“res://Effects/GrassEffect.tscn”)
var grassEffect = GrassEffect.instance()
var world = get_tree().current_scene
world.add_child(grassEffect)
grassEffect.global_position = global_position

func create_dead_grass():
var DeadGrass = load(“res://World/DeadGrass.tscn”)
var deadGrass = DeadGrass.instance()
get_parent().addchild(grasseffect) ++ "addchild*(deadgrass)**"* - didnt work either ++
deadGrass.global_position = global_position
set_deferred(“diasbled”,true)

func _on_HurtBox_area_entered(_area):
create_grass_effect()
queue_free()
create_dead_grass()

all that does is breaks it. the grass is gone from my map.

sCabab1359 | 2020-11-09 22:06

Sorry about mixing the grasseffect and the deadgrass there.
I also noticed now that the queuefree is called before the createdeadgrass there.
You could also create the deadgrass first invisible and then set it visible afterwards

func onHurtBoxareaentered(area):
creategrasseffect()
createdeadgrass()
– you need to store deadgrass in a variable in the createdeadgrass function
deadgrass.visible = false
queuefree()
deadgrass.visible = true

Another solution would be to not call queufree at all, but change the sprite image to your deadgrass. You would also need to stop the hurtbox from working.
I did this sample, but I don’t seem to be able to upload this solution here.
Basically it looks like this in my solution
func _on_Hurtbox_area_entered(_area):
_create_grass_effect()
–queue_free()
hurtbox.queue_free()
sprite.visible = false → has a live grass png
sprite2.visible = true → has a dead grass png and is invisble initially

aipie | 2020-11-09 22:54

this isnt working. either im too dumb for this shit or im too dumb for this shit. i quit

sCabab1359 | 2020-11-09 23:44

Don’t give up yet.

I have uploaded a solution here
It contains a finished project from this series, but I have adjusted the grass.
So you can destroy the grass, an effect is shown and then dead grass is left behind.

aipie | 2020-11-10 20:00

thank you, that is actually the tutorial i am following. i got this up and running with what you linked tome. thank you so much. i am completely new to this. i have been coding (i wouldn’t say coding, more like fumbling), for two entire weeks. So I am very green and still wet behind the ears. i wish i would have gotten into this in my teens instead of my 40s.
It is hard to stay encouraged when you go back to square one over and over and over. do you know of any resources for learning how to code with GD Script? I am a kinesthetic learner, meaning that i learn from hands on. i have a hard time reading a lesson and getting it, so any video links would be greatly appreciated,
thank you again for your help, im going to work your script into my own. appreciate all of your help

sCabab1359 | 2020-11-13 18:11

I’m glad you’re back and that the solution is working out for you.

I’m actually using godot only for a few months now, but I do have 20+ years of coding experience. That makes it a bit difficult to assess what resources would be very helpful for you. This list here are resources I have used to get my knowledge about godot.

The documentation of godot. I actually read the first part completely. I found it well explained. Also the fact that it contains a “your first game” is encouraging for new users as you create have a finished game you can play. But I suggest you do read the first chapter before it.

Create your first platformer is a video tutorial from gdquest that I also did. It is very well explained. Also on their website, there are a lot of focused tutorials.

Heartbeast you have already found. He also has a paid course, but I do not know if this is what you are looking for. It does begin with the very basics (project wise, not code wise (only explained what you need to know to finish a project).

Lastly, kids can code has a lot of focused tutorials. I believe it is both text and video format (I never looked at the videos), but I find it well explained.

These are the resources I used.
Hope they are useful for you too.

aipie | 2020-11-13 20:21