Simple Coding Question...

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
michaeladair
Prole
Posts: 9
Joined: Mon Dec 29, 2014 5:12 am

Simple Coding Question...

Post by michaeladair »

So I have 3 previously defined variables by the user, r, g, and b. These are inputted by the user and should be numbers.
Here is where it is called.

Code: Select all

function load( ip, name, r, g, b)
	-- Collider = HC( 100, on_collision )
	cam = camera(vector(256, 208), 1)
	love.graphics.setBackgroundColor( 50, 50, 50 )
	love.graphics.setColor( r, g, b) 

	client = lube.client()
	client:setHandshake("chillout")
	client:setCallback(onReceive)
	client:connect(ip, 18025)		--82.24.82.156 marks

	client:send( "name" .. name)
end
I'm not sure If I called it right...

Here is where the user inputs the variables... (Is there any easier way to add a form?)

Code: Select all

local name = ""
local ip = ""
local r = ""
local g = ""
local b = ""
local flag = 1

function love.draw()
	love.graphics.print("ip: " .. ip, 10, 10)
	love.graphics.print("name: " .. name, 10, 30 )
	love.graphics.print("Red: " .. r, 10, 50)
	love.graphics.print("Green: " .. g, 10, 70)
	love.graphics.print("Blue: " .. b, 10, 90)
end

function love.keypressed( key )
	if key == 'return' then
		if flag == 1 then
			flag = 2
		elseif flag == 2 then
			flag = 3
		elseif flag == 3 then
			flag = 4
		elseif flag == 4 then
			flag = 5
		elseif flag == 5 then
			playing( ip, name, r, g, b )
		end
	elseif key == 'backspace' then
		if flag == 1 then
			ip = string.sub(ip, 1, string.len( ip ) - 1)
		elseif flag == 2 then
			name = string.sub(name , 1, string.len( name  ) - 1)
		elseif flag == 3 then
			r = string.sub(r , 1, string.len( r  ) - 1)
		elseif flag == 4 then
			g = string.sub(g , 1, string.len( g  ) - 1)
		elseif flag == 5 then
			b = string.sub(b , 1, string.len( b  ) - 1)
		end
	else
		if flag == 1 then
			ip = ip .. key
		elseif flag == 2 then
			name = name .. key
		elseif flag == 3 then
			r = r .. key
		elseif flag == 4 then
			g = g .. key
		elseif flag == 5 then
			b = b .. key
		end
	end
end
User avatar
AlexCalv
Prole
Posts: 49
Joined: Fri Aug 08, 2014 7:12 pm
Contact:

Re: Simple Coding Question...

Post by AlexCalv »

I'm not sure of what you're asking really but you can simplify the drawing calls by doing this instead:

Code: Select all

love.graphics.print("IP: " ..ip.. " Name: " ..name.. " Red: " ..r.. " Green: " ..g.. " Blue: " ..b, 10, 10)
Or separate lines:

Code: Select all

love.graphics.print("IP: " ..ip.. "\nName: " ..name.. "\nRed: " ..r.. "\nGreen: " ..g.. "\nBlue: " ..b, 10, 10)
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Simple Coding Question...

Post by zorg »

I think i kind of understand what you want to do.

At a first glance, i only see one potential error;

Code: Select all

function load( ip, name, r, g, b) <--

Code: Select all

elseif flag == 5 then
         playing( ip, name, r, g, b ) <--
      end
   elseif key == 'backspace' then
if those were ment to be the same thing, then rename one of them.
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.
User avatar
michaeladair
Prole
Posts: 9
Joined: Mon Dec 29, 2014 5:12 am

Re: Simple Coding Question...

Post by michaeladair »

The problem that I have is that it does not see the variables as numbers:

Code: Select all

ERROR
states/playing.lua: 11: Bad argument #1 to 'setColor' (number expected, got nil)
Line 11:

Code: Select all

love.graphics.setColor( r, g, b) 
Playing.lua:

Code: Select all

players = {}

function onReceive(data)
	players = TSerial.unpack( data )
end

function load( ip, name, r, g, b )
	-- Collider = HC( 100, on_collision )
	cam = camera(vector(256, 208), 1)
	love.graphics.setBackgroundColor( 50, 50, 50 )
	love.graphics.setColor( r, g, b) 

	client = lube.client()
	client:setHandshake("chillout")
	client:setCallback(onReceive)
	client:connect(ip, 18025)		--82.24.82.156 marks

	client:send( "name" .. name)
end

function love.draw()
	cam:attach()

	for k, v in pairs(players) do
		love.graphics.rectangle('fill', v.x, v.y, 32, 32)
		love.graphics.print(v.name, v.x-10, v.y-20)
		love.graphics.setColor(255, 255, 255) --White
	end

	cam:detach()

	-- debug
	drawMessages()
	love.graphics.print( love.timer.getFPS(), 10, 10 )
end

function love.update( dt )
	-- Collider:update( dt )
	client:update(dt)

	if love.keyboard.isDown('a') or love.keyboard.isDown('left') then
		client:send('left')
	elseif love.keyboard.isDown('d') or love.keyboard.isDown('right') then
		client:send('right')
	end

	if love.keyboard.isDown('w') or love.keyboard.isDown('up') then
		client:send('up')
	elseif love.keyboard.isDown('s') or love.keyboard.isDown('down') then
		client:send('down')
	end
end

function love.keypressed( key )
end

function love.keyreleased( key )
end

function love.mousepressed( x, y, key )
end

function love.quit()
	client:send('quit')
end
Menu.lua:

Code: Select all

local name = ""
local ip = ""
local r = ""
local g = ""
local b = ""
local flag = 1

function love.draw()
	love.graphics.print("ip: " .. ip, 10, 10)
	love.graphics.print("name: " .. name, 10, 30 )
	love.graphics.print("Red: " .. r, 10, 50)
	love.graphics.print("Green: " .. g, 10, 70)
	love.graphics.print("Blue: " .. b, 10, 90)
end

function love.keypressed( key )
	if key == 'return' then
		if flag == 1 then
			flag = 2
		elseif flag == 2 then
			flag = 3
		elseif flag == 3 then
			flag = 4
		elseif flag == 4 then
			flag = 5
		elseif flag == 5 then
			playing( ip, name, r, g, b )
		end
	elseif key == 'backspace' then
		if flag == 1 then
			ip = string.sub(ip, 1, string.len( ip ) - 1)
		elseif flag == 2 then
			name = string.sub(name , 1, string.len( name  ) - 1)
		elseif flag == 3 then
			r = string.sub(r , 1, string.len( r  ) - 1)
		elseif flag == 4 then
			g = string.sub(g , 1, string.len( g  ) - 1)
		elseif flag == 5 then
			b = string.sub(b , 1, string.len( b  ) - 1)
		end
	else
		if flag == 1 then
			ip = ip .. key
		elseif flag == 2 then
			name = name .. key
		elseif flag == 3 then
			r = r .. key
		elseif flag == 4 then
			g = g .. key
		elseif flag == 5 then
			b = b .. key
		end
	end
end
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Simple Coding Question...

Post by zorg »

The problem is that you're not passing those values to your load function, hence they are nil, as in, they don't exist.

Again, from what code you posted, i can not see where you call that function even.
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.
User avatar
michaeladair
Prole
Posts: 9
Joined: Mon Dec 29, 2014 5:12 am

Re: Simple Coding Question...

Post by michaeladair »

zorg wrote:The problem is that you're not passing those values to your load function, hence they are nil, as in, they don't exist.

Again, from what code you posted, i can not see where you call that function even.
How do I pass the values? The ip and name are passed??
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Simple Coding Question...

Post by zorg »

If those are, then as i said, from the code snippets you posted, i could not figure out the problem; sorry.
Either someone else will figure it out, or just post a .love file, or a zip with all the files (including your main.lua) or something, because i feel that something's still missing from the big picture.
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.
User avatar
michaeladair
Prole
Posts: 9
Joined: Mon Dec 29, 2014 5:12 am

Re: Simple Coding Question...

Post by michaeladair »

zorg wrote:If those are, then as i said, from the code snippets you posted, i could not figure out the problem; sorry.
Either someone else will figure it out, or just post a .love file, or a zip with all the files (including your main.lua) or something, because i feel that something's still missing from the big picture.
Oh, i forgot to add the variables to the main.lua... it worked but now I have a problem with the rectangle not being written in the correct color.
It ends up writing the rectangle in white still.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Simple Coding Question...

Post by Positive07 »

You have this code in love.draw

Code: Select all

for k, v in pairs(players) do
      love.graphics.rectangle('fill', v.x, v.y, 32, 32)
      love.graphics.print(v.name, v.x-10, v.y-20)
      love.graphics.setColor(255, 255, 255) --White
end
The first time you draw the rectangle you get the right color, but the second time, you have set the color to white... so everything you draw after the first iteration is white, if you want to change this

Then in load change this

Code: Select all

love.graphics.setColor( r, g, b) 
to this

Code: Select all

color = {r,g,b} 
And in your for loop do this

Code: Select all

for k, v in pairs(players) do
      love.graphics.setColor(color)
      love.graphics.rectangle('fill', v.x, v.y, 32, 32)
      love.graphics.setColor(255, 255, 255) --White
      love.graphics.print(v.name, v.x-10, v.y-20)
end
I put white before the print, I guess that is what you really wanted, otherwise the print would have the same color as the rectangle, if that is not what you wanted then do this instead

Code: Select all

for k, v in pairs(players) do
      love.graphics.setColor(color)
      love.graphics.rectangle('fill', v.x, v.y, 32, 32)
      love.graphics.print(v.name, v.x-10, v.y-20)
end
love.graphics.setColor(255, 255, 255) --White
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Post Reply

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest