Clojurians
#clojure-gamedev
<
2018-05-22
>

This page is not created by, affiliated with, or supported by Slack Technologies, Inc.

jjttjj14:05:21

Is clojurescript game development actually a viable thing? For a game like a sidescroller that requires constant animation? I've been looking at play-cljs and chocolatier both which seem to be significant efforts. but their own demos for these types of games are unplayably slow and buggy out of the box. I'm trying to decide if it's worth it to try to track down what's causing this in these libraries but I'm starting to suspect it might be the performance overhead of clojurescript itself and that it's not really easily fixable?

jjttjj14:05:29

I'm not sure which rabbit hole I want to go down: attempt fix existing cljs frameworks, go without a framework, or abandon cljs altogether for something else

ivanreese17:05:12

The majority of game code won't be all that performance intensive, and for that there should be no problem using CLJS.

ivanreese17:05:33

Depending on the style of game, if you're doing a lot of work per-frame, you might end up wanting to write parts of it in JS. But you'd probably want to build it in CLJS first to figure out what you want your game to be, and then when you notice slowness, measure what is causing the slowness and apply targeted fixes for that stuff.

ivanreese17:05:25

CLJS also offers some nice half-step measures to eke out additional perf where you really need it — like using mutable JS arrays and objects from CLJS. That way you have the benefit of staying in Clojure-land, but you don't end up beating on the garbage collector.

ivanreese17:05:44

When doing games and such in the browser, you usually end up spending more of your time per frame rendering rather than running scripts, so whenever that's the case your choice of language really doesn't matter — rather, what matters is how your structure your scene graph, your render pipeline, what sort of tricks you can do with buffering, which graphics API you're using, etc.

jjttjj20:05:49

awesome thanks for the tips, that's good to hear that cljs is definitely viable.

jjttjj20:05:29

Currently trying to get to the bottom of the bugs in cljs-play described here: https://github.com/oakes/play-cljs-examples/issues/4

ivanreese21:05:03

I think the author of that comment is missing an important detail. In the browser, you are always going to run at 60fps — no faster, and no slower (unless your code/rendering takes too long to run each frame). So even if the player has a 144Hz monitor, the game will still run at 60Hz.

ivanreese21:05:28

When you're doing more advanced work, you'll want to use the amount of time elapsed between frames as part of your physics sim. But if this is one of your first game projects, you might be better off ignoring that detail and just coding the game as if every frame represented the same span of time.

ivanreese21:05:38

That'll make it easier to get started with physics and animation.

ivanreese21:05:16

The article they linked in that comment does do a good job of describing how to set up a good timestep, but there are choices you'll need to make based on how you design your game, and how you expect it to run (the article does a good job of explaining some of these under "Panic! Spiral of death")

ivanreese21:05:48

But if you've never run into these problems before, using these solutions might be a little too.. defensive.

ivanreese21:05:29

I say, keep this article in your back pocket. When you run into an issue, follow the recommendations in the article one at a time until your problem is resolved.

ivanreese21:05:48

Chances are good you won't need to do any of it.

ivanreese21:05:11

(And by the time you do, you'll know enough to pick and choose which parts of their solution you want to use. For example, you probably want to apply some smoothing to deltaTime values, since the framerate will be kinda spiky in non-Chrome browsers — a technique they don't cover. But again, not something you need to worry about until you actually have a big, complex game that is behaving weirdly on different machines in different browsers.)

iwannaseethelight22:05:40

So I’ve been considering building a large simulation game in Clojure using LWJGL

iwannaseethelight22:05:54

I’m trying to decide if Clojure will be fast enough in practice

the2bears22:05:21

play-clj wraps libgdx which is LWJGL++

the2bears22:05:30

I've used it quite successfully for 2d games.

the2bears22:05:16

it will be fairly familiar to you if you've used LWJGL - I did Java games years ago with it, and the transition was fairly easy

iwannaseethelight22:05:52

I’ve looked at it - I’d rather do my own thing - my question(s) are mostly about whether Clojure can be performant enough

iwannaseethelight22:05:27

I know I can always stick things in deftype / defrecord, etc but I’m not sure about Clojure’s performance at scale

ivanreese22:05:46

(Or use volatile / transients)

iwannaseethelight22:05:48

I use Clojure at work actually for large modeling simulations and it performs pretty well

iwannaseethelight22:05:53

@ivanreese yeah for sure

iwannaseethelight22:05:14

Having a repl in-game would just be so sick

iwannaseethelight22:05:18

one thing that is unfortunate is that using Clojure limits me to *nix / osx / windows

iwannaseethelight22:05:37

but it’s a large simluation game so I guess if it ever does well I just pay someone to port it :wink:

jjttjj22:05:17

Awesome thanks so much for this info. The problem is, it seems like there are actually major performance/loop issues out of the box with the super Koalia game demo that make it unplayable at least on my computer (with bugs like falling through the ground). I assumed that they might have something to do with the issues described in the linked loop article. I started checking into this and haven't confirmed or ruled it out yet (the bugs might be unrelated) @ivanreese

jjttjj22:05:21

My initial attempts to measure the fps with the demo show it at only about 10 fps on my pretty specced out computer. I could have the measuring wrong. But I'm not sure yet if that in itself is causing the buggyness or is just a symptom of it

iwannaseethelight22:05:50

Considering minecraft was built in java, I’m betting it’s at least feasible to build a sim game in clojure, might just be hard to get right

iwannaseethelight23:05:52

having a repl would be so awesome though, you basically boot the game and never restart it while developing

the2bears23:05:24

@iwannaseethelight you're right, having the REPL while the game runs is really good. Debugging especially, tuning of configurable parameters... so much fun.

iwannaseethelight23:05:44

I was an engine programmer for a decade, hardcore low level c++

the2bears23:05:51

Yeah, Minecraft was Java/LWJGL I believe.

iwannaseethelight23:05:07

but I’m a hardcore emacs user and was using elisp for years so when i found clojure i was like “omg”

the2bears23:05:26

Yeah... saved my sanity from a career of Java.

iwannaseethelight23:05:27

i think the hard part is the immutability though as far as performance goes

iwannaseethelight23:05:46

that and rendering

the2bears23:05:56

Why rendering?

the2bears23:05:17

You mean time consuming?

iwannaseethelight23:05:47

you don’t have the luxury of an engine so if you want to do anything complicated at all you’re hosed - aka, any 3d animation, skeletal rendering, lighting, complicated shaders, etc

iwannaseethelight23:05:07

my game is top down 2d ala rimworld so i’m probably ok, but it’s such a waste of time to write a renderer

the2bears23:05:15

I'm old school...

the2bears23:05:21

2d shmups :slightly_smiling_face:

iwannaseethelight23:05:24

had the crazy thought of using unity and communicating with a clojure process over udp :stuck_out_tongue:

the2bears23:05:04

That would likely slow things down far too much

the2bears23:05:16

well, UDP is better than HTTP...

iwannaseethelight23:05:46

yeah it likely would

iwannaseethelight23:05:51

there are big disadvantages to using clojure

iwannaseethelight23:05:18

namely - gc issues, having no renderer, ui framework, etc., and being limited to desktop machines

iwannaseethelight23:05:55

pros are - it’s more fun to build imo, and you have the expressivity of lisp

iwannaseethelight23:05:13

rimworld has done pretty well on steam (no mobile) haha

iwannaseethelight23:05:26

and when i say pretty well, he’s swimming in dollar bills haha

iwannaseethelight23:05:36

i actually decompiled the source for that game, it’s all c-style

iwannaseethelight23:05:42

no ui / renderer / limited to desktop / performance issues are the biggies

iwannaseethelight23:05:48

especially those first two

the2bears23:05:45

I'm happy enough just having Clojure as a medium to express games in :slightly_smiling_face:

iwannaseethelight23:05:12

one other thing that’s nice about clojure i guess is it’s concurrency / parallelism mechanisms

the2bears23:05:45

yes, but in 2d I wouldn't bother with anything other than a single thread of execution.

the2bears23:05:13

for other styles I can see the benefits

the2bears23:05:37

but if you can avoid it with a single game loop I tend to avoid it :slightly_smiling_face:

iwannaseethelight23:05:26

it’s likely bat shit crazy to even consider building a game in clojure though haha - should use unity and call it a day

the2bears23:05:19

Well, honestly it's like asking how long a piece of string is. Some games are fine in Clojure, and mine are fine performant-wise for what they are. If you're trying to do something more involved, more computationally intense then yes, the extra GC work will really slow things down.

the2bears23:05:44

My advice is if you want to give it a try, then go for it.

iwannaseethelight23:05:31

To @ivanreese’s point, you can use things like transient to make things a bit better

ivanreese23:05:22

You can also do 90% of the game in Clojure, and the perf-critical 10% of the game in something closer to the metal.

ivanreese23:05:28

Mirroring the way most AAA games are split between C++ and Lua, or how many indie games using Unity or Unreal use some sort of easy-to-use scripting language on top of a high-perf core engine.

ivanreese23:05:56

As for... no renderer.. there have got to be renderers for Java, no?

ivanreese23:05:24

And if you have a renderer, you have a UI layer — just build it out of game objects.