Triggering the pause function Codea has _ FIXED wrote some Lua code

Hi all,

Has anyone had any luck coding in the pause function that Codea has when you test your games? Am looking to trigger a game pause and instigate a button to unpause or go back to the game’s menu

Thanks

Are you talking about the physics.pause . There isn’t a true pause for Codea because it’s always running. If you want a pause, you have to code it yourself. It isn’t hard if you plan for it early in the coding cycle.

Thanks @dave1707 - no it’s the Codea pause level I’m hoping to expose. You know when you run your program and have access to the buttons bottom left, one is a pause in Xcode that overrides the play state of Lua.

@Majormorgan Oh, that one. I don’t do anything with XCode, so I can’t give you any help with that.

@Majormorgan I know how to do it, but it has to be written in Obj-C, I have the code on my computer, do you want me to put it on this thread

Why does it specifically have to be the Codea pause?

Hey @CamelCoder that would be cool. I might be able to get that to work!

Hey @yojimbo2000 - it’s only because it’s the only code I know that totally pauses everything in Codea. To go in and write code that will stop everything in Lua might not be feasible with how far I’ve come with it. A lot of code is independent - for example each alien shoots, moves and detects according to its own time. I’ll look into it, but I know the Codea code overrides everything.

Thanks!

@Majormorgan The Codea pause doesn’t pause everything. Try the below program of a physics ball falling. Press the Codea pause button just after starting the program. Notice what the y value is. Wait a few seconds and press the pause button again to restart the code. The ball will be completely off the screen. Notice what the y value is again.

function setup()
    b=physics.body(CIRCLE,30)
    b.x=WIDTH/2
    b.y=HEIGHT
end

function draw()
    background(40, 40, 50)
    fill(255)
    ellipse(b.x,b.y,60)
    text("y value = "..b.y//1,120,HEIGHT-100)
end

@Majormorgan If we think about what Codea executes each frame other than the draw loop: physics, tweens, sounds, responses to touch events and orientation changes. Physics has a pause command, tweens can be paused with the code here: https://codea.io/talk/discussion/4474/pause-resume-tweens

@Majormorgan Here’s another problem with trying to pause Codea. It doesn’t stop the screen touch. Run this program and press Codea pause. You’ll see the draw count stop. Tap the screen 10 times and then press Codea pause to resume the code. You’ll see the touch count shows how many times you tapped the screen. Apparently the iOS touch routine buffers the touches and then passes them to Codea once it restarts.

function setup()
    drawCnt=0
    tchCnt=0
end

function draw()
    background(40, 40, 50)
    fill(255)
    drawCnt=drawCnt+1
    text("draw count  "..drawCnt,200,500)
    text("touch count  "..tchCnt,200,400)
end

function touched(t)
    if t.state==BEGAN then
        tchCnt=tchCnt+1
    end
end

@Majormorgan In my most recent app, I do show ads if the user wants to watch them for free coins. I had to pause Codea when the ads show just so nothing continues while the ad is being played. While I was testing it, I realized that the music and sounds in the background still play! So @yojimbo2000 and @dave1707 are correct. Btw, I’ll get the code up for how to do it once my iTunes stops acting up, it won’t let me go to xCode.

To go in and write code that will stop everything in Lua might not be feasible with how far I’ve come with it.

@Majormorgan that right there is your code saying “for the love of God stop adding new features and refactor me!”

What you should aim for is no function longer than 10 lines, clean separation of concerns, updating logic and rendering logic kept separate, and so on. Then it’s as simple as:

function draw()
    if not paused then
        currentState:update()
    end
    currentState:draw()
end

The real question is that once codea is paused, how do you plan to unpause it?

@Majormorgan Basically, you just have to go to a .m file that has a StandaloneViewController imported. Then, in the function where you want to pause Codea, say StandaloneViewController *cc=self.viewController, then the view controller will be stored into the cc variable, then to pause it, do:

cc.paused=YES;

@John To unpause it, it’s as simple as…

cc.paused=NO;

@CamelCoder I was under the assumption that he wanted to pause and unpause within Codea rather than from the Obj-C end.

@John I was sort of confused why you asked the question, I was like… “He made Codea, does he really not know how to do it?” Then, before I went to sleep last night, I rethought the question, and I thought to myself, “Ah! I’m such an idiot” Sorry if that response before came out condescending, just a misunderstanding. :expressionless:

Yeah you’re all right. The whole engine is quite big and unwieldy, but for so many reasons. Mainly me learning how to do more and more things. I think I have a way to do it within Codea and that is the preferred way as I don’t have a clue about Xcode and objective C - but it’s gonna take a lot of checking I have everything wrapped up. It will be worth it.

When you factor in all the things I’ve made the game do, each objects specific hit states, buttons, reusing classes to create different aliens that do very different things I can see where it all became unwieldy. To be honest when I made Mightymower I should have remembered I needed a pause function and to bake it in.

Things like the music carrying on are fine. Even the sound as they are triggered by time events anyway, be it the global leveltimer or self.timer within classes.

I’ll have a think and see what I can do.

Thanks all for the advice!

“May the Starsceptre be with you…!”

@John 's question is right. And whilst @CamelCoder has a cool answer my problems begin to explode wider than my not bad grasp on Lua over Objective C. I’m a creative who works in pictures and video. I’m lucky to have scored big with Codea as it makes my app making life so much easier than having to try to learn another way. Programming never ever came easy to me and Lua just seems to have clicked although my functions are very very basic. I mean my game is basically a ton of ‘if’ statements.

That’s not an excuse for me to not go back and redo stuff, rather my total lack of time is. But you guys have all taught me so much and I know my next game will be a ton easier to code because of it.

I will sleep on my in Codea solution and test the new pause function my brain thinks I can achieve over the next few days.

Thanks again

https://instagram.com/p/BWGVCSgHiYY/

@Majormorgan You can use debug to pause a program. It doesn’t pause music or physics objects and I haven’t looked at what else it doesn’t pause. The only problem is once I call the debug hook function loop, there isn’t anything manual I can do to get out of it. Right now I use os.time and pause for 10 seconds. You have to stay in the function loop so nothing else in Codea can execute.

function setup()
    x=10
    y=400
    xv=1
    yv=1
    cnt=0
    sec=10
    debug.sethook(loop,"l")
end

function loop()
    while pause do
        ct=os.time()
        if ct>st+sec then
            pause=false
        end  
    end
end

function draw()
    background(40, 40, 50)
    fill(255)
    text("tap screen to pause for "..sec.." seconds.",WIDTH/2,HEIGHT/2)
    x=x+xv
    if x>WIDTH or x<0 then
        xv=xv*-1
    end
    y=y+yv
    if y>HEIGHT or y<0 then
        yv=yv*-1
    end
    ellipse(x,y,20)
    cnt=cnt+1
    text(cnt,WIDTH/2,HEIGHT-50)
end

function touched(t)
    if t.state==BEGAN then
        st=os.time()
        pause=true
    end
end