War battles tutorial help (SOLVED)

Im having troubles with the war battles tutorial. I can’t get the rocket to fire and when i press my fire key the player sprite will keep moving in one direction. can I get some help?

heres my player.script

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

	self.moving = false
	self.firing = false                     -- [1]

	self.input = vmath.vector3()
	self.dir = vmath.vector3(0, 1, 0)
	self.speed = 50
end

function final(self)                                -- [7]
	msg.post(".", "release_input_focus")            -- [8]
end

function update(self, dt)
	if self.moving then
		local pos = go.get_position()
		pos = pos + self.dir * self.speed * dt
		go.set_position(pos)
	end

	if self.firing then
		local angle = math.atan2(self.dir.y, self.dir.x)    -- [1]
		local rot = vmath.quat_rotation_z(angle)            -- [2]
		local props = { dir = self.dir }                    -- [3]
		factory.create("#rocketfactory", nil, rot, props)   -- [4]
	end

	self.input.x = 0
	self.input.y = 0

	self.moving = false
	self.firing = false                     -- [2]
end

function on_input(self, action_id, action)          -- [14]
	if action_id == hash("up") then
		self.input.y = 1                            -- [15]
	elseif action_id == hash("down") then
		self.input.y = -1
	elseif action_id == hash("left") then
		self.input.x = -1
	elseif action_id == hash("right") then
		self.input.x = 1
	elseif action_id == hash("fire") and action.pressed then
		self.firing = true
	end

	if vmath.length(self.input) > 0 then
		self.moving = true                          -- [16]
		self.dir = vmath.normalize(self.input)      -- [17]
	end
end

and my rocket.script

go.property("dir", vmath.vector3())                 -- [1]

function init(self)
	self.speed = 200                                -- [2]
end

function update(self, dt)
	local pos = go.get_position()                   -- [3]
	pos = pos + self.dir * self.speed * dt          -- [4]
	go.set_position(pos)                            -- [5]
end

the error im getting is:
ERROR:SCRIPT: /main/player.script:27: The component could not be found
stack traceback:
[C]: in function ‘create’
/main/player.script:27: in function </main/player.script:16>

What is your factory called? With the line

factory.create("#rocketfactory", ...)

you are trying to access the component “/player#rocketfactory”, which seems to not exist. If the factory game object is called “rocketfactory”, and its factory component is called “#factory”, then you want to write

factory.create("/rocketfactory#factory", ...)

For more on this, see this documentation on components in particular.

1 Like

Thanks that really helped! it turned out i had missnamed the rocketfactory component to rocketfactort :stuck_out_tongue:. Thanks a ton!!

1 Like