Not certain if this is a bug, but i am getting some very strange behavior.
I have a character script bound to a character game object. The script initializes properly (tested via prints) but the “on input” part seems to not work at all. The scripts init function does make sure to acquire input and no other object in the project ever acquires input.
However, if i hot reload the script the inputs are suddenly working properly, which is the part that i find really weird.
Is there a simple explanation for this that i am just missing?
To clarify a bit:
The script is placed in main.collection/Character_collection/Character_object/character_script.
The update loops of the script does run without the reload.
The on input section doesn’t register any input, despite input being aquired.
On doing a hot reload on the script, it runs perfectly.
Of course! I was hoping someone would be able to spot the problem without me having to show off my ugly coding…
I think this pasted somewhat properly
local move_acceleration = 5
local air_acceleration_factor = 0.8
local max_speed = 10
local gravity = -7
local jump_takeoff_speed = 40
-- pre-hashing ids improves performance
local msg_contact_point_response = hash("contact_point_response")
local msg_animation_done = hash("animation_done")
local group_Ground = hash("Ground")
local group_Walls = hash("Walls")
local group_Breakable = hash("Breakable")
local input_left = hash("Left")
local input_right = hash("Right")
local input_jump = hash("Jump")
local input_rush = hash("Rush")
function init(self)
msg.post(".", "acquire_input_focus")
self.velocity = vmath.vector3(0, 0, 0)
self.correction = vmath.vector3()
self.ground_contact = false
self.move_input = 0
self.anim = nil
self.hit_roof = false
self.face_left = false
self.rushing = false
print("finished init")
end
function update(self, dt)
if self.face_left then
sprite.set_hflip("#sprite", true)
else
sprite.set_hflip("#sprite",false)
end
msg.post("camera","look_at",{ position = go.get_position()})
local target_speed = self.move_input * max_speed
local speed_diff = target_speed - self.velocity.x
local acceleration = vmath.vector3(0, gravity, 0)
if speed_diff ~= 0 then
if speed_diff < 0 then
acceleration.x = -move_acceleration
else
acceleration.x = move_acceleration
end
if not self.ground_contact then
acceleration.x = air_acceleration_factor * acceleration.x
end
end
local dv = acceleration * dt
if math.abs(dv.x) > math.abs(speed_diff) then
dv.x = speed_diff
end
local v0 = self.velocity
self.velocity = self.velocity + dv
if self.rushing then
if self.face_left then
self.velocity.x = -max_speed-3
else
self.velocity.x = max_speed+3
end
end
local dp = (v0 + self.velocity) * dt * 0.5
go.set_position(go.get_position() + dp)
self.correction = vmath.vector3()
self.move_input = 0
self.ground_contact = false
local moving = self.velocity.x == 0
if moving then
msg.post("#sprite", "play_animation", {id = hash("Running")})
moving = false
end
end
local function handle_obstacle_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 == msg_contact_point_response then
if message.group == group_Ground or message.group == group_Walls then
handle_obstacle_contact(self, message.normal, message.distance)
elseif message.group == group_Breakable then
if self.rushing then
print(message.other_id)
msg.post(message.other_id,"destroy_self")
else
handle_obstacle_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
function on_input(self, action_id, action)
if action_id == input_left then
self.move_input = -action.value
self.face_left = true
self.rushing = false
elseif action_id == input_right then
self.move_input = action.value
self.face_left = false
self.rushing = false
elseif action_id == input_jump then
if action.pressed then
jump(self)
elseif action.released then
abort_jump(self)
end
elseif action_id == input_rush then
self.rushing = true
end
end
end
You have mismatching ends in your code. There is a missing end in on_message which causes jump(), abort_jump() and on_input() to be local to the on_message() function…
Yes, same here. That is (mostly) why i didn’t even bother checking through the ends more then skimming. I figured that a hot reload would do nothing in that case, seeing as the script itself would be broken.