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 


Save Array to File with Lines?

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

Joined: 28 Jun 2015
Posts: 432

PostPosted: Sun Mar 20, 2016 2:58 am    Post subject: Save Array to File with Lines? Reply with quote

Code:

local file2 = io.open("c:\\users\\akuma\\desktop\\lua\\hello.txt","r+")
local read = {}   -- alloc?????
local loop = 0    --define start of loop
    for line in file2:lines() do
     loop = loop + 1   ----read = read line + 1 on loop
     read[loop] = line
     print(read[loop])  -- print file
    end
 file2:close()


Above code read all lines. Easy enough.

Trying to accomplish a simple .ini file of a couple settings.

So the above FILE(hello.txt) has 5 lines in it consisting of...

899 --Screen top pos
1022 -- Screen left pos
566 --form width
700 --form height
1 -- Check for compare


these lines are easy to read and return the postion and size of my window. eg..

Code:

function loadsavedsettings(timer)
MyForm.Top = read[1]
MyForm.Left = read[2]
MyForm.Width = read[3]
MyForm.Height = read[4]
end


What i cant figure out is how to write new settings back after the updated values in the array. eg..

Code:

function SAVEsettingsOnCLOSE(sender)
read[1] = MyForm.Top
read[2] = MyForm.Left
read[3] = MyForm.Width
read[4] = MyForm.Height
end

local file2 = io.open("c:\\users\\akuma\\desktop\\lua\\hello.txt","r+")
 write the array back to file2
end



How can an Array be written to file LINE by LINE?

_________________
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 55

Joined: 01 Oct 2008
Posts: 942

PostPosted: Sun Mar 20, 2016 6:59 am    Post subject: This post has 1 review(s) Reply with quote

"How can an Array be written to file LINE by LINE? "

The array entry can be 'join' by table.concat function http://www.lua.org/manual/5.3/manual.html#pdf-table.concat . It return a string with each array entry join by a delimiter.
Code:
local s = table.concat(arr,'\n')

then the string can be write to the file in one go:
Code:

local f = io.open('save','w')
f:write(s)
f:close()


Or do it under a for loop:
Code:

local f = io.open('save','w')
for i=1,#arr do
  f:write(arr[i]..'\n')
end
f:close()


The arr entry may need to be string or number for the concatenation work.


CE has a settings class, which store data in user registry, may have a look:
Code:
Settings class
  This class can be used to read out and set settings of cheat engine and of plugins, and store your own data




ADDED:
Steve Donovan's Penlight seems have a self-contained module that work with ini file:
https://github.com/stevedonovan/Penlight/blob/master/lua/pl/config.lua
http://stevedonovan.github.io/Penlight/api/libraries/pl.config.html

...oops, no writing function, sry~

_________________
- Retarded.
Back to top
View user's profile Send private message
mgr.inz.Player
I post too much
Reputation: 218

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

PostPosted: Sun Mar 20, 2016 7:59 am    Post subject: Reply with quote

Code:
filename=[[C:\position.dat]]

function loadSettings()
  local settings = createStringlist()
  settings.loadFromFile(filename)
  MyForm.Top = tonumber(settings[0])
  MyForm.Left = tonumber(settings[1])
  MyForm.Width = tonumber(settings[2])
  MyForm.Height = tonumber(settings[3])
  settings.destroy()
end



function saveSettings()
  local settings = createStringlist()
  settings.add( MyForm.Top )
  settings.add( MyForm.Left )
  settings.add( MyForm.Width )
  settings.add( MyForm.Height )
  settings.saveToFile(filename)
  settings.destroy()
end

_________________
Back to top
View user's profile Send private message MSN Messenger
MikeNoey
Advanced Cheater
Reputation: 0

Joined: 08 Jun 2018
Posts: 64

PostPosted: Mon Jul 29, 2019 10:38 pm    Post subject: Reply with quote

Code:
filename=[[C:\config.txt]]


function loadSettings()
  local settings = createStringlist()
  settings.loadFromFile(filename)
  MyForm.Top = tonumber(settings[0])
  MyForm.Left = tonumber(settings[1])
  MyForm.Width = tonumber(settings[2])
  MyForm.Height = tonumber(settings[3])
  settings.destroy()
end



function saveSettings()
  local settings = createStringlist()
  settings.add( MyForm.Top )
  settings.add( MyForm.Left )
  settings.add( MyForm.Width )
  settings.add( MyForm.Height )
  settings.saveToFile(filename)
  settings.destroy()
end


loadSettings()



I get the following error upon executing this script

"Error:[string "filename =[[C:\config.txt]]..."]:7: attempt to index a nil value (global 'MyForm')"


the contents of the config.txt file
899
1022
566
700


What am I doing wrong ?
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Mon Jul 29, 2019 11:05 pm    Post subject: Reply with quote

change 'MyForm' to your form name

or change the function to these:

Code:
filename=[[C:\config.txt]]

function loadSettings(MyForm)
  local settings = createStringlist()
  settings.loadFromFile(filename)
  MyForm.Top = tonumber(settings[0])
  MyForm.Left = tonumber(settings[1])
  MyForm.Width = tonumber(settings[2])
  MyForm.Height = tonumber(settings[3])
  settings.destroy()
end

function saveSettings(MyForm)
  local settings = createStringlist()
  settings.add( MyForm.Top )
  settings.add( MyForm.Left )
  settings.add( MyForm.Width )
  settings.add( MyForm.Height )
  settings.saveToFile(filename)
  settings.destroy()
end


-- use:
saveSettings(UDF1)  -- or form name
loadSettings(UDF1) -- or form name

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
MikeNoey
Advanced Cheater
Reputation: 0

Joined: 08 Jun 2018
Posts: 64

PostPosted: Wed Jul 31, 2019 10:41 pm    Post subject: Reply with quote

I figured out why it wasn't working for me. I think "MyForm" is related to a trainer. Once I removed "MyForm" from the code it worked for me. I have been trying to figure out how to write to specific lines of a document but i'm not having any luck. for example writing to lines 3, 6, 9 and 12.

Code:
function saveSettings()
  local settings = createStringlist()
  settings.add( Top[3] )
  settings.add( Left [6])
  settings.add( Width[9] )
  settings.add( Height[12] )
  settings.saveToFile(filename)
  settings.destroy()
end


Also running this code overwrites what was already in the document. What if I want to keep some things in the document. Keep everything on lines 1 through to 10 but change something on line 11 through to 15. How would I adjust this code to do that ?
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Wed Jul 31, 2019 11:58 pm    Post subject: Reply with quote

Use '\n' for the new line,

I am using this:

Code:
function saveSettings(MyForm)
  path = TrainerOrigin or getMainForm()
  local settingfile = io.open(path.."\\mysetting.ini", "r")
  if not settingfile then return nil end
   settingfile.close()
   local settingfile = io.open(path.."\\mysetting.ini", "a+")
   local top = MyForm.Top
   local left = MyForm.Left
   local width = MyForm.Width
   local height = MyForm.Height
   settingfile:write(top,"\n")
   settingfile:write(left,"\n")
   settingfile:write(width,"\n")
   settingfile:write(height,"\n")
   settingfile.close()
end


-- use:
saveSettings(UDF1)  -- or form name

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
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