This is my main.lua
-- Usual SceneManager stuff.
sceneManager = SceneManager.new({
-- ["logoScreen"] = logoScreen,
-- ["menuScreen"] = menuScreen,
-- ["gameScreen"] = gameScreen,
-- ["levelEndScreen"] = levelEndScreen,
-- ["nextLevelScreen"] = nextLevelScreen,
["optionScreen"] = optionScreen
})
--add manager to stage
stage:addChild(sceneManager)
--start start scene
sceneManager:changeScene("optionScreen", 1, SceneManager.flipWithFade, easing.outBack)
local loader = UrlLoader.new('<a href="http://localhost/write.php'" rel="nofollow">http://localhost/write.php'</a>)
local function onComplete(event)
local myString=event.data -- We got the string here.
......
function how_many(searchAt,letter)
local i2 = 1
local a = 0
while (i2 < string.len(searchAt)+1)
do
if(string.sub(searchAt,i2,i2) == letter)
then
a = a + 1
i2 = i2 + 1
else
i2 = i2 + 1
end
end
return a
end
...........
function getH()
h = how_many(echoString,'\n')
return h
end
end
local function onError()
print("error")
end
local function onProgress(event)
print("progress: " .. event.bytesLoaded .. " of " .. event.bytesTotal)
end
loader:addEventListener(Event.COMPLETE, onComplete)
loader:addEventListener(Event.ERROR, onError)
loader:addEventListener(Event.PROGRESS, onProgress) |
and this is options.lua
function optionScreen:init()
......
h = getH() -----!!!PROBLEM!!!
while(h<8)
do
print ("00")
h = h + 1
end |
I want to use the value h which is found in main.lua.But it doesn't work:S How can i do it?
Thanks in advance.
Comments
You can either right click on the file "options.lua" in the IDE and set a code dependency so that the file containing the correct definition of "h" is parsed BEFORE "options.lua" (check the call order tab to see which order your code is parsed in) or (IMHO a better solution) would be to move the definition of "h" and any other global variables, into a file called "init.lua" which is guaranteed by Gideros to be the first file processed (main.lua is usually the last).
Also FWIW - "h" is a very poor name for a global - it's way to easy to get it confused later, or mistaken for a local etc etc - I would always recommend naming globals in UPPER_CASE and making them descriptive - that way you can see instantly when looking at your code which is which (just my $0.02)
Likes: loves_oi
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
After it just adreess them whereever you want. It doesn't matter in which file is or function.
Just be sure that do not redefine any global variable again as local variable in any of your lua files or function again.
Possible init.lua file:
You can also name your sprites with starting Spr for example it will enhance the readibility of your code. like a global sprite background will be:
GlbSprBackground
but a local sprite of background will be just:
SprBackground
Or another example your buttons can start with Btn, your text strings can start with Str etc etc:D
Likes: loves_oi
This is main.lua
This is optionscreen.lua
main.lua
@loves_oi - the problem is that the function getH doesn't exist or get called until the onComplete function is called (as these are defined inside it). As the OptionsScreen:init method is called first you are picking up the fact that at this point "h" doesn't actually exist so lua creates it (as a global) and gives it a default value of nil.
I would suggest liberally adding some print() statements to your code so you can understand the flow of code - as soon as you see that you'll begin to understand why it's not working. Rather than me just tell you a "fix" now, it's better that you try and understand the "why" so you won't make this mistake in the future - think of it a bit like the difference between "giving a man a fish" and "teaching a man how to fish".
With all the callbacks etc - the flow of code in Lua CAN be a little difficult to trace - just remember the KISS principal as much as possible.
Likes: loves_oi
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
look in the project window. The order in which the lua files appear is how they are executed. Gideros runs each one of the files, if you want to control running them, use the require function to trigger the loading of that file and in the project set them as excluded from execution.
Likes: loves_oi
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps