Page 1 of 1

Convert a number to string

Posted: Mon Dec 26, 2016 3:25 pm
by CalmoSoft
Hello All,

How can I convert a number to string?
Thank you very much for help.

Greetings,
Gal Zsolt
(~ CalmoSoft ~)

Re: Convert a number to string

Posted: Mon Dec 26, 2016 3:39 pm
by zorg

Re: Convert a number to string

Posted: Mon Dec 26, 2016 3:46 pm
by CalmoSoft
Hello Zorg,

Thank you very much for your helpful reply.

Greetings,
Gal Zsolt
(~ CalmoSoft ~)

Re: Convert a number to string

Posted: Mon Dec 26, 2016 4:06 pm
by CalmoSoft
Hello Zorg,

I read the next in attached page:

>>>Conversely, whenever it finds a number where it expects a string, Lua converts the number to a string<<<

I have the next code but it does not work.
What do I wrong?

Code:

Code: Select all

function love.draw()
	for row = 1, 4 do
	     for col = 1, 4 do
                  love.graphics.print(nr, printx, printy)
  		  love.graphics.rectangle('line',	row*40, col*40, 40, 40)
		  love.graphics.print((row-1)*4+col, row*40, col*40)
	     end
	end
end

function love.mousepressed(x, y, button, istouch)
            if button == 1 then -- Versions prior to 0.10.0 use the MouseConstant 'l'
               if (x>=20 and x<=60 and y>=20 and y<=60) then
                   nr = 99
              end
              printx = x
              printy = y
           end
end
Greetings,
Gal Zsolt
(~ CalmoSoft ~)

Re: Convert a number to string

Posted: Mon Dec 26, 2016 4:30 pm
by davisdude
I'm not sure which variable is giving you an error, but you could try the function tostring.

Re: Convert a number to string

Posted: Mon Dec 26, 2016 4:52 pm
by CalmoSoft
Hello Davisdude,

Now I have the next code but it still does not work.
Please send me a sample.

Code:
function love.draw()
for row = 1, 4 do
for col = 1, 4 do
love.graphics.print(str, printx, printy)
love.graphics.rectangle('line', row*40, col*40, 40, 40)
love.graphics.print((row-1)*4+col, row*40, col*40)
end
end
end

function love.mousepressed(x, y, button, istouch)
if button == 1 then -- Versions prior to 0.10.0 use the MouseConstant 'l'
printx = x
printy = y
if (x>=20 and x<=60 and y>=20 and y<=60) then
nr = 99
str=tostring(nr)
end
end
end
Greetings,
Gal Zsolt
(~ CalmoSoft ~)

Re: Convert a number to string

Posted: Mon Dec 26, 2016 5:25 pm
by CalmoSoft
Hello Davisdude,

Now I have the next code and it works properly.
Thank you very much for your help.

Code:

Code: Select all

function love.load()
	       love.graphics.setNewFont(30)
         button = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "---", "Scramble", "Reset"}
end

function reset()
         love.graphics.clear()
         button = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "---", "Scramble", "Reset"}
         for row = 1, 4 do
		         for col = 1, 4 do 
                 item = (row-1)*4+col
                 love.graphics.print(tostring(button[item]), col*40, row*40)
  		           love.graphics.rectangle('line',	col*40, row*40, 40, 40)
		         end
         end 
end         

function scramble()
             for n = 1, 100 do
                 love.graphics.clear()
                 for m = 1, 16 do
                     if button[m] == "---" then
                        empty = m
                     end
                 end    
  	             tile = love.math.random(1, 16)
                 love.timer.sleep(0.01)
                 up = (empty == (tile - 4))
  	             down = (empty == (tile + 4))
                 left = ((empty == tile - 1) and (tile % 4 ~= 1))
   	             right = ((empty == tile + 1) and (tile % 4 ~= 0))
                 move = (up or down or left or right)
                 if (move == true) then
                     button[empty] = button[tile]
                     button[tile] = "---"
                     empty = tile
                 end
             end
             for row = 1, 4 do
		             for col = 1, 4 do 
                     item = (row-1)*4+col
                     love.graphics.print(tostring(button[item]), col*40, row*40)
  		               love.graphics.rectangle('line',	col*40, row*40, 40, 40)
		             end
             end 
             love.graphics.print(button[17], 40, 200)
             love.graphics.rectangle('line',	40, 200, 160, 40)
             love.graphics.print(button[18], 40, 240)
             love.graphics.rectangle('line',	40, 240, 160, 40)
             --reset()
end       

function love.draw()
         for row = 1, 4 do
		         for col = 1, 4 do 
                 item = (row-1)*4+col
                 love.graphics.print(tostring(button[item]), col*40, row*40)
  		           love.graphics.rectangle('line',	col*40, row*40, 40, 40)
             end
	        end
          love.graphics.print(button[17], 40, 200)
          love.graphics.rectangle('line',	40, 200, 160, 40)
          love.graphics.print(button[18], 40, 240)
          love.graphics.rectangle('line',	40, 240, 160, 40)
end

function love.mousepressed(x, y, button, istouch)
   if button == 1 then 
      if (x>=40 and x<=80 and y>=40 and y<=80) then
         btn = 1
         reset()
      end    
      if (x>=80 and x<=120 and y>=40 and y<=80) then
         btn = 2   
      end
      if (x>=120 and x<=160 and y>=40 and y<=80) then
         btn = 3   
      end
      if (x>=160 and x<=2000 and y>=40 and y<=80) then
         btn = 4   
      end
      if (x>=40 and x<=80 and y>=80 and y<=120) then
         btn = 5   
      end
      if (x>=80 and x<=120 and y>=80 and y<=120) then
         btn = 6   
      end
      if (x>=120 and x<=160 and y>=80 and y<=120) then
         btn = 7   
      end
      if (x>=160 and x<=200 and y>=80 and y<=120) then
         btn = 8   
      end
      if (x>=40 and x<=80 and y>=120 and y<=160) then
         btn = 9   
      end
      if (x>=80 and x<=120 and y>=120 and y<=160) then
         btn = 10
      end
      if (x>=120 and x<=160 and y>=120 and y<=160) then
         btn = 11   
      end 
      if (x>=160 and x<=200 and y>=120 and y<=160) then
         btn = 12   
      end 
      if (x>=40 and x<=80 and y>=160 and y<=200) then
         btn = 13
      end   
      if (x>=80 and x<=120 and y>=160 and y<=200) then
         btn = 14
      end   
      if (x>=120 and x<=160 and y>=160 and y<=200) then
         btn = 15 
      end 
      if (x>=160 and x<=200 and y>=160 and y<=200) then
         btn = 16 
      end
      if (x>=40 and x<=200 and y>=200 and y<=240) then
         btn = 17   
         scramble()
      end
      if (x>=40 and x<=200 and y>=240 and y<=280) then
         btn = 18   
         reset()
      end
    end  
  end
Greetings,
Gal Zsolt
(~ CalmoSoft ~)

Re: Convert a number to string

Posted: Mon Dec 26, 2016 5:27 pm
by raidho36
When program crashes, you are given an error screen (error text also dumped into the terminal). It points to an exact location where the error occurred, and also gives exact reason why. Try using that information to trace down to the source of the problem. Often enough, problem is very simple to resolve, saving you time waiting on others to assist you with this.

Re: Convert a number to string

Posted: Mon Dec 26, 2016 5:35 pm
by CalmoSoft
Hello Raidho66,

You are right.
Excuse me.
Now I have attached my file.

Gretetings,
Gal Zsolt
(~ CalmoSoft ~)