Tweakscene, tweak your game variables while you play

After hours of changing variables and building each time to balance my game. I decided to create a tweakscene.

You can use this to change the values of some of your game variables while you play. This allows you to change them, play the game and take notes where they should be for perfect gameplay.

Please forgive my code as I am a beginner. I could not for the life of me get the for loop to work on the textfield and it is impossible to debug since it doesn’t work on the simulator. I am open to any suggestion for improvement. I have set it to 5 variables for now but as you will see from the screen. It could be easily adapted to 10 or even 15 per screen. I just want to get that darn for loop to work before I do that.

Thanks to Ansca for the code on textfield.

I hope this is as useful to you guys as it is to me

[code]
–tweakscene Version 1 (beta) November 8, 2010
–Written by Amigoni

module(…, package.seeall)

–[[
TWEAKSCENE
I designed this code to help me tweak game parameters while playing the game, without
having to edit the code and build over and over. I hope you find it as useful as I have.
It saved me tons of hours while making my games.

INSTRUCTIONS:

  1. In your main.lua file enter copy and paste the following text.
    local tweakscene = require “tweakscene”

  2. In your Main Menu copy and paste the text below to place a button that goes to the tweaksceneGroup.
    you can substitute with your own button if you’d like


–PLACE THIS BUTTON ON YOUR MENU SCENE AND EDIT THE NECESSARY PARTS
–Return to Menu Button
tweakSceneButton = display.newRect ( 15, 15, 55, 25, 5) – Edit this to place it where you want it
tweakSceneButton:setFillColor (255,0,0,255)
tweaksceneGroup:insert(tweakSceneButton)

tweakSceneButtonText = display.newText(“TWEAK”, 16 , 13,“MarkerFelt-Thin”,20);
tweakSceneButtonText:setTextColor (255,255,255,255)
–Edit below to add this to your menuGroup
–menuGroup:insert(tweakSceneButtonText)

function tweakSceneButton.listener (event)

if (event.phase == “ended”) then
unloadme()
–Edit Below to make your menuGroup Reappear
–menuscene.menuGroup.isVisible = true

end
return true – This stop the event to the upper Object
end
tweakSceneButton:addEventListener(“touch”, tweakSceneButton.listener )

  1. Assign the variables below to the variables you want to tweak in your game
  2. Edit the description of the variables to your liking

Enjoy!!!
–]]

tweaksceneGroup = display.newGroup ()

–ENTER THE VARIABLES YOU WANT TO TWEAK
variable = {}
variable[1] = nil
variable[2] = nil
variable[3] = nil
variable[4] = nil
variable[5] = nil

–ENTER THE DESCRIPTIONS OF EACH VARIABLE
description = {}
description[1] = “”
description[2] = “”
description[3] = “”
description[4] = “”
description[5] = “”


–Handle the text Field
local function fieldHandler1( event )

if ( “began” == event.phase ) then
– This is the “keyboard has appeared” event
– In some cases you may want to adjust the interface when the keyboard appears.

elseif ( “ended” == event.phase ) then
– This event is called when the user stops editing a field: for example, when they touch a different field

elseif ( “submitted” == event.phase ) then
– This event occurs when the user presses the “return” key (if available) on the onscreen keyboard
jumpscene.stride = tField1.text
displaytext1 ()
– Hide keyboard
native.setKeyboardFocus( nil )

end

end

local function fieldHandler2( event )

if ( “began” == event.phase ) then
– This is the “keyboard has appeared” event
– In some cases you may want to adjust the interface when the keyboard appears.

elseif ( “ended” == event.phase ) then
– This event is called when the user stops editing a field: for example, when they touch a different field

elseif ( “submitted” == event.phase ) then
– This event occurs when the user presses the “return” key (if available) on the onscreen keyboard
jumpscene.stride = tField2.text
displaytext2 ()
– Hide keyboard
native.setKeyboardFocus( nil )

end

end

local function fieldHandler3( event )

if ( “began” == event.phase ) then
– This is the “keyboard has appeared” event
– In some cases you may want to adjust the interface when the keyboard appears.

elseif ( “ended” == event.phase ) then
– This event is called when the user stops editing a field: for example, when they touch a different field

elseif ( “submitted” == event.phase ) then
– This event occurs when the user presses the “return” key (if available) on the onscreen keyboard
jumpscene.stride = tField3.text
displaytext3 ()
– Hide keyboard
native.setKeyboardFocus( nil )

end

end

local function fieldHandler4( event )

if ( “began” == event.phase ) then
– This is the “keyboard has appeared” event
– In some cases you may want to adjust the interface when the keyboard appears.

elseif ( “ended” == event.phase ) then
– This event is called when the user stops editing a field: for example, when they touch a different field

elseif ( “submitted” == event.phase ) then
– This event occurs when the user presses the “return” key (if available) on the onscreen keyboard
jumpscene.stride = tField4.text
displaytext4 ()
– Hide keyboard
native.setKeyboardFocus( nil )

end

end

local function fieldHandler5( event )

if ( “began” == event.phase ) then
– This is the “keyboard has appeared” event
– In some cases you may want to adjust the interface when the keyboard appears.

elseif ( “ended” == event.phase ) then
– This event is called when the user stops editing a field: for example, when they touch a different field

elseif ( “submitted” == event.phase ) then
– This event occurs when the user presses the “return” key (if available) on the onscreen keyboard
jumpscene.stride = tField5.text
displaytext5 ()
– Hide keyboard
native.setKeyboardFocus( nil )

end

end


–Display the current values next to the text Field

function displaytext1()
local astring = tostring(tField1.text);
if not display_text_text1 then
display_text_text1 = display.newText(astring, tField1.x + 130, tField1.y,“MarkerFelt-Thin”,15);
display_text_text1:setTextColor(255,255,255);
display_text_text1.align = “right”
tweaksceneGroup:insert( display_text_text1 )
end
display_text_text1.text = astring;
end

function displaytext2()
local astring = tostring(tField2.text);
if not display_text_text2 then
display_text_text2 = display.newText(astring, tField2.x + 130, tField2.y,“MarkerFelt-Thin”,15);
display_text_text2:setTextColor(255,255,255);
display_text_text2.align = “right”
tweaksceneGroup:insert( display_text_text2 )
end
display_text_text2.text = astring;
end

function displaytext3()
local astring = tostring(tField3.text);
if not display_text_text3 then
display_text_text3 = display.newText(astring, tField3.x + 130, tField3.y,“MarkerFelt-Thin”,15);
display_text_text3:setTextColor(255,255,255);
display_text_text3.align = “right”
tweaksceneGroup:insert( display_text_text3 )
end
display_text_text3.text = astring;
end

function displaytext4()
local astring = tostring(tField4.text);
if not display_text_text4 then
display_text_text4 = display.newText(astring, tField4.x + 130, tField4.y,“MarkerFelt-Thin”,15);
display_text_text4:setTextColor(255,255,255);
display_text_text4.align = “right”
tweaksceneGroup:insert( display_text_text4 )
end
display_text_text4.text = astring;
end

function displaytext5()
local astring = tostring(tField5.text);
if not display_text_text5 then
display_text_text5 = display.newText(astring, tField5.x + 130, tField5.y,“MarkerFelt-Thin”,15);
display_text_text5:setTextColor(255,255,255);
display_text_text5.align = “right”
tweaksceneGroup:insert( display_text_text5 )
end
display_text_text5.text = astring;
end


–Loads all Assets
function loadme()

tField1 = native.newTextField( 0, 0, 100, 30, fieldHandler1 )
tField1.x = 20
tField1.y = 70
tField1.font = native.newFont( native.systemFontBold, 18 )
tField1.text = variable[1]

tField2 = native.newTextField( 0, 0, 100, 30, fieldHandler2 )
tField2.x = 20
tField2.y = 125
tField2.font = native.newFont( native.systemFontBold, 18 )
tField2.text = variable[2]

tField3 = native.newTextField( 0, 0, 100, 30, fieldHandler3 )
tField3.x = 20
tField3.y = 180
tField3.font = native.newFont( native.systemFontBold, 18 )
tField3.text = variable[3]

tField4 = native.newTextField( 0, 0, 100, 30, fieldHandler4 )
tField4.x = 20
tField4.y = 235
tField4.font = native.newFont( native.systemFontBold, 18 )
tField4.text = variable[4]

tField5 = native.newTextField( 0, 0, 100, 30, fieldHandler5 )
tField5.x = 20
tField5.y = 285
tField5.font = native.newFont( native.systemFontBold, 18 )
tField5.text = variable[5]

description1 = display.newText(description[1], tField1.x , tField1.y - 20,“MarkerFelt-Thin”,15);
description1:setTextColor(255,255,255,255)
description1.align = “center”
tweaksceneGroup:insert(description1)

description2 = display.newText(description[2], tField2.x , tField2.y - 20,“MarkerFelt-Thin”,15);
description2:setTextColor(255,255,255,255)
description2.align = “center”
tweaksceneGroup:insert(description2)

description3 = display.newText(description[3], tField3.x , tField3.y - 20,“MarkerFelt-Thin”,15);
description3:setTextColor(255,255,255,255)
description3.align = “center”
tweaksceneGroup:insert(description3)

description4 = display.newText(description[4], tField4.x , tField4.y - 20,“MarkerFelt-Thin”,15);
description4:setTextColor(255,255,255,255)
description4.align = “center”
tweaksceneGroup:insert(description4)

description5 = display.newText(description[5], tField5.x , tField5.y - 20,“MarkerFelt-Thin”,15);
description5:setTextColor(255,255,255,255)
description5.align = “center”
tweaksceneGroup:insert(description5)

displaytext1()
displaytext2()
displaytext3()
displaytext4()
displaytext5()

–Return to Menu Button
menuButton = display.newRect ( 15, 15, 55, 25, 5)
menuButton:setFillColor (255,0,0,255)
tweaksceneGroup:insert(menuButton)

menuButtonText = display.newText(“MENU”, 16 , 13,“MarkerFelt-Thin”,20);
menuButtonText:setTextColor (255,255,255,255)
tweaksceneGroup:insert(menuButtonText)

function menuButton.listener (event)

if (event.phase == “ended”) then
menuscene.menuGroup.isVisible = true
unloadme()
end
return true – This stop the event to the upper Object
end
menuButton:addEventListener(“touch”, menuButton.listener )
end

–Unload all Assets and Collect Garbage
function unloadme ()
–Remove Table Fields, they cannot be included in display Group
tField1:removeSelf()
tField1 = nil
tField2:removeSelf()
tField2 = nil
tField3:removeSelf()
tField3 = nil
tField4:removeSelf()
tField4 = nil
tField5:removeSelf()
tField5 = nil

display_text_text1:removeSelf()
display_text_text1 = nil
display_text_text2:removeSelf()
display_text_text2 = nil
display_text_text3:removeSelf()
display_text_text3 = nil
display_text_text4:removeSelf()
display_text_text4 = nil
display_text_text5:removeSelf()
display_text_text5 = nil

–Remove all Display Objects
for i=tweaksceneGroup.numChildren,1,-1 do
local child = tweaksceneGroup[i]
child.parent:remove( child )
child = nil
end

collectgarbage( “collect”)
end

/[code]

[import]uid: 8192 topic_id: 3494 reply_id: 303494[/import]

Upps. I can’t edit my original post but I missetyped "[/code] at the end and fixed an issue

  
module(..., package.seeall)  
  
--[[  
TWEAKSCENE INSTRUCTIONS  
I designed this code to help me tweak game parameters while playing the game, without   
having to edit the code and build over and over. I hope you find it as useful as I have.   
It saved me tons of hours while making my games.   
  
INSTRUCTION:  
1) In your main.lua file enter copy and paste the following text.   
local tweakscene = require "tweakscene"  
  
2) In your Main Menu copy and paste the text below to place a button that goes to the tweaksceneGroup.  
you can substitute with your own button if you'd like  
---------------------------------------------------------------  
--PLACE THIS BUTTON ON YOUR MENU SCENE AND EDIT THE NECESSARY PARTS  
--Return to Menu Button  
tweakSceneButton = display.newRect ( 15, 15, 55, 25, 5) -- Edit this to place it where you want it  
tweakSceneButton:setFillColor (255,0,0,255)  
tweaksceneGroup:insert(tweakSceneButton)  
  
tweakSceneButtonText = display.newText("TWEAK", 16 , 13,"MarkerFelt-Thin",20);  
tweakSceneButtonText:setTextColor (255,255,255,255)  
--Edit below to add this to your menuGroup  
--menuGroup:insert(tweakSceneButtonText)  
  
 function tweakSceneButton.listener (event)  
  
 if (event.phase == "ended") then   
 unloadme()  
 --Edit Below to make your menuGroup Reappear  
 --menuscene.menuGroup.isVisible = true  
  
 end  
 return true -- This stop the event to the upper Object  
 end  
tweakSceneButton:addEventListener("touch", tweakSceneButton.listener )  
---------------------------------------------------------------  
  
3) Assign the variables below to the variables you want to tweak in your game  
4) Edit the description of the variables to your liking  
  
Enjoy!!!  
--]]  
---------------------------------------------------------------  
tweaksceneGroup = display.newGroup ()  
  
--ENTER THE VARIABLES YOU WANT TO TWEAK  
variable = {}  
variable[1] = nil  
variable[2] = nil  
variable[3] = nil  
variable[4] = nil  
variable[5] = nil  
  
--ENTER THE DESCRIPTIONS OF EACH VARIABLE  
description = {}  
description[1] = ""  
description[2] = ""  
description[3] = ""  
description[4] = ""  
description[5] = ""  
  
---------------------------------------------------------------  
--Handle the text Field  
local function fieldHandler1( event )  
   
 if ( "began" == event.phase ) then  
 -- This is the "keyboard has appeared" event  
 -- In some cases you may want to adjust the interface when the keyboard appears.  
  
 elseif ( "ended" == event.phase ) then  
 -- This event is called when the user stops editing a field: for example, when they touch a different field  
  
 elseif ( "submitted" == event.phase ) then  
 -- This event occurs when the user presses the "return" key (if available) on the onscreen keyboard  
 variable[1] = tField1.text  
 displaytext1 ()  
 -- Hide keyboard  
 native.setKeyboardFocus( nil )  
  
 end  
   
end  
  
local function fieldHandler2( event )  
   
 if ( "began" == event.phase ) then  
 -- This is the "keyboard has appeared" event  
 -- In some cases you may want to adjust the interface when the keyboard appears.  
  
 elseif ( "ended" == event.phase ) then  
 -- This event is called when the user stops editing a field: for example, when they touch a different field  
  
 elseif ( "submitted" == event.phase ) then  
 -- This event occurs when the user presses the "return" key (if available) on the onscreen keyboard  
 variable[2] = tField2.text  
 displaytext2 ()  
 -- Hide keyboard  
 native.setKeyboardFocus( nil )  
  
 end  
   
end  
  
local function fieldHandler3( event )  
   
 if ( "began" == event.phase ) then  
 -- This is the "keyboard has appeared" event  
 -- In some cases you may want to adjust the interface when the keyboard appears.  
  
 elseif ( "ended" == event.phase ) then  
 -- This event is called when the user stops editing a field: for example, when they touch a different field  
  
 elseif ( "submitted" == event.phase ) then  
 -- This event occurs when the user presses the "return" key (if available) on the onscreen keyboard  
 variable[3] = tField3.text  
 displaytext3 ()  
 -- Hide keyboard  
 native.setKeyboardFocus( nil )  
  
 end  
   
end  
  
local function fieldHandler4( event )  
   
 if ( "began" == event.phase ) then  
 -- This is the "keyboard has appeared" event  
 -- In some cases you may want to adjust the interface when the keyboard appears.  
  
 elseif ( "ended" == event.phase ) then  
 -- This event is called when the user stops editing a field: for example, when they touch a different field  
  
 elseif ( "submitted" == event.phase ) then  
 -- This event occurs when the user presses the "return" key (if available) on the onscreen keyboard  
 variable[4] = tField4.text  
 displaytext4 ()  
 -- Hide keyboard  
 native.setKeyboardFocus( nil )  
  
 end  
   
end  
   
local function fieldHandler5( event )  
   
 if ( "began" == event.phase ) then  
 -- This is the "keyboard has appeared" event  
 -- In some cases you may want to adjust the interface when the keyboard appears.  
  
 elseif ( "ended" == event.phase ) then  
 -- This event is called when the user stops editing a field: for example, when they touch a different field  
  
 elseif ( "submitted" == event.phase ) then  
 -- This event occurs when the user presses the "return" key (if available) on the onscreen keyboard  
 variable[5] = tField5.text  
 displaytext5 ()  
 -- Hide keyboard  
 native.setKeyboardFocus( nil )  
  
 end  
   
end  
  
---------------------------------------------------------------  
--Display the current values next to the text Field  
  
function displaytext1()  
 local astring = tostring(tField1.text);  
 if not display\_text\_text1 then  
 display\_text\_text1 = display.newText(astring, tField1.x + 130, tField1.y,"MarkerFelt-Thin",15);  
 display\_text\_text1:setTextColor(255,255,255);  
 display\_text\_text1.align = "right"  
 tweaksceneGroup:insert( display\_text\_text1 )  
 end  
 display\_text\_text1.text = astring;  
end  
  
function displaytext2()  
 local astring = tostring(tField2.text);  
 if not display\_text\_text2 then  
 display\_text\_text2 = display.newText(astring, tField2.x + 130, tField2.y,"MarkerFelt-Thin",15);  
 display\_text\_text2:setTextColor(255,255,255);  
 display\_text\_text2.align = "right"  
 tweaksceneGroup:insert( display\_text\_text2 )  
 end  
 display\_text\_text2.text = astring;  
end  
  
function displaytext3()  
 local astring = tostring(tField3.text);  
 if not display\_text\_text3 then  
 display\_text\_text3 = display.newText(astring, tField3.x + 130, tField3.y,"MarkerFelt-Thin",15);  
 display\_text\_text3:setTextColor(255,255,255);  
 display\_text\_text3.align = "right"  
 tweaksceneGroup:insert( display\_text\_text3 )  
 end  
 display\_text\_text3.text = astring;  
end  
  
function displaytext4()  
 local astring = tostring(tField4.text);  
 if not display\_text\_text4 then  
 display\_text\_text4 = display.newText(astring, tField4.x + 130, tField4.y,"MarkerFelt-Thin",15);  
 display\_text\_text4:setTextColor(255,255,255);  
 display\_text\_text4.align = "right"  
 tweaksceneGroup:insert( display\_text\_text4 )  
 end  
 display\_text\_text4.text = astring;  
end  
  
function displaytext5()  
 local astring = tostring(tField5.text);  
 if not display\_text\_text5 then  
 display\_text\_text5 = display.newText(astring, tField5.x + 130, tField5.y,"MarkerFelt-Thin",15);  
 display\_text\_text5:setTextColor(255,255,255);  
 display\_text\_text5.align = "right"  
 tweaksceneGroup:insert( display\_text\_text5 )  
 end  
 display\_text\_text5.text = astring;  
end  
  
---------------------------------------------------------------  
--Loads all Assets  
function loadme()  
  
 tField1 = native.newTextField( 0, 0, 100, 30, fieldHandler1 )  
 tField1.x = 20  
 tField1.y = 70  
 tField1.font = native.newFont( native.systemFontBold, 18 )  
 tField1.text = variable[1]  
  
  
 tField2 = native.newTextField( 0, 0, 100, 30, fieldHandler2 )  
 tField2.x = 20  
 tField2.y = 125  
 tField2.font = native.newFont( native.systemFontBold, 18 )  
 tField2.text = variable[2]  
  
 tField3 = native.newTextField( 0, 0, 100, 30, fieldHandler3 )  
 tField3.x = 20  
 tField3.y = 180  
 tField3.font = native.newFont( native.systemFontBold, 18 )  
 tField3.text = variable[3]  
  
 tField4 = native.newTextField( 0, 0, 100, 30, fieldHandler4 )  
 tField4.x = 20  
 tField4.y = 235  
 tField4.font = native.newFont( native.systemFontBold, 18 )  
 tField4.text = variable[4]  
  
 tField5 = native.newTextField( 0, 0, 100, 30, fieldHandler5 )  
 tField5.x = 20  
 tField5.y = 285  
 tField5.font = native.newFont( native.systemFontBold, 18 )  
 tField5.text = variable[5]  
  
  
 description1 = display.newText(description[1], tField1.x , tField1.y - 20,"MarkerFelt-Thin",15);  
 description1:setTextColor(255,255,255,255)  
 description1.align = "center"  
 tweaksceneGroup:insert(description1)  
  
 description2 = display.newText(description[2], tField2.x , tField2.y - 20,"MarkerFelt-Thin",15);  
 description2:setTextColor(255,255,255,255)  
 description2.align = "center"  
 tweaksceneGroup:insert(description2)  
  
 description3 = display.newText(description[3], tField3.x , tField3.y - 20,"MarkerFelt-Thin",15);  
 description3:setTextColor(255,255,255,255)  
 description3.align = "center"  
 tweaksceneGroup:insert(description3)  
  
 description4 = display.newText(description[4], tField4.x , tField4.y - 20,"MarkerFelt-Thin",15);  
 description4:setTextColor(255,255,255,255)  
 description4.align = "center"  
 tweaksceneGroup:insert(description4)  
  
 description5 = display.newText(description[5], tField5.x , tField5.y - 20,"MarkerFelt-Thin",15);  
 description5:setTextColor(255,255,255,255)  
 description5.align = "center"  
 tweaksceneGroup:insert(description5)  
  
 displaytext1()  
 displaytext2()  
 displaytext3()  
 displaytext4()  
 displaytext5()  
  
  
 --Return to Menu Button  
 menuButton = display.newRect ( 15, 15, 55, 25, 5)  
 menuButton:setFillColor (255,0,0,255)  
 tweaksceneGroup:insert(menuButton)  
  
 menuButtonText = display.newText("MENU", 16 , 13,"MarkerFelt-Thin",20);  
 menuButtonText:setTextColor (255,255,255,255)  
 tweaksceneGroup:insert(menuButtonText)  
  
 function menuButton.listener (event)  
  
 if (event.phase == "ended") then   
 menuscene.menuGroup.isVisible = true  
 unloadme()  
 end  
 return true -- This stop the event to the upper Object  
 end  
 menuButton:addEventListener("touch", menuButton.listener )  
end  
  
--Unload all Assets and Collect Garbage  
function unloadme ()  
 --Remove Table Fields, they cannot be included in display Group  
 tField1:removeSelf()  
 tField1 = nil  
 tField2:removeSelf()  
 tField2 = nil  
 tField3:removeSelf()  
 tField3 = nil  
 tField4:removeSelf()  
 tField4 = nil  
 tField5:removeSelf()  
 tField5 = nil  
  
 display\_text\_text1:removeSelf()  
 display\_text\_text1 = nil  
 display\_text\_text2:removeSelf()  
 display\_text\_text2 = nil  
 display\_text\_text3:removeSelf()  
 display\_text\_text3 = nil  
 display\_text\_text4:removeSelf()  
 display\_text\_text4 = nil  
 display\_text\_text5:removeSelf()  
 display\_text\_text5 = nil  
  
 --Remove all Display Objects  
 for i=tweaksceneGroup.numChildren,1,-1 do  
 local child = tweaksceneGroup[i]  
 child.parent:remove( child )  
 child = nil  
 end  
  
 collectgarbage( "collect")  
end  
  
  

Thanks [import]uid: 8192 topic_id: 3494 reply_id: 10496[/import]