Difference between revisions of "Lua:createTableFile"

From Cheat Engine
Jump to navigation Jump to search
m (Reverted edits by This content is not available (Talk) to last revision by TheyCallMeTim13)
(Major overhaul of the post.)
Line 1: Line 1:
 
[[Category:Lua]]
 
[[Category:Lua]]
'''function''' createTableFile(''fileName'', ''filePath'' OPTIONAL) ''':''' TableFile
+
{{CodeBox|'''function''' createTableFile(''fileName'', ''filePath'' OPTIONAL) ''':''' TableFile}}
  
Creates and adds a new [[Lua:Class:TableFile|TableFile]] object representing a file stored with the cheat table.
+
Creates a new [[Lua:Class:TableFile|TableFile]] object and adds it to the current cheat table.
You can retrieve the data as a [[Lua:Class:MemoryStream|MemoryStream]] or save it to disk.
+
 
 +
If filePath is specified, Cheat Engine reads the file from disk and embeds its contents in the table. If filePath is omitted, an empty table file is created.
  
 
===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
Line 13: Line 14:
 
|fileName
 
|fileName
 
|String
 
|String
|The name of the file to be added to the cheat table
+
|The name the file will have inside the cheat table.
 
|-
 
|-
 
|filePath
 
|filePath
|String
+
|String OPTIONAL
|The path to the file to be added to the cheat table
+
|The path to the file on disk. If omitted, a blank table file is created.
 
|}
 
|}
  
== Examples ==
+
===Returns===
<pre>
+
[[Lua:Class:TableFile|TableFile]] — The created table file object.
local file = createTableFile('protected.wav')
+
 
 +
===Description===
 +
createTableFile adds a file entry to the current cheat table.
 +
 
 +
This is useful for packaging resources with a table, such as sounds, images, binary data, configuration files, or other files used by Lua scripts. The file can later be retrieved with [[Lua:findTableFile|findTableFile]], saved back to disk, or accessed through its [[Lua:Class:MemoryStream|MemoryStream]].
 +
 
 +
===Examples===
 +
 
 +
====Create a blank table file====
 +
<syntaxhighlight lang="lua" line>
 +
local file = createTableFile("runtime.dat")
 +
 
 +
print("Created table file: " .. file.Name)
 +
</syntaxhighlight>
 +
 
 +
====Create a table file from disk====
 +
<syntaxhighlight lang="lua" line>
 +
local file = createTableFile("protected.wav", [[C:\\SomeDirectory\\protected.wav]])
 +
 
 +
print("Added table file: " .. file.Name)
 +
</syntaxhighlight>
 +
 
 +
====Play an embedded sound====
 +
<syntaxhighlight lang="lua" line>
 +
local file = createTableFile("protected.wav", [[C:\\SomeDirectory\\protected.wav]])
 +
 
 
playSound(file)
 
playSound(file)
</pre>
+
</syntaxhighlight>
<pre><nowiki>
+
 
local file = createTableFile('protected.wav', [[C:\SomeDirectory\MySounds]])
+
====Create the file only if it does not already exist====
playSound(file)
+
<syntaxhighlight lang="lua" line>
</nowiki></pre>
+
local file = findTableFile("config.json")
 +
 
 +
if file == nil then
 +
  file = createTableFile("config.json", [[C:\\SomeDirectory\\config.json]])
 +
end
 +
 
 +
print("Using table file: " .. file.Name)
 +
</syntaxhighlight>
 +
 
 +
====Replace an existing table file====
 +
<syntaxhighlight lang="lua" line>
 +
local oldFile = findTableFile("config.json")
 +
 
 +
if oldFile ~= nil then
 +
  oldFile.delete()
 +
end
 +
 
 +
local newFile = createTableFile("config.json", [[C:\\SomeDirectory\\config.json]])
 +
 
 +
print("Replaced table file: " .. newFile.Name)
 +
</syntaxhighlight>
 +
 
 +
====Save the created table file to disk====
 +
<syntaxhighlight lang="lua" line>
 +
local file = createTableFile("data.bin", [[C:\\SomeDirectory\\data.bin]])
 +
 
 +
file.saveToFile([[C:\\Temp\\data_copy.bin]])
 +
</syntaxhighlight>
 +
 
 +
====Read the created file as a MemoryStream====
 +
<syntaxhighlight lang="lua" line>
 +
local file = createTableFile("data.bin", [[C:\\SomeDirectory\\data.bin]])
 +
local stream = file.getData()
 +
 
 +
print("Stream size: " .. tostring(stream.Size))
 +
</syntaxhighlight>
 +
 
 +
====Access the Stream property====
 +
<syntaxhighlight lang="lua" line>
 +
local file = createTableFile("data.bin", [[C:\\SomeDirectory\\data.bin]])
 +
local stream = file.Stream
 +
 
 +
stream.Position = 0
 +
 
 +
print("Stream size: " .. tostring(stream.Size))
 +
</syntaxhighlight>
 +
 
 +
====Create multiple table files====
 +
<syntaxhighlight lang="lua" line>
 +
local files = {
 +
  { name = "startup.wav", path = [[C:\\Sounds\\startup.wav]] },
 +
  { name = "success.wav", path = [[C:\\Sounds\\success.wav]] },
 +
  { name = "failed.wav",  path = [[C:\\Sounds\\failed.wav]] }
 +
}
 +
 
 +
for i, entry in ipairs(files) do
 +
  if findTableFile(entry.name) == nil then
 +
    createTableFile(entry.name, entry.path)
 +
  end
 +
end
 +
</syntaxhighlight>
 +
 
 +
====Use a helper function====
 +
<syntaxhighlight lang="lua" line>
 +
local function ensureTableFile(name, path)
 +
  local file = findTableFile(name)
 +
 
 +
  if file == nil then
 +
    file = createTableFile(name, path)
 +
  end
 +
 
 +
  return file
 +
end
 +
 
 +
local sound = ensureTableFile("protected.wav", [[C:\\SomeDirectory\\protected.wav]])
  
=== Adding a File ===
+
playSound(sound)
 +
</syntaxhighlight>
  
To add a file to your table, open the '''Table''' menu and pick '''Add File''':
+
===Adding a File through the GUI===
 +
To add a file to your table manually, open the '''Table''' menu and choose '''Add File'''.
  
 
[[Image:AddFile.png]]
 
[[Image:AddFile.png]]
  
Pick the file and it will be added to the table with the same name and show
+
Select the file you want to embed. Cheat Engine adds it to the table using the original filename.
up as a separate entry below '''Add file''' in the '''Table''' menu. From that
+
 
you can rename it, save it out to disk, or delete it:
+
After the file has been added, it appears as a separate entry below '''Add file''' in the '''Table''' menu. From there, you can rename it, save it to disk, or delete it.
  
 
[[Image:AddedFileMenu.png]]
 
[[Image:AddedFileMenu.png]]
 
  
 
{{LuaSeeAlso}}
 
{{LuaSeeAlso}}
 
=== Related Functions ===
 
* [[Lua:findTableFile|findTableFile]]
 
 
=== Related Classes ===
 
* [[Lua:Class:TableFile|TableFile]]
 
* [[Lua:Class:MemoryStream|MemoryStream]]
 

Revision as of 18:57, 25 June 2026

<> Lua API Reference

function createTableFile(fileName, filePath OPTIONAL) : TableFile

Creates a new TableFile object and adds it to the current cheat table.

If filePath is specified, Cheat Engine reads the file from disk and embeds its contents in the table. If filePath is omitted, an empty table file is created.

Function Parameters

Parameter Type Description
fileName String The name the file will have inside the cheat table.
filePath String OPTIONAL The path to the file on disk. If omitted, a blank table file is created.

Returns

TableFile — The created table file object.

Description

createTableFile adds a file entry to the current cheat table.

This is useful for packaging resources with a table, such as sounds, images, binary data, configuration files, or other files used by Lua scripts. The file can later be retrieved with findTableFile, saved back to disk, or accessed through its MemoryStream.

Examples

Create a blank table file

1 local file = createTableFile("runtime.dat")
2 
3 print("Created table file: " .. file.Name)

Create a table file from disk

1 local file = createTableFile("protected.wav", [[C:\\SomeDirectory\\protected.wav]])
2 
3 print("Added table file: " .. file.Name)

Play an embedded sound

1 local file = createTableFile("protected.wav", [[C:\\SomeDirectory\\protected.wav]])
2 
3 playSound(file)

Create the file only if it does not already exist

1 local file = findTableFile("config.json")
2 
3 if file == nil then
4   file = createTableFile("config.json", [[C:\\SomeDirectory\\config.json]])
5 end
6 
7 print("Using table file: " .. file.Name)

Replace an existing table file

1 local oldFile = findTableFile("config.json")
2 
3 if oldFile ~= nil then
4   oldFile.delete()
5 end
6 
7 local newFile = createTableFile("config.json", [[C:\\SomeDirectory\\config.json]])
8 
9 print("Replaced table file: " .. newFile.Name)

Save the created table file to disk

1 local file = createTableFile("data.bin", [[C:\\SomeDirectory\\data.bin]])
2 
3 file.saveToFile([[C:\\Temp\\data_copy.bin]])

Read the created file as a MemoryStream

1 local file = createTableFile("data.bin", [[C:\\SomeDirectory\\data.bin]])
2 local stream = file.getData()
3 
4 print("Stream size: " .. tostring(stream.Size))

Access the Stream property

1 local file = createTableFile("data.bin", [[C:\\SomeDirectory\\data.bin]])
2 local stream = file.Stream
3 
4 stream.Position = 0
5 
6 print("Stream size: " .. tostring(stream.Size))

Create multiple table files

 1 local files = {
 2   { name = "startup.wav", path = [[C:\\Sounds\\startup.wav]] },
 3   { name = "success.wav", path = [[C:\\Sounds\\success.wav]] },
 4   { name = "failed.wav",  path = [[C:\\Sounds\\failed.wav]] }
 5 }
 6 
 7 for i, entry in ipairs(files) do
 8   if findTableFile(entry.name) == nil then
 9     createTableFile(entry.name, entry.path)
10   end
11 end

Use a helper function

 1 local function ensureTableFile(name, path)
 2   local file = findTableFile(name)
 3 
 4   if file == nil then
 5     file = createTableFile(name, path)
 6   end
 7 
 8   return file
 9 end
10 
11 local sound = ensureTableFile("protected.wav", [[C:\\SomeDirectory\\protected.wav]])
12 
13 playSound(sound)

Adding a File through the GUI

To add a file to your table manually, open the Table menu and choose Add File.

AddFile.png

Select the file you want to embed. Cheat Engine adds it to the table using the original filename.

After the file has been added, it appears as a separate entry below Add file in the Table menu. From there, you can rename it, save it to disk, or delete it.

AddedFileMenu.png

Main Pages

Core Lua documentation entry points

Lua
Script Engine