r/shittyprogramming • • Apr 12 '16

Easy JavaScript game loop

Post image
277 Upvotes

37 comments sorted by

28

u/auxiliary-character Apr 12 '16

I forget, does javascript have tail call recursion?

27

u/MC_Labs15 Apr 12 '16

This particular code will throw an error

9

u/auxiliary-character Apr 12 '16

Yeah, even if javascript has tail call recursion, they're not using return statements at the end.

3

u/Niriel Apr 12 '16

Does JavaScript have a Bottom type? That's the only return type that makes sense for an infinite loop, since it's uninhabited.

4

u/Darksonn Apr 12 '16

No, but they don't have return types either, so it's fine.

4

u/Niriel Apr 12 '16

They don't have a return value, but they do have a return type. Bottom is a type with no value. https://en.m.wikipedia.org/wiki/Bottom_type

6

u/whatIsThisBullCrap Apr 12 '16

Javascript is dynamically and weakly typed. It doesn't matter what the type is

1

u/Darksonn Apr 13 '16

You misunderstand. I know perfectly well what a bottom type is, and I wasn't referring to functions that never return.

I was referring to functions in javascript. In javascript no function has a return type; if they return, they just return a thing. That's why there's no need for a bottom type in javascript.

1

u/ConcernedInScythe May 09 '16

There's an implicit return at the end of the function block, which any decent optimiser should understand.

1

u/serg06 Apr 12 '16

JS throws errors?

0

u/[deleted] Apr 12 '16

[deleted]

8

u/MC_Labs15 Apr 12 '16

o_O

5

u/Ilostmyredditlogin Apr 12 '16

Chasing bounties?

1

u/lichorat Apr 13 '16

What bounties?

1

u/Ilostmyredditlogin Apr 13 '16

1

u/lichorat Apr 13 '16

Oh I tried to do that. Turns out Chrome Stalls aren't security related.

1

u/Ilostmyredditlogin Apr 13 '16

Oh -- from what the first guy said, I thought it crashed the tab (which could indicate a bug to exploit).

1

u/lichorat Apr 13 '16

Ah I see. Nah, it just hangs and then you can close the tab, but not interact with it.

13

u/[deleted] Apr 12 '16

ES6 does, but I don't think any browsers support it yet.

4

u/pixx_ Apr 12 '16

thanks for killing my chrome tab

1

u/MC_Labs15 Apr 12 '16

This is /r/shittyprogramming . What'd you expect?

2

u/IronedSandwich May 07 '16 edited May 07 '16

+/u[EDIT: nope]/CompileBot javascript

function foo() {
    console.log("FOO");
    bar();
}

function bar() {
    console.log("BAR);
    foo();
}

foo()

Hope I was fast enough

1

u/MC_Labs15 May 07 '16

RIP CompileBot

1

u/IronedSandwich May 07 '16

ok fine I'll remove it

56

u/taeratrin Apr 12 '16

I ran this. My processor grew legs and is coming to hunt you down. Run.

33

u/fukitol- Apr 12 '16

all it's missing is var isRunning = true; in the global scope, if(!isRunning) { return; } window.on('stopRunning', function() { isRunning = false; }); in bar() and processEvents(); in foo()

It's practically a game!

2

u/[deleted] Apr 12 '16

Being serious: is it bad practice to have state variables in the global scope like that, in general?

3

u/fukitol- Apr 12 '16

Generally, with something like a game loop, there's going to be some sort of state being stored at the global scope. For instance, I might store the player there. But most things should rely solely on events, and anything in the global scope should be kept to a minimum.

4

u/the_omega99 Apr 12 '16

Eh? It doesn't have to be global. Assuming that only the loop needs to be aware that it's running, you can encapsulate it in something where it isn't accessible.

10

u/JipsAndJools Apr 12 '16

Holy stack overflow errors batman

6

u/nohimn Apr 12 '16

function bar() { console.log('BAR'); setTimeout(foo, 1); }

Fixed it for ya.

1

u/Zatherz May 12 '16

MC_Labs15

Hey, I used to watch your videos a long time ago.

1

u/MC_Labs15 May 12 '16

Hey! Yeah, I haven't actually made one in quite a while. I should start doing it again

5

u/Kilazur Apr 12 '16

What does this do in SHITTYprogramming? It's obviously to implement a double buffering later.

1

u/BobFloss Apr 12 '16

This belongs in /r/badcode. What the fuck are the mods even doing?