<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.frozenbyte.com/index.php?action=history&amp;feed=atom&amp;title=Lua_Scripts</id>
	<title>Lua Scripts - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.frozenbyte.com/index.php?action=history&amp;feed=atom&amp;title=Lua_Scripts"/>
	<link rel="alternate" type="text/html" href="https://wiki.frozenbyte.com/index.php?title=Lua_Scripts&amp;action=history"/>
	<updated>2026-05-16T14:01:05Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>https://wiki.frozenbyte.com/index.php?title=Lua_Scripts&amp;diff=283&amp;oldid=prev</id>
		<title>AnteroFB: Created page with &quot;= Lua Scripts =  &quot;As a general rule, you can do anything in Lua.&quot;  == Creating Custom Scripts ==  Start by creating a file, like myscripts.lua to data/script folder (you can u...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.frozenbyte.com/index.php?title=Lua_Scripts&amp;diff=283&amp;oldid=prev"/>
		<updated>2018-06-15T08:15:31Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;= Lua Scripts =  &amp;quot;As a general rule, you can do anything in Lua.&amp;quot;  == Creating Custom Scripts ==  Start by creating a file, like myscripts.lua to data/script folder (you can u...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Lua Scripts =&lt;br /&gt;
&lt;br /&gt;
&amp;quot;As a general rule, you can do anything in Lua.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Creating Custom Scripts ==&lt;br /&gt;
&lt;br /&gt;
Start by creating a file, like myscripts.lua to data/script folder (you can use sub-folders as well). Add following lines to start of the file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
module(..., package.seeall)&lt;br /&gt;
debug.ReloadScripts.allowReload(...)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then edit init.lua in data/script and add following line to the end of the file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
require &amp;quot;myscripts&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You need to restart the editor to get init.lua processed again, but after that you can add functions to myscript.lua and access them in namespace or module names &amp;#039;&amp;#039;&amp;#039;myscripts&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Namespaces and naming ==&lt;br /&gt;
&lt;br /&gt;
=== Identifiers ===&lt;br /&gt;
&lt;br /&gt;
The naming convention used here is [http://en.wikipedia.org/wiki/CamelCase lowerCamelCase] for module, while file names are &amp;#039;&amp;#039;lowercase_separated_with_underscores&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
This means that if you want a module to be named &amp;#039;&amp;#039;&amp;#039;myScripts&amp;#039;&amp;#039;&amp;#039; the associated file will be named &amp;#039;&amp;#039;my_scripts.lua&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
require &amp;quot;myScripts&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Namespaces and subfolders ===&lt;br /&gt;
&lt;br /&gt;
If you want to put your scripts under a specific folder tree, let&amp;#039;s say &amp;#039;&amp;#039;foo/bar/my_scripts.lua&amp;#039;&amp;#039;, the folders will be separated by a dot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
require &amp;quot;foo.bar.myScripts&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Adding Functions to your script ==&lt;br /&gt;
&lt;br /&gt;
So let&amp;#039;s add a function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function findEntity(name)&lt;br /&gt;
	local thingy = common.CommonUtils.getSceneInstanceManager():findInstanceByName(name)&lt;br /&gt;
	if thingy then&lt;br /&gt;
		logger:info(&amp;quot;Myscript.findEntity: Found &amp;quot; .. name)&lt;br /&gt;
	else&lt;br /&gt;
		logger:error(&amp;quot;Myscript.findEntity: Could not find &amp;quot; .. name)&lt;br /&gt;
	end&lt;br /&gt;
	return thingy&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you have editor running, you need to reloadScripts before the changes take effect. So in console (accessed by pressing F8) type reloadScripts() and hit enter (You can also access it under the &amp;quot;Debug-&amp;gt;Reload Scripts&amp;quot; menu). Remember to reload scripts everytime you launch the game for a test, Or restart the editor and the changes will take effect permanently.&lt;br /&gt;
&lt;br /&gt;
Now you can test the newly created findEntity function by typing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
tmpVar = myscripts.findEntity(&amp;quot;Foobar&amp;quot;)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the script prints to Error List, so you can see if it found anything. If it did, variable tmpVar will now contain it. You can see some of it by simply typing in console&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
return tmpVar&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing object properties ==&lt;br /&gt;
&lt;br /&gt;
Finding what you are looking for is not easy.&lt;br /&gt;
&lt;br /&gt;
You can figure out many things by using TAB autocompletion in console, and know that accessors for everything, such as components and properties, are available.&lt;br /&gt;
&lt;br /&gt;
For example, a snippet accessing an object and moving it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local object= findEntity(name)&lt;br /&gt;
local transformComponent= object:getTransformComponent()&lt;br /&gt;
transformComponent.setPosition(VC3(10,0,0))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Check out other scripts in the &amp;quot;scripts&amp;quot; folder and [[Helpful Lua Snippets]] page!&lt;br /&gt;
&lt;br /&gt;
== Trigger Lua Call ==&lt;br /&gt;
&lt;br /&gt;
=== CustomLuaExpressionPropertyAnimationEntity ===&lt;br /&gt;
&lt;br /&gt;
This entity, found under the &amp;#039;&amp;#039;&amp;#039;Entity&amp;#039;&amp;#039;&amp;#039;-&amp;gt;&amp;#039;&amp;#039;&amp;#039;PropertyAnimationEntity&amp;#039;&amp;#039;&amp;#039; tree, allows you to call a Lua script.&lt;br /&gt;
&lt;br /&gt;
Change its &amp;#039;&amp;#039;Expression&amp;#039;&amp;#039; property to call your function, for example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
myscripts.myFunction()&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Change its &amp;#039;&amp;#039;Immediate&amp;#039;&amp;#039; property to trigger the call when the game is loaded.&lt;br /&gt;
&lt;br /&gt;
=== TriggerToLuaCallEntity ===&lt;br /&gt;
&lt;br /&gt;
This entity can be found under the &amp;#039;&amp;#039;&amp;#039;Entity&amp;#039;&amp;#039;&amp;#039;-&amp;gt;&amp;#039;&amp;#039;&amp;#039;TriggerEntity&amp;#039;&amp;#039;&amp;#039; tree.&lt;br /&gt;
&lt;br /&gt;
When triggered by a collector (see [[Gameplay Events]]), it executes a lua expression. You can then link it to an &amp;#039;&amp;#039;&amp;#039;AreaCollector&amp;#039;&amp;#039;&amp;#039;, or a lever, or anything.&lt;br /&gt;
&lt;br /&gt;
==== TriggerToLuaCallComponent&amp;#039;s properties ====&lt;br /&gt;
* &amp;#039;&amp;#039;Expression&amp;#039;&amp;#039; : a lua expression, like &amp;quot;myscripts.myFunction()&amp;quot;&lt;br /&gt;
* &amp;#039;&amp;#039;RunOnce&amp;#039;&amp;#039; : allow script to be ran only once&lt;br /&gt;
&lt;br /&gt;
== Pro Tips ==&lt;br /&gt;
&lt;br /&gt;
=== Console Logs ===&lt;br /&gt;
You use the console (F8) to try your script out, and find out what component to call and how to modify it. After you finally get what you want, you can copy-paste it from the console log file into a new lua scripts.&lt;br /&gt;
&lt;br /&gt;
Console log file is accessible under &amp;#039;&amp;#039;&amp;lt;nowiki&amp;gt;{{Trine 2 folder}}\log\console.log&amp;lt;/nowiki&amp;gt;&amp;#039;&amp;#039;, which would be, by default:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C:\Program Files (x86)\Steam\steamapps\common\Trine 2\log\console.log&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== TrineFUILoadingScreenComponent ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Custom values&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Public editor users might want to set their own images and stories via Lua like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- Making loading screen use custom values&lt;br /&gt;
trineLoadingScreenComponent:setInitializeToDefaultValues(false)&lt;br /&gt;
&lt;br /&gt;
-- Setting image file paths (not sure if modders can add images)&lt;br /&gt;
trineLoadingScreenComponent:setChapterImagePath(&amp;quot;path/to/the/chapter/image.png&amp;quot;)&lt;br /&gt;
trineLoadingScreenComponent:setChapterTextPath(&amp;quot;path/to/the/chapter/text/image.png&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
-- Setting custom story texts (should not be used with ChapterTextPath as it occupies the same space as these)&lt;br /&gt;
trineLoadingScreenComponent:setTitleNumber(&amp;quot;Part 13&amp;quot;)&lt;br /&gt;
trineLoadingScreenComponent:setTitleText(&amp;quot;This is a title&amp;quot;)&lt;br /&gt;
trineLoadingScreenComponent:setStoryText(&amp;quot;Once upon a time...\n\n...and then...\n\nThe End!&amp;quot;)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TitleText and StoryText can also be locale keys if modders have acces to those.&lt;/div&gt;</summary>
		<author><name>AnteroFB</name></author>
	</entry>
</feed>