Difference between revisions of "Lua:saveTable"

From Cheat Engine
Jump to navigation Jump to search
(Created page with 'Category:Lua '''function''' saveTable(''fileName'', ''protect'' OPTIONAL) Saves the current table. If protect is provided and set to true and the filename has the ".CETRAINE…')
 
(Major overhaul of the post.)
Line 1: Line 1:
 
[[Category:Lua]]
 
[[Category:Lua]]
'''function''' saveTable(''fileName'', ''protect'' OPTIONAL)
+
{{CodeBox|'''function''' saveTable(''filename'', ''protect'' OPTIONAL, ''dontDeactivateDesignerForms'' OPTIONAL) ''':''' void}}
  
Saves the current table. If protect is provided and set to true and the filename has the ".CETRAINER" extension, it will protect it from reading normally.
+
{{CodeBox|'''function''' saveTable(''stream'', ''dontDeactivateDesignerForms'' OPTIONAL) ''':''' void}}
 +
 
 +
Saves the current Cheat Engine table.
 +
 
 +
saveTable can save the table to a file path or to a stream object. When saving to a filename with the '''.CETRAINER''' extension, the optional protect parameter can be used to protect the trainer from being read normally.
  
 
===Function Parameters===
 
===Function Parameters===
{|width="85%" cellpadding="10%" cellpadding="5%" cellspacing="0" border="0"
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 
!align="left"|Parameter
 
!align="left"|Parameter
 
!align="left"|Type
 
!align="left"|Type
 
!style="width: 80%;background-color:white;" align="left"|Description
 
!style="width: 80%;background-color:white;" align="left"|Description
 
|-
 
|-
|fileName
+
|filename
 
|String
 
|String
|The name of the file to save cheat table to
+
|The path where the current table should be saved. Usually this is a .CT or .CETRAINER file.
 +
|-
 +
|stream
 +
|Stream
 +
|A stream object the current table should be saved to.
 
|-
 
|-
 
|protect
 
|protect
|Boolean
+
|Boolean OPTIONAL
|If set to true and the filename has the ".CETRAINER" extension, it will protect it from reading normally
+
|Only used by the filename overload. If true and filename has the .CETRAINER extension, the trainer is protected from being read normally.
 +
|-
 +
|dontDeactivateDesignerForms
 +
|Boolean OPTIONAL
 +
|If true, designer forms are not deactivated while saving the table.
 
|}
 
|}
  
== Examples ==
+
===Returns===
<pre>
+
This function does not return a value.
saveTable('SomeGame.CT')
+
 
</pre>
+
===Description===
<pre>
+
saveTable saves the currently loaded Cheat Engine table.
saveTable('SomeGame.CETRAINER', true)
+
 
</pre>
+
When called with a filename, Cheat Engine writes the current table to disk. When called with a stream, Cheat Engine writes the table data to the given stream object.
 +
 
 +
The protect parameter only applies to the filename overload and is intended for protected .CETRAINER output.
 +
 
 +
===Save Modes===
 +
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Call
 +
!style="width: 80%;background-color:white;" align="left"|Behavior
 +
|-
 +
|saveTable(filename)
 +
|Saves the current table to a file.
 +
|-
 +
|saveTable(filename, protect)
 +
|Saves the current table to a file and optionally protects .CETRAINER output.
 +
|-
 +
|saveTable(filename, protect, dontDeactivateDesignerForms)
 +
|Saves the current table to a file with explicit protection and designer-form behavior.
 +
|-
 +
|saveTable(stream)
 +
|Saves the current table to a stream object.
 +
|-
 +
|saveTable(stream, dontDeactivateDesignerForms)
 +
|Saves the current table to a stream object with explicit designer-form behavior.
 +
|}
 +
 
 +
===Examples===
 +
 
 +
====Save the current table as a CT file====
 +
<syntaxhighlight lang="lua" line>
 +
saveTable([[C:\\CheatTables\\example.ct]])
 +
</syntaxhighlight>
 +
 
 +
====Save the current table as a CETRAINER file====
 +
<syntaxhighlight lang="lua" line>
 +
saveTable([[C:\\CheatTables\\example.cetrainer]])
 +
</syntaxhighlight>
 +
 
 +
====Save a protected CETRAINER file====
 +
<syntaxhighlight lang="lua" line>
 +
saveTable([[C:\\CheatTables\\protected.cetrainer]], true)
 +
</syntaxhighlight>
 +
 
 +
====Save a table without deactivating designer forms====
 +
<syntaxhighlight lang="lua" line>
 +
saveTable([[C:\\CheatTables\\example.ct]], false, true)
 +
</syntaxhighlight>
 +
 
 +
====Ask the user where to save the table====
 +
<syntaxhighlight lang="lua" line>
 +
local dialog = createSaveDialog(nil)
 +
 
 +
dialog.Filter = "Cheat Engine Tables (*.CT)|*.CT|Cheat Engine Trainers (*.CETRAINER)|*.CETRAINER|All files (*.*)|*.*"
 +
dialog.DefaultExt = "ct"
 +
 
 +
if dialog.execute() then
 +
  saveTable(dialog.FileName)
 +
end
 +
 
 +
dialog.destroy()
 +
</syntaxhighlight>
 +
 
 +
====Save only if the filename is valid====
 +
<syntaxhighlight lang="lua" line>
 +
local filename = [[C:\\CheatTables\\backup.ct]]
 +
 
 +
if filename ~= "" then
 +
  saveTable(filename)
 +
end
 +
</syntaxhighlight>
 +
 
 +
====Create a timestamped backup====
 +
<syntaxhighlight lang="lua" line>
 +
local backupName = os.date([[C:\\CheatTables\\backup_%Y%m%d_%H%M%S.ct]])
 +
 
 +
saveTable(backupName)
 +
</syntaxhighlight>
 +
 
 +
====Save the current table to a MemoryStream====
 +
<syntaxhighlight lang="lua" line>
 +
local stream = createMemoryStream()
 +
 
 +
saveTable(stream)
 +
 
 +
print("Saved bytes: " .. tostring(stream.Size))
 +
 
 +
stream.destroy()
 +
</syntaxhighlight>
 +
 
 +
====Save the current table to a stream and then to disk====
 +
<syntaxhighlight lang="lua" line>
 +
local stream = createMemoryStream()
 +
 
 +
saveTable(stream)
  
{{LuaSeeAlso}}
+
stream.Position = 0
 +
stream.saveToFile([[C:\\CheatTables\\stream_saved.ct]])
 +
 
 +
stream.destroy()
 +
</syntaxhighlight>
 +
 
 +
====Save to a stream without deactivating designer forms====
 +
<syntaxhighlight lang="lua" line>
 +
local stream = createMemoryStream()
 +
 
 +
saveTable(stream, true)
 +
 
 +
print("Saved bytes: " .. tostring(stream.Size))
 +
 
 +
stream.destroy()
 +
</syntaxhighlight>
 +
 
 +
====Store the current table as an embedded table file====
 +
<syntaxhighlight lang="lua" line>
 +
local stream = createMemoryStream()
 +
 
 +
saveTable(stream)
 +
stream.Position = 0
 +
 
 +
local tableFile = createTableFile("backup.ct")
 +
tableFile.Stream.copyFrom(stream, stream.Size)
 +
 
 +
stream.destroy()
 +
</syntaxhighlight>
 +
 
 +
====Replace an existing backup file====
 +
<syntaxhighlight lang="lua" line>
 +
local filename = [[C:\\CheatTables\\backup.ct]]
 +
 
 +
if fileExists(filename) then
 +
  deleteFile(filename)
 +
end
  
=== Related Functions ===
+
saveTable(filename)
* [[Lua:findTableFile|loadTable]]
+
</syntaxhighlight>
  
=== Related Classes ===
+
{{LuaSeeAlso}}
* [[Lua:Class:TableFile|TableFile]]
 
* [[Lua:Class:MemoryStream|MemoryStream]]
 

Revision as of 19:01, 25 June 2026

<> Lua API Reference

function saveTable(filename, protect OPTIONAL, dontDeactivateDesignerForms OPTIONAL) : void

<> Lua API Reference

function saveTable(stream, dontDeactivateDesignerForms OPTIONAL) : void

Saves the current Cheat Engine table.

saveTable can save the table to a file path or to a stream object. When saving to a filename with the .CETRAINER extension, the optional protect parameter can be used to protect the trainer from being read normally.

Function Parameters

Parameter Type Description
filename String The path where the current table should be saved. Usually this is a .CT or .CETRAINER file.
stream Stream A stream object the current table should be saved to.
protect Boolean OPTIONAL Only used by the filename overload. If true and filename has the .CETRAINER extension, the trainer is protected from being read normally.
dontDeactivateDesignerForms Boolean OPTIONAL If true, designer forms are not deactivated while saving the table.

Returns

This function does not return a value.

Description

saveTable saves the currently loaded Cheat Engine table.

When called with a filename, Cheat Engine writes the current table to disk. When called with a stream, Cheat Engine writes the table data to the given stream object.

The protect parameter only applies to the filename overload and is intended for protected .CETRAINER output.

Save Modes

Call Behavior
saveTable(filename) Saves the current table to a file.
saveTable(filename, protect) Saves the current table to a file and optionally protects .CETRAINER output.
saveTable(filename, protect, dontDeactivateDesignerForms) Saves the current table to a file with explicit protection and designer-form behavior.
saveTable(stream) Saves the current table to a stream object.
saveTable(stream, dontDeactivateDesignerForms) Saves the current table to a stream object with explicit designer-form behavior.

Examples

Save the current table as a CT file

1 saveTable([[C:\\CheatTables\\example.ct]])

Save the current table as a CETRAINER file

1 saveTable([[C:\\CheatTables\\example.cetrainer]])

Save a protected CETRAINER file

1 saveTable([[C:\\CheatTables\\protected.cetrainer]], true)

Save a table without deactivating designer forms

1 saveTable([[C:\\CheatTables\\example.ct]], false, true)

Ask the user where to save the table

 1 local dialog = createSaveDialog(nil)
 2 
 3 dialog.Filter = "Cheat Engine Tables (*.CT)|*.CT|Cheat Engine Trainers (*.CETRAINER)|*.CETRAINER|All files (*.*)|*.*"
 4 dialog.DefaultExt = "ct"
 5 
 6 if dialog.execute() then
 7   saveTable(dialog.FileName)
 8 end
 9 
10 dialog.destroy()

Save only if the filename is valid

1 local filename = [[C:\\CheatTables\\backup.ct]]
2 
3 if filename ~= "" then
4   saveTable(filename)
5 end

Create a timestamped backup

1 local backupName = os.date([[C:\\CheatTables\\backup_%Y%m%d_%H%M%S.ct]])
2 
3 saveTable(backupName)

Save the current table to a MemoryStream

1 local stream = createMemoryStream()
2 
3 saveTable(stream)
4 
5 print("Saved bytes: " .. tostring(stream.Size))
6 
7 stream.destroy()

Save the current table to a stream and then to disk

1 local stream = createMemoryStream()
2 
3 saveTable(stream)
4 
5 stream.Position = 0
6 stream.saveToFile([[C:\\CheatTables\\stream_saved.ct]])
7 
8 stream.destroy()

Save to a stream without deactivating designer forms

1 local stream = createMemoryStream()
2 
3 saveTable(stream, true)
4 
5 print("Saved bytes: " .. tostring(stream.Size))
6 
7 stream.destroy()

Store the current table as an embedded table file

1 local stream = createMemoryStream()
2 
3 saveTable(stream)
4 stream.Position = 0
5 
6 local tableFile = createTableFile("backup.ct")
7 tableFile.Stream.copyFrom(stream, stream.Size)
8 
9 stream.destroy()

Replace an existing backup file

1 local filename = [[C:\\CheatTables\\backup.ct]]
2 
3 if fileExists(filename) then
4   deleteFile(filename)
5 end
6 
7 saveTable(filename)

Main Pages

Core Lua documentation entry points

Lua
Script Engine