0 votes

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


extends KinematicBody2D
func _on_FoodPH_eaten():
food.visible = not get_node("Food").visible


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:
#function for removing the food once all the substance is gone.
#queue causes crash, find fix

func Eaten():
    print("food eaten")

#useless for now
func _ready():

func Eaten():
print("food eaten")


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):
    position = pos
    #add starting animation stuff here

#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
    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")

func depleteHunger(): 
    Hunger -= 1
func get_input():
    velocity.x = 0

    if Input.is_action_pressed("Slide"):
        #if Input.is_action_pressed("right"):

    if Input.is_action_pressed("right"):
        velocity.x += speed

    if Input.is_action_pressed("left"):
        velocity.x -= speed

func _physics_process(delta):
    #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:

        Hunger += 1
    velocity.y += gravity * delta
    velocity = move_and_slide(velocity, Vector2.UP)
    #if not is_on_floor():
    if Input.is_action_just_pressed("jump"):

        if is_on_floor():
            velocity.y = jump_speed

    if should_change_animation:
        should_change_animation = false

#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"):
                    emit_signal ("Eat")
func _on_Player_Eat():
    Hunger += 2
in Engine by (12 points)
edited by

What prints out?

"eating food"+"yum!" are printed out each bite until the substance reaches 0 or less, then it prints out "eating food"+"food eaten"+"yum!" for each bite after that, and it never prints out "signal1"

How are you connecting the signal?

I dont see any code to connect the signal in your "main" code above, if it doesnt connect the signal then it never looks out for it. Have you included all of the code? Also can you highlight the code and select the curly braces in the control buttons to make it viewable properly like this

func example():
    some_code_stuff as properly_formatted

I connected the signal through the engine's UI, and I've added the rest of the code and properly formatted it.

Fantastic news I am glad you managed to solve it!

1 Answer

+1 vote

Okay well I think the simplest explanation is still that the signal isnt connected properly but as a work around you could do the below as the food script to avoid the issue altogether.

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:

#just despawns itself rather than getting another node to do it.    
func Despawn():
by (3,321 points)

when I do this the game crashes once the substance has reached 0 or less, and it sends out the error "E 0:00:11.630 callptr: Attempted method call on a deleted object.
<C++ Source> core/variant
call.cpp:1222 @ call_ptr()
Player.gd:103 @ _input()"

I think I managed to fix it! your answer helped quite a bit, it led me in the right direction. Thank you so much! (is there a way to mark this as solved?)

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 Frequently asked questions and 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 [email protected] with your username.