I'm working on a game, and I'm trying to make a eating feature, I got the player's part down for the most part, but the food's script does not want to work it seems. I've tried so many different ways to make the food disappear once it's substance has reached 0 or less. sorry if the code below is hard to read, this is my first time making a game and my first time posting a question. print "signal1" is the only one that doesn't activate at any time, and there are no errors. the(top) main script is attached to the parent node of both the food and player scripts. The (mid)food script is attached to a kinematicbody2D, and the (bottom)player script is attached to another separate kinematicbody2D node.
Please let me know if there is a better way to do this
main
extends KinematicBody2D
func _on_FoodPH_eaten():
print("signal1")
food.visible = not get_node("Food").visible
food.queue_free()
food
extends KinematicBody2D
#variables for substance and freshness, freshness is unused for now.
signal eaten
export (int) var Substance = clamp(10,0,10)
export (int) var Freshness = clamp(10,0,10)
var velocity = Vector2.ZERO
export (int) var gravity = 4000
export (int) var speed = 1200
#function for eating, it lowers substance each bite until the food is eaten
func Eating():
Substance -= 1
print("eating food")
if Substance <= 0:
Eaten()
#function for removing the food once all the substance is gone.
#queue causes crash, find fix
func Eaten():
emit_signal("eaten")
print("food eaten")
#useless for now
func _ready():
pass
func Eaten():
emit_signal("eaten")
print("food eaten")
`
player
extends KinematicBody2D
#signals for different actions to let other scenes/scripts/nodes react
#most unused for now
#WIP signals
signal Eat
#signal Hurt
#signal Roar
#signal Hide
#variables for a bunch of stuff, will organize later on.
var Food = load("res://Food/FoodPH.gd").new()
#var FoodSprite = load("").new()
var should_change_animation = true
var state_machine
var velocity = Vector2.ZERO
export (int) var Hunger = clamp (5,0,10)
export (int) var Limit = clamp (100,0,100)
export (int) var Health = clamp (10,0,10)
export (int) var speed = 1200
export (int) var jump_speed = -1800
export (int) var gravity = 4000
#starting function, sets position and activates movement
# also turns on idle animation
func start(pos):
set_physics_process(true)
position = pos
#add starting animation stuff here
#state_machine.travel("Idle")
#function for dying, works, animation is only a placeholder, ceases movement
#print is only for confirmation, can be removed if needed
func die():
#add dying animation once made
state_machine.travel("Death")
set_physics_process(false)
print("you died")
func _ready():
state_machine = $AnimationTree.get("parameters/playback")
# warning-ignore:return_value_discarded, find a fix soon
$HungerCD.connect("timeout", self, "depleteHunger")
#AnimationPlayer.start
func depleteHunger():
Hunger -= 1
func get_input():
velocity.x = 0
if Input.is_action_pressed("Slide"):
#if Input.is_action_pressed("right"):
state_machine.travel("Slide")
if Input.is_action_pressed("right"):
velocity.x += speed
#state_machine.travel("RightNorm")
else:
state_machine.travel("Idle")
if Input.is_action_pressed("left"):
velocity.x -= speed
#state_machine.travel("LeftNorm")
else:
state_machine.travel("Idle")
func _physics_process(delta):
get_input()
#to check hunger/health value
if Input.is_action_just_pressed("dev"):
print("Hunger = ", Hunger, " Health = ", Health)
#death funcion working SM
if Hunger == 0:
Health -= 1
Hunger += 1
if Health == 0:
die()
Hunger += 1
velocity.y += gravity * delta
velocity = move_and_slide(velocity, Vector2.UP)
#if not is_on_floor():
#state_machine.travel("JumpNorm")
if Input.is_action_just_pressed("jump"):
if is_on_floor():
velocity.y = jump_speed
if should_change_animation:
should_change_animation = false
state_machine.travel("Idle")
#eating mechanic WIP
func _input(event):
if event.is_action_pressed("Bite"):
for body in $Player.get_overlapping_areas():
if body.is_in_group("Food"):
#TestSingle.Eating()
Food.Eating()
emit_signal ("Eat")
func _on_Player_Eat():
Hunger += 2
print("yum!")