Share Your Code

TweakScene

Posted by amigoni, Posted on November 8, 2010

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
--tweakscene Version 1 (beta) November 8, 2010
--Written by Amigoni
 
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
                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>

Compatibility: 
Corona Game Edition