Error with Tutorial 3 (Movement) (SOLVED)

Hi all,

I am a complete Newb so please bare with me.

I’m working through the 3rd Tutorial, on Movement. All was well and good until the introduction of Acceleration. I am pretty sure I have followed the instruction well.

The update function looks like this:

function update(self, dt)

  if vmath.length_sqr(self.input) > 1 then
     self.input = vmath.normalize(self.input)
  end

  local acceleration = self.input * 200
  local dv = acceleration * dt
  local v0 = self.velocity

  --error occurs on next line
  local v1 = self.velocity + dv
  --error occurs on previous line

  local movement = (v0 + v1) * dt * 0.5

  local p = go.get_position()
  go.set_position(p + movement)	
  self.velocity = v1
  self.input = vmath.vector3()

end

I get an error message that reads:

ERROR:SCRIPT: main/spaceship.script:15: bad argument #1 to ‘__add’ (vector3 expected, got function)
stack traceback:
[C]:-1: in function __add
main/spaceship.script:15: in function <main/spaceship.script:7>

What am I doing wrong?

Any help will be greatly appreciated.

Thank you all.

Regards

Frank

1 Like

Hello! :waving_hand:
Please post the rest of the code, the init function. There must be some place where you replace the self.velocity with a function definition, which in Lua is possible, as it’s a dynamically typed language. Perhaps you have some typo like missing the brackets:

self.velocity = vmath.vector3

Instead of:

self.velocity = vmath.vector3() -- <-- notice the brackets at the end

This means you’re calling the vector3 function (with no parameters), so you save the result of it in the self.velocity. If you miss the brackets, you are assigning the function definition to the self.velocity, as functions are first-class citizens in Lua, meaning you can assign them to variables:

my_function_callback = function() print("Hello") end
my_function_callback() -- prints "Hello"
2 Likes

Hi all,

Thanks for the reply.

Perfect fix for my problem.

I need to watch out for similar issues in the future.

Thank you.

Regards

Frank

1 Like

Glad it’s solved! :wink: