Problems with input in war battles tutorial (SOLVED)

it’s been 2 hours i am trying to fix this problem following the tutorial of war battles and input is not working it’s not firing rocket and the player don’t move help please:
this is my player code:

function init(self)
	msg.post(".", "acquire_input_focus")
	self,moving = false
	self.firing = false
	self.input = vmath.vector3()
	self.dir = vmath.vector(0, 1, 0)
	self.speed = 50
end

function final(self)
	msg.post(".", "release_input_focus")
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)
		local rot = vmath.quat_rotation_z(angle)
		local props = { dir = self.dir }
		factory.create("#rocketfactory")
	end
	self.input.x = 0
	self.input.y = 0
	self.moving = false
	self.firing = false
end

function on_input(self, action_id, action)
	if action_id == hash("up") then
		self.input.y = 1
	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.lenght(self.input) > 0 then
		self.moving = true
		self.dir = vmath.normalize(self.input)
	end
end

and this is the rocket script:

go.property("dir", vmath.vector3())

function init(self)
	self.speed = 200
end

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

Hey!
Did you tried to add some print in on_input? Is the script gathering any input?
Did you also set up the input binding?

i setup input binding i will try print

they give me this error
ERROR:SCRIPT: main/player.script:26: attempt to index field ‘input’ (a nil value)
stack traceback:
main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

    ERROR:SCRIPT: main/player.script:26: attempt to index field 'input' (a nil value)
    stack traceback:
      main/player.script:26: in function <main/player.script:14>

I will get back to this tommorow (as it’s very late in my time zone) , but for now on, please check out what is this “input” field in line 26 (there is self.input in the chunk you attached above), it’s weird it’s nil there, maybe there are some typos?

I don’t know how you are debugging, but you can’t just change things randomly and hope the problem goes away, you need to go through your code systematically, step-by-step, until you find what went wrong, and how.

First, if there are any errors in the console, immediately stop and fix them. Assume that nothing will work properly anywhere if you get an error message. Usually syntax error messages are pretty clear, like that nil value error—it tells you the file, the line, the variable name, what it tried to do, and why it failed.

Second…well I won’t reinvent the wheel :grinning_face_with_smiling_eyes:, read the Debugging Manual, it even has a nice checklist near the end that specifically mentions input issues (albeit briefly).

Based on your past threads, you tend to make a lot of typing errors. Using autocomplete can help with this, as well as simply slowing down and being more careful. :slight_smile:


Better, more specific, thread titles will also make people more likely to help you (and not just ignore your thread). Instead of “I am trying to fix this problem help”, something like: “Can’t get any input to work with war battles tutorial” would be better.

5 Likes

I can’t check it myself because it’s very late for me as well, but I believe I have found the - or at least a - missing piece.

In your script (main/player.script) you have:

While it should be as shown in the tutorial:

factory.create("#rocketfactory", nil, rot, props)

Add explicit position (nil, the rocket will spawn at the player’s position), rotation (the calculated quaternion) and spawn property values.

This can be found here: https://github.com/defold/tutorial-war-battles#set-the-direction-of-the-rocket

2 Likes

That’s a comma, not a .

Like Ross said, if there is an error in the console then stop whatever you are doing and try to understand what it means. There should be no errors. Also, when you ask for help, please include any errors you have in the console. There are four different people in this thread trying to help you. You need to provide the necessary information so that they can help you quickly and not spend a lot of time asking for more information.

Please also have a look here to understand what kinds of relevent information to share when asking for help:

2 Likes

Yeah, don’t worry, step by step, you will definitely learn how to debug it, the beginnings are always tough :wink:
As @britzl pointed out there is a typo in self.moving, so you didn’t posted all the errors, but the few last, showing in every update.

Always check out the first appearing error. In init you fail before initializing self.input and thus the error you pasted above is showing, because you are trying to access it in update function (which is called every frame) :wink:

Fix the typos (this and the one pointed out by @amel regarding factory) and let us know if there is anything more

3 Likes

Unfortunately, this line does not throw an error, though the next line will. But printing it out shows that there is a problem: “false nil”. Debug print statements are very helpful and important.
If your code does not work, follow the great suggestions above, and my advice would be to look for typos first. Almost every code snipped you posted so far has at least one in it. This is something to be aware of. You need to take a bit more care when writing code but this will improve the more practice you have. Everyone here started out as a beginner, too.

Thanks for helping this is the best community

Thanks

Ok

Ok thanks