Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Is there a way to do a guard statement or an early return in Lil?

I feel like I missed something, but I haven’t seen anything about it in the Lil or Decker documentation.

I have a function like:

on view do
 if frames_since_last_step < 10
   # stop the function execution here since its not time
  return
 end

 # do the next animation step
end

Oh hm, maybe I can use sleep[10] instead of what I was trying to do here?

Still curious about writing guards for other validation though.

(+2)

I don’t believe so, no. Even though it looks a bit like Lua or Python, Lil’s functional-language heritage means there’s no shortcuts, no return or break or continue. This can make some things more difficult, but once you get used to thinking in that style you’re writing much more vectorisation-friendly code, and Lil’s vector operations are much more efficient than regular loops.

(+1)

Cool, thanks for sharing! Excited to vectorize my brain properly as I go.

(+1)

Lil doesn't have a keyword for performing an early return from a function, or otherwise an equivalent of break/continue for each/while loops or a throw/catch exception mechanism. All control flow is strictly structured and local.

For some situations where you might want to guard a clause, you can take advantage of the fact that if ... elseif ... else ... end structures, like all control structures in Lil, are expressions which return their result:

C:

int foo(){
 if(cond1){return a;}
 if(cond2){foo();return b;}
 bar();quux();return c;
}

Lil:

on foo do
 if cond1 a
 elseif cond2 foo[] b
 else bar[] quux[] c
 end
end

I encourage breaking functions down into fairly short definitions. You can nest function declarations within one another if you need "helper" functions that are only used in one place, and those nested functions will close over the lexical scope of their "parent" function.

(+1)

Thank you for the advice! I think the tip to define many more functions is where I should start. Excited to see how to adapt to this pattern.