Convert a number to string

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
CalmoSoft
Prole
Posts: 16
Joined: Sat Nov 26, 2016 10:31 pm
Location: Hungary

Convert a number to string

Post by CalmoSoft »

Hello All,

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

Greetings,
Gal Zsolt
(~ CalmoSoft ~)
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Convert a number to string

Post by zorg »

Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
CalmoSoft
Prole
Posts: 16
Joined: Sat Nov 26, 2016 10:31 pm
Location: Hungary

Re: Convert a number to string

Post by CalmoSoft »

Hello Zorg,

Thank you very much for your helpful reply.

Greetings,
Gal Zsolt
(~ CalmoSoft ~)
CalmoSoft
Prole
Posts: 16
Joined: Sat Nov 26, 2016 10:31 pm
Location: Hungary

Re: Convert a number to string

Post 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 ~)
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Convert a number to string

Post by davisdude »

I'm not sure which variable is giving you an error, but you could try the function tostring.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
CalmoSoft
Prole
Posts: 16
Joined: Sat Nov 26, 2016 10:31 pm
Location: Hungary

Re: Convert a number to string

Post 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 ~)
CalmoSoft
Prole
Posts: 16
Joined: Sat Nov 26, 2016 10:31 pm
Location: Hungary

Re: Convert a number to string

Post 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 ~)
Attachments
CalmoSoftPuzzleGame.love
(981 Bytes) Downloaded 168 times
Last edited by CalmoSoft on Wed Dec 28, 2016 11:01 pm, edited 6 times in total.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Convert a number to string

Post 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.
CalmoSoft
Prole
Posts: 16
Joined: Sat Nov 26, 2016 10:31 pm
Location: Hungary

Re: Convert a number to string

Post by CalmoSoft »

Hello Raidho66,

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

Gretetings,
Gal Zsolt
(~ CalmoSoft ~)
Attachments
CalmoSoftPuzzleGame.love
(981 Bytes) Downloaded 184 times
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests