Cannot see buttons in Lua - mobile

I have added a logo and few buttons, such as PLAY and CREDITS. I do not receive any errors and I'm having a hard time seeing the problem, what am I missing?
--Background
local bg = display.newImage("background.png")
--Buttons
local title
local playBtn
local creditsBtn
--Functions
local Main=('')
local startButtonListeners=('')
--Start of Functions
function Main()
title= display.newImage("logo.png")
playBtn= display.newImage("playbtn.png", 130, 248)
creditsBtn= display.newImage("creditsbtn.png", 125, 316)
titleView= display.newGroup(title, playBtn, creditsBtn)
startButtonListeners("add")
end

If that is your code in its entirety, you never called your main function, and in corona, you don't have to call a main function, main.lua is run at the beginning of your project. So try running your code like this
--Background
local bg = display.newImage("background.png")
--Buttons
local title
local playBtn
local creditsBtn
--Functions
local Main
local startButtonListeners, anotherButtonListener
--Start of Functions
Main = function()
title= display.newImage("logo.png")
playBtn= display.newImage("playbtn.png", 130, 248)
creditsBtn= display.newImage("creditsbtn.png", 125, 316)
titleView= display.newGroup()
titleView:insert(title)
titleView:insert(playBtn)
titleView:insert(creditsBtn)
playBtn:addEventListener("tap", startButtonListeners)
--creditsBtn:addEventListener("tap", anotherButtonListener)
end
startButtonListeners = function(event)
--Do something here
end
anotherButtonListener = function(event)
--Do something for the credits here
end
Main() --Remember to actually call Main to make it run
In Lua, there is no declared main function, it just runs everything in a sequence. Remember that you do not need to write a main function like you would in C, but more like Python, it will just run what you write.
EDIT: Why don't you post the errors you get so we can help you better? But scanning the code something definitely got past me. The newGroup line.
Refer above for the edited code.

Related

Gideros how to pass a function to Timer.delayCall?

First, it is possible to do like this:
local delay = 1000
local timer = Timer.delayedCall(delay,
function(data)
print(data)
end,
20)
But, it seems not possible to do like this:
function Game:DoSomething(data)
print(data)
end
local timer = Timer.delayedCall(delay,
self.DoSomething,
20)
In other words, I would like to define the function outside (so to be reused by others). However, that seems not possible. Or did I do it wrong?
If what you want is have Timer.delayedCall call a method with multiple arguments in a generic way, you can do it like this:
function Game:DoSomething(data)
print(data)
end
function invokeMethod(args)
local func=table.remove(args,1)
func(unpack(args))
end
local timer = Timer.delayedCall(
delay,
invokeMethod,
{self.DoSomething,self,20}) --self is the instance you are calling
PS: this is my first post on SO, sorry if it isn't formatted correctly...

How to get the percentage of execution of a lua script?

I run Lua scripts inside c application in a separate task, I added a feature to check on the status of the running script if it's still running or completed.
I want to add some info in case of running script, I want to add the percentage executed from the running script, does Lua support this info by any mean.
I tried to use the info reported in Lua_debug, but it doesn't look to be correct (Like currentlin, definedline), I need the percentage over the whole script not just for a specific function.
You could set a debug hook and listen to the hook events:
Ideone
local script = [[
print 'hello'
print 'world'
for i=0,3 do
local answer = 42
end
]]
local script_function = loadstring(script)
local function count_lines ()
local count = 0
for k,v in script:gmatch '\n' do count = count+1 end
return count
end
local function trace(total_lines)
local max_last_line = 0
return function(info,line_number)
if line_number > max_last_line then
print(tostring(line_number/total_lines*100)..'%')
max_last_line = line_number
end
end
end
local lines = count_lines()
local thread = coroutine.create( script_function )
debug.sethook(thread, trace(lines), "l")
coroutine.resume(thread)
output:
20%
hello
40%
world
60%
80%
100%
However, it is to mention that it's still quite hard to get meaningful numbers from such a progress report - if you organize your code and the only statement at the end is the call to some kind of main, the progress will be 100%, whereas the number wouldn't represent anything useful. You could also track the lines and measure coverage, but naturally, 100% coverage is not something that you normally have during an execution run. You could also try to build a system for progress reporting and call a function like
ReportProgress( tasks / all_tasks )
which would be bound to C and would generate the necessary progress update

Need something like wait or delay in corona sdk

Im making a game in corona. I'm searching for something to make a stop in my code for 5 seconds. I found timer.performwith delay, but it's not working, i need something that stops all the code for 5 seconds. Can someone help me with that please?
I want to make after this transition to wait for 5 seconds and the continue with the code.
transition.to(block[old], {time=tranTime, x=block[new].x, y=block[new].y})
transition.to(block[new], {time=tranTime, x=block[old].x, y=block[old].y})
local function yourFunction()
print("This will get called 5seconds after block[old] transition...")
end
local function transitionFinished()
print("Transition of block[old] is completed...")
timer.performWithDelay(5000,yourFunction,1) --[[ If you want, you can use this
line to call 'yourFunction'
after desired time(here 5seconds)
--]]
end
transition.to(block[old], {time=tranTime, x=block[new].x, y=block[new].y, onComplete=transitionFinished})
transition.to(block[new], {time=tranTime, x=block[old].x, y=block[old].y})
Or else, if you want to pause all the transitions, there are many custom classes and you can make use of something like one that DevfaR said.
Or if you want to perform a transition after a delay, you can also use:
transition.to(block[old], {delay=1000,time=tranTime, x=block[new].x, y=block[new].y})
-- this will get called after a delay of 1000ms --
Keep coding............ :)
There is no way to stop code for a certain amount of time but what you can do is use the onComplete event in the transition. For example:
local function1 = function()
print("This will show after the transition finishes.")
end
transition.to(block[old], {time=tranTime, x=block[new].x, y=block[new].y, onComplete=function1})
transition.to(block[new], {time=tranTime, x=block[old].x, y=block[old].y})

Lua Timer(s) on command

Sorry, but this one is what some may call an open-ended question.
I am attempting to make a command where if someone says !spectate, they are put in spectate mode, and stay in there for 30 seconds. So far I have been totally unsuccessful, and as such, have no idea really of what I'm doing, so not erroneous code available :(
Here's some things which may assist those who answer:
To put one in spectate mode, after someone says !spectate (without the 30 second limit) you do this:
if Message == "!spectate" then
InputConsole("spectate %d", pID)
end
The game this will be used with is Command and Conquer: Renegade
Sorry I can't be much more helpful than that, I am totally out of my depth here!
However you'd go about doing this would be specific to Renegade's Lua API. I've never used it myself, but the almighty Google reckons that Renegades uses LuaTT, for which the API docs say:
You can have only 255 scripts attached to objects. For timers, make your own api based of OnThink and os.time
The docs aren't particularly good, but a quick look at the example code found here suggests something along these lines would work:
local timers = {
{ time = 1343910384, cb = function() doSomething() end }
}
function OnThink() -- this is called every frame
for i = 1, #timers do
if os.time() > timers[i].time then
timers[i].cb()
table.remove(timers, i)
end
end
end
The code you posted would then look something like this:
if Message == "!spectate" then
InputConsole("spectate %d", pID) -- move player to spectators
table.insert(timers, {
time = os.time() + 30, -- 30 seconds from now,
cb = function() InputConsole("spectate %d", pID) end -- remove player from spectators
})
end

Sandboxing in Lua 5.2

I am learning from "Programing in Lua" by Roberto Ierusalimschy, and I found that in the book, the example of Sandboxing uses the function setfenv() to change the environment of a given function, but in lua 5.2 this function is no longer available.
I tried to load some values from a file (a configuration file) into a field in a table, but, in lua 5.2 I can't use setfenv ( so I can load the values in the given environment). After reading some articles about lua 5.2 I found that each function may have (or not) an upvalue called _ENV which serves as the environment, so, I tried the following code:
function sandbox(sb_func, sb_env)
if not sb_func then return nil, "sandbox function not valid" end
sb_orig_env = _ENV
_ENV = sb_env -- yes, replaces the global _ENV
pcall_res, message = pcall( sb_func )
local modified_env = _ENV -- gets the environment that was used in the pcall( sb_func )
_ENV = sb_orig_env
return true, modified_env
end
function readFile(filename)
code = loadfile(filename)
res, table = sandbox(code, {})
if res then
--[[ Use table (modified_env) ]]--
else
print("Code not valid")
end
Replacing _ENV in the 'sandbox' function works well (can't access the regular fields), but, when the 'code' is executed it seems that it ignores that I replaced _ENV, it still can access regular fields (print, loadfile, dofile, etc).
Reading a little more, I found that lua 5.2 provides a function for this purpose, this function is loadin(env, chunk), which runs the given chunk in the given environment, but, when I try to add this function to my code, the function doesn't exist ( Is not present in the global _G field).
Some help will be appreciated.
When you assign to _ENV from within sandbox, you're not overwriting the global environment--you're replacing the _ENV upvalue of the currently running code. Adding calls to print(_ENV) may help you better understand the identities of the tables involved.
For example:
function print_env()
print(_ENV)
end
function sandbox()
print(_ENV) -- prints: "table: 0x100100610"
-- need to keep access to a few globals:
_ENV = { print = print, print_env = print_env, debug = debug, load = load }
print(_ENV) -- prints: "table: 0x100105140"
print_env() -- prints: "table: 0x100105140"
local code1 = load('print(_ENV)')
code1() -- prints: "table: 0x100100610"
debug.setupvalue(code1, 1, _ENV) -- set our modified env
code1() -- prints: "table: 0x100105140"
local code2 = load('print(_ENV)', nil, nil, _ENV) -- pass 'env' arg
code2() -- prints: "table: 0x100105140"
end
The loadin function was present in some pre-release versions of Lua 5.2 but was removed before the final release. Instead, the Lua 5.2 load and loadfile functions take an env argument. You can also modify the _ENV of another function using debug.setupvalue.

Resources