Tutorial Update_animation problem

I have this script in hero:

local gravity = -20

local jump_takeoff_speed = 900

local play_animation, update_animation


function init(self)
    msg.post(".","acquire_input_focus")
    
    self.position = go.get_position()
    
    self.velocity = vmath.vector3(0,0, 0)
    self.ground_contact = false
end
    
 function final(self)
    msg.post(".", "release_input_focus")
end   
    
function update(self, dt)
    local gravity = vmath.vector3(0, gravity, 0)
    
        if not self.ground_contact then
        
            self.velocity = self.velocity + gravity
        end
    
    go.set_position(go.get_position() + self.velocity *dt)
    
    update_animation(self)
    
    self.correction = vmath.vector3()
    self.ground_contact = false


end

local function handle_geometry_contact(self,normal,distance)

    local proj = vmath.dot(self.correction, normal)
    
    local comp = (distance - proj)*normal
    
    self.correction = self.correction + comp
    
    go.set_position(go.get_position() + comp)
    
    if normal.y > 0.7 then
        self.ground_contact = true
    end
    
    proj = vmath.dot(self.velocity, normal)
    
    if proj < 0 then
        self.velocity = self.velocity - proj * normal
    end
end

function on_message(self, message_id, message, sender)
 if message_id == hash("contact_point_response") then
     if message.group == hash("geometry") then
         handle_geometry_contact(self, message.normal, message.distance)
     end
 end
end

local function jump(self)
    if self.ground_contact then
        self.velocity.y = jump_takeoff_speed
    end
end

local function abort_jump(self)
    if self.velocity.y > 0 then
        self.velocity.y = self.velocity.y * 0.5
    end
end

  

local function play_animation(self, anim)
    -- only play animations which are not already playing
    if self.anim ~= anim then
        -- tell the spine model to play the animation
        spine.play("#spinemodel", anim, go.PLAYBACK_LOOP_FORWARD, 0.15)
        -- remember which animation is playing
        self.anim = anim
    end
end
     
local function update_animation(self)
    -- make sure the right animation is playing
    if self.ground_contact then
        play_animation(self, hash("run_right"))
    else
        if self.velocity.y > 0 then
            play_animation(self, hash("jump_right"))
        else
            play_animation(self, hash("fall_right"))
        end
    end
end

And i get this error :
stack traceback:
main/Assets/hero.script:31: in function <main/Assets/hero.script:21>
ERROR:SCRIPT: main/Assets/hero.script:31: attempt to call upvalue ‘update_animation’ (a nil value)

i just don’t get it why no clue. I was thinking maybe no spinemodel on hero but there is spine model, maybe no atlas attached but it is attached so help please :frowning:

At the top you’re declaring the following:

local play_animation, update_animation

I’m assuming that you are trying to forward declare the two functions that you are going to use throughout the code? The thing though is that when you are declaring the update_animation function you aren’t actually assigning it to the update_animation variable declared at the top, but instead redeclare it at the bottom where it’s not visible to the function that are trying to use it. You should have done like this:

update_animation = function(self)
	-- make sure the right animation is playing
	if self.ground_contact then
		play_animation(self, hash("run_right"))
	else
		if self.velocity.y > 0 then
			play_animation(self, hash("jump_right"))
		else
			play_animation(self, hash("fall_right"))
		end
	end
end

You could also just as well have moved play_animation and update_animation to the top of the file.

I changed code as you suggested to this :

local gravity = -20

local jump_takeoff_speed = 900

function init(self)
msg.post(".",“acquire_input_focus”)

self.position = go.get_position()

self.velocity = vmath.vector3(0,0, 0)
self.ground_contact = false

end

local function play_animation(self, anim)
    -- only play animations which are not already playing
    if self.anim ~= anim then
        -- tell the spine model to play the animation
        spine.play("#spinemodel", anim, go.PLAYBACK_LOOP_FORWARD, 0.15)
        -- remember which animation is playing
        self.anim = anim
    end
end
 
local function update_animation(self)
    -- make sure the right animation is playing
    if self.ground_contact then
        play_animation(self, hash("run_right"))
    else
        if self.velocity.y > 0 then
            play_animation(self, hash("jump_right"))
        else
            play_animation(self, hash("fall_right"))
        end
    end
end

function final(self)
msg.post(".", “release_input_focus”)
end

function update(self, dt)
local gravity = vmath.vector3(0, gravity, 0)

    if not self.ground_contact then
    
        self.velocity = self.velocity + gravity
    end

go.set_position(go.get_position() + self.velocity *dt)

update_animation(self)

self.correction = vmath.vector3()
self.ground_contact = false

end

local function handle_geometry_contact(self,normal,distance)

local proj = vmath.dot(self.correction, normal)

local comp = (distance - proj)*normal

self.correction = self.correction + comp

go.set_position(go.get_position() + comp)

if normal.y > 0.7 then
    self.ground_contact = true
end

proj = vmath.dot(self.velocity, normal)

if proj < 0 then
    self.velocity = self.velocity - proj * normal
end

end

function on_message(self, message_id, message, sender)
if message_id == hash(“contact_point_response”) then
if message.group == hash(“geometry”) then
handle_geometry_contact(self, message.normal, message.distance)
end
end
end

local function jump(self)
if self.ground_contact then
self.velocity.y = jump_takeoff_speed
end
end

local function abort_jump(self)
if self.velocity.y > 0 then
self.velocity.y = self.velocity.y * 0.5
end
end

But now i have this error :
WARNING:DLIB: Failed to send announce message (-22)

It’s hard to tell what really is the issue here. The error doesn’t really tell. Anything else in the log? Can you try to rebuild? Does it start at all? Is that the full file?

This is full file . It starts but hero dont respons to player input. I rebuild it. Full logs:
INFO:ENGINE: Defold Engine 1.2.72 (181f1cd)
INFO:ENGINE: Loading data from: build/default
INFO:ENGINE: Initialised sound device ‘default’

INFO:DLIB: SSDP: Started on address 192.168.3.109
WARNING:DLIB: Failed to send announce message (-22)
WARNING:DLIB: Failed to send announce message (-22)
INFO:SOUND: Waiting for OpenAL device to complete
INFO:DLIB: SSDP: Done on address 192.168.3.109

it was working without updateanimation and play animation but when i add them everything gone to hell.

If what you pasted above is the full file then I’m not seeing any on_input() function to handle user input and the jump and abort_jump functions aren’t called.

Thx. I really didn’t see that dissapearing probably i deleted it by mistake thx again:)