<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://wiki.gideros.rocks/index.php?action=history&amp;feed=atom&amp;title=Tuto_Gideros_Game_Template1_Part_3_Main</id>
	<title>Tuto Gideros Game Template1 Part 3 Main - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.gideros.rocks/index.php?action=history&amp;feed=atom&amp;title=Tuto_Gideros_Game_Template1_Part_3_Main"/>
	<link rel="alternate" type="text/html" href="https://wiki.gideros.rocks/index.php?title=Tuto_Gideros_Game_Template1_Part_3_Main&amp;action=history"/>
	<updated>2026-11-18T01:31:33Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://wiki.gideros.rocks/index.php?title=Tuto_Gideros_Game_Template1_Part_3_Main&amp;diff=23845&amp;oldid=prev</id>
		<title>MoKaLux: Created page with &quot;__TOC__ == the main.lua file == In the &#039;&#039;main.lua&#039;&#039; file we will start shaping our game, that is: * give our window a title and position * configure our save system: the actua...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.gideros.rocks/index.php?title=Tuto_Gideros_Game_Template1_Part_3_Main&amp;diff=23845&amp;oldid=prev"/>
		<updated>2024-10-25T18:27:02Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;__TOC__ == the main.lua file == In the &amp;#039;&amp;#039;main.lua&amp;#039;&amp;#039; file we will start shaping our game, that is: * give our window a title and position * configure our save system: the actua...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;__TOC__&lt;br /&gt;
== the main.lua file ==&lt;br /&gt;
In the &amp;#039;&amp;#039;main.lua&amp;#039;&amp;#039; file we will start shaping our game, that is:&lt;br /&gt;
* give our window a title and position&lt;br /&gt;
* configure our save system: the actual data (game difficulty, sound levels, player controls) and the load and save functions&lt;br /&gt;
* a preloader to show a splash screen/logo. It can also be used to preload assets&lt;br /&gt;
* finally we &amp;quot;launch&amp;quot; the game&lt;br /&gt;
&lt;br /&gt;
The code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- windows title and position&lt;br /&gt;
if not application:isPlayerMode() then&lt;br /&gt;
	application:set(&amp;quot;windowTitle&amp;quot;, &amp;quot;GIDEROS GAME TEMPLATE&amp;quot;)&lt;br /&gt;
	application:set(&amp;quot;windowPosition&amp;quot;, (screenwidth - myappwidth) * 0.5, (screenheight - myappheight) * 0.4)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- global prefs functions&lt;br /&gt;
function myLoadPrefs(xconfigfilepath)&lt;br /&gt;
	local mydata = getData(xconfigfilepath) -- try to read information from file&lt;br /&gt;
	if not mydata then -- if no prefs file, create it&lt;br /&gt;
		mydata = {}&lt;br /&gt;
		-- set prefs&lt;br /&gt;
		mydata.g_language = g_language&lt;br /&gt;
--		mydata.g_currlevel = g_currlevel&lt;br /&gt;
		mydata.g_difficulty = g_difficulty&lt;br /&gt;
		mydata.g_bgmvolume = g_bgmvolume&lt;br /&gt;
		mydata.g_sfxvolume = g_sfxvolume&lt;br /&gt;
		-- controls&lt;br /&gt;
		mydata.g_keyleft = g_keyleft&lt;br /&gt;
		mydata.g_keyright = g_keyright&lt;br /&gt;
		mydata.g_keyup = g_keyup&lt;br /&gt;
		mydata.g_keydown = g_keydown&lt;br /&gt;
		mydata.g_keyaction1 = g_keyaction1&lt;br /&gt;
		mydata.g_keyaction2 = g_keyaction2&lt;br /&gt;
		mydata.g_keyaction3 = g_keyaction3&lt;br /&gt;
		-- save prefs&lt;br /&gt;
		saveData(xconfigfilepath, mydata) -- create file and save datas&lt;br /&gt;
	else -- prefs file exists, use it!&lt;br /&gt;
		-- set prefs&lt;br /&gt;
		g_language = mydata.g_language&lt;br /&gt;
--		g_currlevel = mydata.g_currlevel&lt;br /&gt;
		g_difficulty = mydata.g_difficulty&lt;br /&gt;
		g_bgmvolume = mydata.g_bgmvolume&lt;br /&gt;
		g_sfxvolume = mydata.g_sfxvolume&lt;br /&gt;
		-- controls&lt;br /&gt;
		g_keyleft = mydata.g_keyleft&lt;br /&gt;
		g_keyright = mydata.g_keyright&lt;br /&gt;
		g_keyup = mydata.g_keyup&lt;br /&gt;
		g_keydown = mydata.g_keydown&lt;br /&gt;
		g_keyaction1 = mydata.g_keyaction1&lt;br /&gt;
		g_keyaction2 = mydata.g_keyaction2&lt;br /&gt;
		g_keyaction3 = mydata.g_keyaction3&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function mySavePrefs(xconfigfilepath)&lt;br /&gt;
	local mydata = {} -- clear the table&lt;br /&gt;
	-- set prefs&lt;br /&gt;
	mydata.g_language = g_language&lt;br /&gt;
--	mydata.g_currlevel = g_currlevel&lt;br /&gt;
	mydata.g_difficulty = g_difficulty&lt;br /&gt;
	mydata.g_bgmvolume = g_bgmvolume&lt;br /&gt;
	mydata.g_sfxvolume = g_sfxvolume&lt;br /&gt;
	-- controls&lt;br /&gt;
	mydata.g_keyleft = g_keyleft&lt;br /&gt;
	mydata.g_keyright = g_keyright&lt;br /&gt;
	mydata.g_keyup = g_keyup&lt;br /&gt;
	mydata.g_keydown = g_keydown&lt;br /&gt;
	mydata.g_keyaction1 = g_keyaction1&lt;br /&gt;
	mydata.g_keyaction2 = g_keyaction2&lt;br /&gt;
	mydata.g_keyaction3 = g_keyaction3&lt;br /&gt;
	-- save prefs&lt;br /&gt;
	saveData(xconfigfilepath, mydata) -- save new data&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- prefs file and initial default global prefs values&lt;br /&gt;
g_configfilepath = &amp;quot;|D|configfile.txt&amp;quot;&lt;br /&gt;
g_language = application:getLanguage()&lt;br /&gt;
g_totallevel = 3&lt;br /&gt;
g_currlevel = 1&lt;br /&gt;
g_difficulty = 1 -- 0=easy, 1=normal, 2=hard&lt;br /&gt;
g_bgmvolume = 50 -- 0-100&lt;br /&gt;
g_sfxvolume = 20 -- 0-100&lt;br /&gt;
g_keyleft = KeyCode.LEFT&lt;br /&gt;
g_keyright = KeyCode.RIGHT&lt;br /&gt;
g_keyup = KeyCode.UP&lt;br /&gt;
g_keydown = KeyCode.DOWN&lt;br /&gt;
g_keyaction1 = KeyCode.D -- eg.: punch1&lt;br /&gt;
g_keyaction2 = KeyCode.S -- eg.: punch2&lt;br /&gt;
g_keyaction3 = KeyCode.Q -- eg.: jump/jump punch1&lt;br /&gt;
&lt;br /&gt;
-- load saved prefs from file (|D|configfile.txt)&lt;br /&gt;
myLoadPrefs(g_configfilepath)&lt;br /&gt;
&lt;br /&gt;
-- ************&lt;br /&gt;
-- APP LOADING&lt;br /&gt;
-- ************&lt;br /&gt;
local logo = Bitmap.new(Texture.new(&amp;quot;gfx/logo.png&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
local function drawLogo()&lt;br /&gt;
	logo:setAnchorPoint(0.5, 0.5)&lt;br /&gt;
	logo:setPosition(myappwidth/2+myappleft, myappheight/2)&lt;br /&gt;
	stage:addChild(logo)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function preloader() -- loading textures and sounds when app is starting&lt;br /&gt;
	stage:removeEventListener(Event.ENTER_FRAME, preloader)&lt;br /&gt;
	-- you can preload your assets here or preload them calling a Class function eg.:&lt;br /&gt;
--	MenuScene.preload()&lt;br /&gt;
--	SoundManager.preload()&lt;br /&gt;
	-- timer&lt;br /&gt;
	local timer = Timer.new(3000, 1) -- show logo: duration&lt;br /&gt;
	timer:addEventListener(Event.TIMER, function()&lt;br /&gt;
		stage:removeAllListeners()&lt;br /&gt;
		for i = stage:getNumChildren(), 1, -1 do&lt;br /&gt;
			stage:removeChildAt(i)&lt;br /&gt;
		end collectgarbage()&lt;br /&gt;
		stage:addChild(Transitions.new(Menu.new())) -- next scene&lt;br /&gt;
	end)&lt;br /&gt;
	timer:start()&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- start&lt;br /&gt;
drawLogo()&lt;br /&gt;
stage:addEventListener(Event.ENTER_FRAME, preloader)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Code comments ==&lt;br /&gt;
There is no particular way to write your code. I tried to put each section where it made sense. It is important to write comments to separate each part of the code.&lt;br /&gt;
&lt;br /&gt;
=== prefs file and initial default global prefs values ===&lt;br /&gt;
All are global variables because they will be used in many scenes of our game.&lt;br /&gt;
&lt;br /&gt;
Please note that not all those variables will be saved to the prefs file, for example it makes sense to save the new key mapping for the player and the sound volume to file.&lt;br /&gt;
&lt;br /&gt;
You need to add the variables that will be saved to file in the &amp;#039;&amp;#039;&amp;#039;load&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;save&amp;#039;&amp;#039;&amp;#039; functions. If it is the first time the game is launched then default values are assigned and used, eg.: g_difficulty = 1, g_bgmvolume = 50, g_keyleft = KeyCode.LEFT, ...&lt;br /&gt;
&lt;br /&gt;
=== App loading ===&lt;br /&gt;
We set up a logo for our game, it can be your studio logo or anything you like.&lt;br /&gt;
&lt;br /&gt;
Then there are those two functions to load our logo and a preloader function where we show our logo for a couple of seconds. You could also preload assets in the preloader function.&lt;br /&gt;
&lt;br /&gt;
When the timer ends we load the next scene using some transitions (please note the next scene doesn&amp;#039;t exist yet!)&lt;br /&gt;
&lt;br /&gt;
=== Start the app ===&lt;br /&gt;
Finally we can start the app by calling the two functions above: drawLogo() and preloader()&lt;br /&gt;
&lt;br /&gt;
== Next? ==&lt;br /&gt;
We are ready to create the scenes for our game. Let&amp;#039;s start with the transition scene.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Prev.: [[Tuto Gideros Game Template1 Part 2 Init]]&amp;lt;/br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Next: [[Tuto Gideros Game Template1 Part 4 Transitions]]&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;[[Tutorial - Gideros Game Template1]]&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
{{GIDEROS IMPORTANT LINKS}}&lt;/div&gt;</summary>
		<author><name>MoKaLux</name></author>
	</entry>
</feed>