Help with a coding problem

Hi guy,

so, basically i was trying to make a code to compare values with dirty lary library but every time i inserted the right value the code answered that it was not the correct one.

this is the code:

function on_input(self, action_id, action)
  self.input_v0 = dirtylarry:input("lvl1.input.v0", action_id, action, 
  gui.KEYBOARD_TYPE_NUMBER_PAD, "Valor de V0")
 	dirtylarry:button("lvl1.botao.v0", action_id, action, function() 
		v0 = self.input_v0 
		print("V0:"..tostring(self.input_v0))
		if v0==12 then
			print("You Win ")
			print(v0)
		else
			print("You Lose")
			print(v0)
		end 
	end)
end

and then the console returned this:

DEBUG:SCRIPT: V0:12
DEBUG:SCRIPT: You Lose
DEBUG:SCRIPT: 12

I have no idea why if the value receive “12” it prints “you lose”

can anyone help me ?

Hey!

So the first thing that pops out to be is that on this line:

It appears you are making the assumption that self.input_v0 is a number value. However, it could be a string in your given context. Try using either of these implementations:

if v0=="12" then
or:
if tostring(v0) == tostring(12) then

If that does not work, I suggest setting a break point and using a step through process to watch the code and see what happens. Here’s a reference to the defold debugging page: Debugging in Defold

Using the debugger you can watch your if statement to see specifically what happens.

Best of luck!

4 Likes

yeah, the first option you gave me really work!!! Thx a lot !!

and I know its to muth, but if you coul help me with one more quastion I would appreciate it.
Is there any way to limit the input component to only numbers ?

Instead of converting both to strings, you can do the opposite. You can use ‘tonumber()’ on on the string from Dirty Larry, which will either give you a number or nil.

2 Likes