How to Create 3D Games with PureScript Native and C++

Ideally, you’ll want to use [=] instead of [&] so you don’t alter the input parameters.Here’s some typical C++ lambda function patterns.[&capture,=list](p,a,r,a,m,s) -> ReturnType { body; }[&capture,=list](p,a,r,a,m,s) { body; }[&capture,=list] { body; }I believe the best practice is to unbox your parameters as you receive them and use [=] for each returned lambda function like this.[](const boxed& param_) -> boxed { const auto param = unbox<type>(param_); return [=](const boxed& param1_) -> boxed { const auto param1 = unbox<type>(param1_); return [=](const boxed& param2_) -> boxed { const auto param2 = unbox<type>(param2_); return ….}; };};However, for certain exports, I had to unbox the parameters in the last lambda function only..For example, like this..Unboxing in the first lambda and then copying by reference like this caused a segfault..And I couldn’t unbox in the first lambda and then copy by value like this as the compiler would complain that I was trying to change a constant NodePath with set_scale..There was the option to use the mutable keyword like this but unboxing all the parameters in the last lambda function worked fine.Another route would’ve been to dynamically allocate node paths —// …return [&]() -> boxed { // ….const auto nodePathPtr = std::make_shared<NodePath>( nodePath.find(query) ); return nodePathPtr;};// …returning and accepting pointers to them — but this would’ve added unnecessary overhead..Still, with pointers, you can keep them constant, unbox them as soon as you receive them, and copy them by value using [=].Generally, I stayed close to the Panda3D API..Ideally you want your exports to be very small and basic — putting any extra logic on the PureScript side for added protection..However, in some cases, I deviated when the procedures were routine and repetitive.Binding to some of the Panda3D API wasn’t the only FFI needed..There was also the need to create FFI exports for commonplace functionality like now, JS centric functions like setInterval, requestAnimationFrame, etc., and looking up system environment variables — something completely foreign to JavaScript (in the browser at least).????.Most of the PureScript ecosystem assumes a JavaScript back end which is understandable.. More details

Leave a Reply