Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


io.read() not working?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Redouane
Master Cheater
Reputation: 3

Joined: 05 Sep 2013
Posts: 363
Location: Algeria

PostPosted: Thu Dec 12, 2013 11:04 am    Post subject: io.read() not working? Reply with quote

Hey,I tried to do this:
Code:
a=io.read()
justastringtotest
print(a) -- nil

the io.read() does not work properly,this code returns an error (it should place the string "justastringtotest" in a,here,a is nil
also,the following few lines work perfectly in the standalone lua interpreter but they don't work in CE's lua engine (CE Freezes when you execute the code)
Code:
repeat
r=io.read()
main(r)
until r=="stop"

In fact,the original code is much longer,It'll wait for you to enter a 1st or 2nd degree equation (ax˛+bx+c) then It'll extract a,b,c and calculate the solutions,the whole code works in the lua standalone interpreter but it crashes CE(freezes).
Back to top
View user's profile Send private message
daspamer
Grandmaster Cheater Supreme
Reputation: 54

Joined: 13 Sep 2011
Posts: 1588

PostPosted: Thu Dec 12, 2013 11:47 am    Post subject: Reply with quote

I'm not sure what you're doing... io.read()? no file?.
Use it like this
Code:
f = io.open(Path, "r") --> Place the file you want to open and read, replace path.
print(f:read()) --> returns first line
print(f:read("*a")) --> returns whole file.



Also the reason why the second function makes C.E freeze, is because :
You didn't use the
Code:
processMessages()

function in your code (allows new clicks and events.. without C.E being frozen on you)

Secondly, you didn't place any 'break' statement, because r = io.read(); equals to nil (based C.E), and you repeated it.. forever.. (without calling the processMessages() function as well).

_________________
I'm rusty and getting older, help me re-learn lua.
Back to top
View user's profile Send private message Visit poster's website
mgr.inz.Player
I post too much
Reputation: 222

Joined: 07 Nov 2008
Posts: 4438
Location: W kraju nad Wisla. UTC+01:00

PostPosted: Thu Dec 12, 2013 4:20 pm    Post subject: Reply with quote

I think, you want to load whole txt file, line by line, to array object (or something similar).

Code:
filecontent = {}
file = io.open('c:\\test\\testfile.txt', "r")
if file~=nil then
  for line in file:lines() do
    filecontent[#filecontent+1] = line
  end
  file:close()
end

Should be much faster, and you don't need "processMessages" (if txt file isn't too big of course).



Avoid those constructions:
Code:

  filecontent = ''
  for line in file:lines() do
    filecontent = filecontent..line
  end



I saw few benchmarks. "Table" is better than "string" (concatenating inside loop).


Read Lua manual too (http://www.lua.org/)

_________________
Back to top
View user's profile Send private message MSN Messenger
Redouane
Master Cheater
Reputation: 3

Joined: 05 Sep 2013
Posts: 363
Location: Algeria

PostPosted: Sun Dec 15, 2013 5:54 am    Post subject: Reply with quote

I'm not trying to read a file,my code asks the user to enter a string (equation) then it reads the string with that,It's just an experiment that worked fine in the standalone lua interpreter.
In the standalone lua interpreter,if you enter a=io.read("*number"),then on the next line you enter 5354 It will assign the value 5354 to a,here,it reads the whole line.
@Daspammer thanks for the processMessages() function Smile
Back to top
View user's profile Send private message
mgr.inz.Player
I post too much
Reputation: 222

Joined: 07 Nov 2008
Posts: 4438
Location: W kraju nad Wisla. UTC+01:00

PostPosted: Sun Dec 15, 2013 9:52 am    Post subject: Reply with quote

Oh, you didn't write you want to read user input from stdin.
In CE, there's no Lua Console. You can mimic this. You need form, editbox and OnKeyPress method.

You can test below CT file. Open it with CE, execute lua script.
Type whatever you want, if input is a number (it can be converted to a number) it will assign than number to variable A, otherwise it will NIL variable A.

Code:
function passwordeditkeypress(sender, key)
  if key ~= '\r' then return key end

  local input=UDF1.CEEdit1.Caption
  UDF1.CEEdit1.Caption=''

  if input=='' then return end

  if tonumber(input) then
    A = input+0 -- cast to number
    UDF1.CEMemo1.append('assigned value '..input..' to variable A')
  else
    A = nil
    UDF1.CEMemo1.append('user input: '..input..'. Variable A NIL\'ed')
  end

end

UDF1.CEEdit1.OnKeyPress = passwordeditkeypress

UDF1.show()

CEMemo1 is only for logging. You can remove lines with CEMemo1.



Paste your original code you use in the lua standalone interpreter. I can try to port it to CE Lua.



userinput.ct
 Description:

Download
 Filename:  userinput.ct
 Filesize:  1 KB
 Downloaded:  883 Time(s)


_________________
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites