Trying to read stdin but keeps freezing [Problem Solved]

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
Bigmacbook
Prole
Posts: 8
Joined: Sun Feb 19, 2012 8:01 am

Trying to read stdin but keeps freezing [Problem Solved]

Post by Bigmacbook »

Code: Select all

test = io.stdin:read()
my game always locks up on this line. can anyone help?

I'm trying to read all stdin that comes in!
Last edited by Bigmacbook on Sun Aug 24, 2014 1:25 am, edited 1 time in total.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Trying to read stdin but keeps freezing

Post by Robin »

Well, yeah. That keeps reading untli it gets an EOF (end of file). On Windows, try typing Ctrl+Z followed by Enter. On other systems, Ctrl+D will usually work.

If you redirect stdin from a file it will work normally, because it knows where to stop if you pass it a file.
Help us help you: attach a .love.
Bigmacbook
Prole
Posts: 8
Joined: Sun Feb 19, 2012 8:01 am

Re: Trying to read stdin but keeps freezing

Post by Bigmacbook »

sorry but this doesn't help me at all
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Trying to read stdin but keeps freezing

Post by Robin »

Okay, then we need more information from you. First, as always, a .love that shows your problem. Secondly, in this case, explain if you're reading from a file or a TTY, what OS you're using, what you're doing exactly, what you expect to happen and what actually happens.
Help us help you: attach a .love.
Bigmacbook
Prole
Posts: 8
Joined: Sun Feb 19, 2012 8:01 am

Re: Trying to read stdin but keeps freezing

Post by Bigmacbook »

Ok what I'm doing is I am running my love2d game in a child process of Node.js

So basically I can get Love2d stdout in nodejs but i'm trying to send text from nodejs to love2d game with stdin

I'm on windows 8

Node.js Code

Code: Select all

var spawn = require('child_process').spawn,
    ls    = spawn('/Users/Ryan/Documents/Love/Versions/64/0.9.1/love.exe', ['/Users/Ryan/Documents/Love/Projects/Game1']);


ls.stdout.on('data', function (data) {
  console.log('' + data);
    ls.stdin.write("test");
});

ls.stderr.on('data', function (data) {
  console.log('stderr: ' + data);
});

ls.on('close', function (code) {
  console.log('child process exited with code ' + code);

});
Lua Code

Code: Select all

function love.load()
--Don't Edit--
	io.stdout:setvbuf("no")
	--io.stdin:setvbuf("no")
--Don't Edit--

	love.graphics.setBackgroundColor(255, 255, 255);
	minecraftFont = love.graphics.newFont("Minecraftia2.ttf", 16 )

	io.stdout:write("hello")
	test = io.stdin:read()
end

function love.update(dt)

end

function love.draw()
	love.graphics.setColor(0, 0, 0, 255)
	love.graphics.setFont(minecraftFont)
	love.graphics.print("FPS: "..tostring(love.timer.getFPS( )), 0, -12)
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Trying to read stdin but keeps freezing

Post by Robin »

Well, one way to do that would be to replace

Code: Select all

ls.stdin.write("test");
with

Code: Select all

ls.stdin.write("test");
ls.stdin.close();
but I assume you want to keep the line open, in which case the best way to deal with it would be to use

Code: Select all

ls.stdin.write("test\n");
and read one line at a time.
Help us help you: attach a .love.
Bigmacbook
Prole
Posts: 8
Joined: Sun Feb 19, 2012 8:01 am

Re: Trying to read stdin but keeps freezing

Post by Bigmacbook »

Code: Select all

function love.update(dt)
test = io.read("*line")
end
Is there any way i can do this. I heard you could do this by creating a new thread and stuff but i don't know how to set that up

with the recent Thread api changes i don't know the correct code, so if you supply a code snippet that would be very helpful

I try to use this but its not working viewtopic.php?f=3&t=2094#p21776
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Trying to read stdin but keeps freezing

Post by Ref »

This is what I do (right or wrong).

Code: Select all

dst = io.open( filename, 'w' )     -- or 'a'
      for i = 1, #lines do 
      dst:write( lines[i]..'\n' )
      end
dst:close(  )

src = io.open( filename, 'r' )
	read = {}
	repeat
		local line = src:read( '*l' )
		if line then read[ #read+1 ] = line end
		until line == nil
src:close()
Bigmacbook
Prole
Posts: 8
Joined: Sun Feb 19, 2012 8:01 am

Re: Trying to read stdin but keeps freezing

Post by Bigmacbook »

Thanks for your help but i figured out what i needed

Here is an example if anyone ever needs to do what i have done

main.lua:

Code: Select all

function love.load()

--Don't Edit--
	io.stdout:setvbuf("no")
--Don't Edit--

	thread = love.thread.newThread("node.lua")
	node = love.thread.getChannel("node")
	thread:start()

        --sends data to node.js process
	io.stdout:write("hello")

	test = "none"
end

function love.update(dt)
	v = node:pop()
	if v ~= nil then
	 test = v
	end
end

function love.draw()
	love.graphics.print(test), 0,0)
end
node.lua

Code: Select all

nodeGet = love.thread.getChannel("node")
repeat
    local s = io.read("*line")
    nodeGet:push(s)
until s == "quit"
node.js code

Code: Select all

var spawn = require('child_process').spawn,
    ls    = spawn('/Path/to/love.exe', ['/Path/to/Love/Project/Folder/or/.love/File']);

ls.stdout.on('data', function (data) {
  console.log('' + data);
});

ls.stderr.on('data', function (data) {
  console.log('stderr: ' + data);
});

ls.on('close', function (code) {
  console.log('child process exited with code ' + code);
});

Post Reply

Who is online

Users browsing this forum: No registered users and 79 guests