Attempt to perform arithmetic on field (a nil value)? (SOLVED)

function init(self)
	-- Speed of the player ship
	local speed = 250

	-- Input bindings
	local move_left = hash("left")
	local move_right = hash("right")
	local fire = hash("fire")

	-- Animation sets
	local animation_idle = hash("forward")
	local animation_left = hash("bank_left")
	local animation_right = hash("bank_right")

	--missile
	local missile = hash("/missile")

end

function final(self)
	msg.post(".", "acquire_input_focus")
	self.direction = vmath.vector3()
	self.current_animation = nil
	self.can_fire = true
end

function update(self, dt)
	-- update player position
	local pos = go.get_position()

	-- prevent the ship leaving the screen
	if pos.x < 25 then
		pos.x = 25
	end
	if pos.x > 935 then
		pos.x = 935
	end

	-- set new position
	go.set_position(pos + self.direction * speed * dt)

	-- animate the ship
	local animation = animation_idle
	if self.direction.x > 0 then
		animation = animation_right
	elseif self.direction.x < 0 then
		animation = animation_left
	end

	-- reset animation only if new direction started
	if animation ~= self.current_animation then
		msg.post("#sprite", "play_animation", { id = animation })
		self.current_animation = animation
	end

	-- only continue to move the ship if the key is held
	self.direction.x = 0
end

function on_input(self, action_id, action)
	-- Set direction based on input
	if action_id == move_left then
		self.direction.x = -1
	elseif action_id == move_right then
		self.direction.x = 1
	end
end

There’s an example of what the code looks like and this is the error message

ERROR:SCRIPT: main/player1.script:42: attempt to perform arithmetic on field ‘direction’ (a nil value)
stack traceback:
main/player1.script:42: in function <main/player1.script:29>

Hi and welcome. Not sure what went wrong with your code formatting. Wrap it with three ` in the beginning and end.

self.direction = vmath.vector3()

You define the direction as blank, so in update() before there is input you will be trying to multiply a nil value. Try defining it with 0’s instead.

self.direction = vmath.vector3(0,0,0)
function final(self)
	msg.post(".", "acquire_input_focus")
	self.direction = vmath.vector3(0,0,0)
	self.current_animation = nil
	self.can_fire = true
end

Just tried it, and it gave me this

ERROR:SCRIPT: main/player1.script:40: attempt to perform arithmetic on field ‘direction’ (a nil value)
stack traceback:
main/player1.script:40: in function <main/player1.script:27>

Oh, that stuff should be in init() and not final().

so init() should look like this instead?

function init(self)
	-- Speed of the player ship
	local speed = 250

	-- Input bindings
	local move_left = hash("left")
	local move_right = hash("right")
	local fire = hash("fire")

	-- Animation sets
	local animation_idle = hash("forward")
	local animation_left = hash("bank_left")
	local animation_right = hash("bank_right")

	--missile
	local missile = hash("/missile")

	self.direction = vmath.vector3(0,0,0)
	self.current_animation = nil
	self.can_fire = true
end

Yes.

ERROR:SCRIPT: main/player1.script:41: bad argument #2 to ‘__mul’ (number expected, got nil)
stack traceback:
[C]:-1: in function __mul
main/player1.script:41: in function <main/player1.script:28>

I got this :frowning:

Oh my god i’m so sorry I completely misread a section of a tutorial i’m following, I’ve sorted it now

2 Likes

That should not matter. If you don’t provide a value I believe we set it to 0.

1 Like

You’re defining a bunch of local variables in your init function. All of those are local to the init function, so they won’t be accessible in other functions. If those are going to stay the same for all players, then you should just move them outside of init() to the base level of the script. Things that may change for each instance should be put on ‘self’.

2 Likes