Self.ammo set to 50 shows up as nil

go.property("angular_velocity", 5)
go.property("speed", 200)  -- Define a property to control the movement speed of the game object

function init(self)
	msg.post(".", "acquire_input_focus")
	msg.post("camera", "follow")
	self.mouse_x = action.x  -- Update the x-coordinate of the mouse position
	self.mouse_y = action.y  -- Update the y-coordinate of the mouse position
	self.rotate = 0	
	self.ammo = 50
	self.magazines = 5
	print(self.ammo)
end

function update(self, dt)
	print(self.ammo)
	if self.up_pressed then  -- Check if the "up" action is pressed
		local current_pos = go.get_position()
		local screen_center_x = 1280 / 2
		local screen_center_y = 720 / 2
		local offset_x = self.mouse_x - screen_center_x
		local offset_y = self.mouse_y - screen_center_y

		-- Calculate the normalized direction vector
		local direction = vmath.normalize(vmath.vector3(offset_x, offset_y, 0))

		-- Calculate the target position based on the normalized direction and desired speed
		local target_pos = current_pos + direction * self.speed * dt

		go.animate(".", "position", go.PLAYBACK_ONCE_FORWARD, target_pos, go.EASING_LINEAR, 0.004, 0)
		-- Animate the game object's position to the target position using linear easing over 0.004 seconds
	end

	if self.down_pressed then  -- Check if the "up" action is pressed
	local current_pos = go.get_position()
	local screen_center_x = 1280 / 2
	local screen_center_y = 720 / 2
	local offset_x = self.mouse_x - screen_center_x
	local offset_y = self.mouse_y - screen_center_y

	-- Calculate the normalized direction vector
	local direction = vmath.normalize(vmath.vector3(offset_x, offset_y, 0))

	-- Calculate the target position based on the normalized direction and desired speed
	local target_pos = current_pos - direction * self.speed * dt
	print(target_pos)

	go.animate(".", "position", go.PLAYBACK_ONCE_FORWARD, target_pos, go.EASING_LINEAR, 0.004, 0)
	-- Animate the game object's position to the target position using linear easing over 0.004 seconds
end

end

function on_input(self, action_id, action)
	if action_id == hash("up") then
		if action.pressed then
			self.up_pressed = true  -- Set the "up_pressed" flag to true when the "up" action is pressed
		elseif action.released then
			self.up_pressed = false  -- Set the "up_pressed" flag to false when the "up" action is released
			go.cancel_animations(go.get_id())  -- Cancel any ongoing animations on the game object
		end
	end

	if action_id == hash("down") then
		if action.pressed then
			self.down_pressed = true  -- Set the "down_pressed" flag to true when the "up" action is pressed
		elseif action.released then
			self.down_pressed = false  -- Set the "down_pressed" flag to false when the "up" action is released
			go.cancel_animations(go.get_id())  -- Cancel any ongoing animations on the game object
		end
	end

	-- if action_id == hash("left_click") then
	-- 	if action.pressed then
	-- 		-- Only create an enemy if left click is pressed (initial press)
	-- 		enemy_id = factory.create("#bullet_factory", current_pos, rotation)
	-- 		left_click_pressed = true  -- Set the flag to indicate that left click is pressed
	-- 	elseif action.released then
	-- 		-- Reset the flag when left click is released
	-- 		left_click_pressed = false
	-- 	end
	-- end


	if action_id == hash("left_click") then
		print(self.ammo)
		if action.pressed then
			local screen_center = vmath.vector3(1280 / 2, 720 / 2, 0)
			print(screen_center)
			local mouse_position = vmath.vector3(action.x, action.y, 0)

			-- Calculate the vector from the object to the mouse
			local direction = vmath.normalize(mouse_position - screen_center)

			-- Calculate the rotation angle in radians
			local angle = math.atan2(direction.y, direction.x) - math.rad(90)

			-- Convert the angle to degrees and set the rotation
			local rotation_degrees = math.deg(angle)
			print(rotation_degrees)

			-- Convert the rotation in degrees to a quaternion
			local rotation_quat = vmath.quat_rotation_z(math.rad(rotation_degrees))


			-- Only create an enemy if left click is pressed (initial press) and it has enough ammo
			if self.ammo > 0 then
				print(self.ammo)
				self.ammo = self.ammo-1
				enemy_id = factory.create("#bullet_factory", current_pos, rotation_quat)
			end

			--collision.set_group(new_object, hash("enemy"))

			left_click_pressed = true  -- Set the flag to indicate that left click is pressed
		elseif action.released then
			-- Reset the flag when left click is released
			left_click_pressed = false
		end
	end
		
	
		
self.mouse_x = action.x  -- Update the x-coordinate of the mouse position
self.mouse_y = action.y  -- Update the y-coordinate of the mouse position

end

function on_message(self, message_id, message, sender)
	-- Handle collision
	if message_id == hash("contact_point_response") then
		local newpos = go.get_position() + message.normal * message.distance
		go.set_position(newpos)
	end

		-- -- animate the bullet
		-- local distance = 1000                   -- distance in pixels
		-- local speed = 800                       -- pixels per second
		-- local duration = distance / speed       -- time in second to travel the full distance
		-- local to = pos.y + distance
		-- -- start animation and delete bullet when it has reached its destination
		-- go.animate(bullet_id, "position.y", go.PLAYBACK_ONCE_FORWARD, to, go.EASING_LINEAR, duration, 0, function()
		-- 	go.delete(bullet_id)
end

I am mainly interested in:

function init(self)
	msg.post(".", "acquire_input_focus")
	msg.post("camera", "follow")
	self.mouse_x = action.x  -- Update the x-coordinate of the mouse position
	self.mouse_y = action.y  -- Update the y-coordinate of the mouse position
	self.rotate = 0	
	self.ammo = 50
	self.magazines = 5
	print(self.ammo)
end```
i want the player to only be able to shoot when the ammo is above zero but for some reason the ammo is showing up as nil.

I don’t see any error. Which print line is the first one where it shows up as nil?

1 Like

They all show up as nil, even the print statement in the init function

These two lines will throw an error since action doesn’t exist here:

function init(self)
    ...
	self.mouse_x = action.x  -- Update the x-coordinate of the mouse position
	self.mouse_y = action.y  -- Update the y-coordinate of the mouse position
    ...
end

Errors cause the function to stop early, so everything after it (like setting self.ammo to 50) won’t run. Always scroll up in the output console to check for errors!

2 Likes

I tried to run your first script. But it seems like there are things missing from your code, and there’s code outside of any functions.

I get ERROR:SCRIPT: main/ammo.script:7: attempt to index global 'action' (a nil value)

It’s the line self.mouse_x = action.x

After that it prints two nil.

1 Like

Thanks a lot, I’ll give it a go.