Code Monkey home page Code Monkey logo

godot-addon-ginja's Introduction

Ginja is a string template engine for Godot 4.

Provides access to (some of) Inja's features.

This is a GdExtension addon for Godot 4.

Motivation

  • Generate tailored, efficient shaders.
  • Generate gdscript during development.
  • Generate dialogues, messages, and roleplays.

Demo

If you have a bleeding edge Godot 4 (more recent than alpha9), you can download and run the demo in Releases to see an example shader generation.

A gooey cursor using a generated shader

This source tree holds what's needed to build the shared libraries, not the shared libraries themselves since they are binary blobs.

Usage

var tpl = "Hello, {{ name }} !"
var ginja = Ginja.new()
var msg = ginja.render(tpl, {
    'name': "Nathanaël",
})

print(msg)

This is a very simple example, but you may use loops, conditions, comments, etc. See Inja's documentation for the available features.

You may also define your own functions for use in the templates:

func _ready():
	var ginja = Ginja.new()
	ginja.add_function("repeat", 2, self, "call_repeat")
	var msg = ginja.render("{{ repeat(msg, amount) }}", {
		'msg': "🎶!",
		'amount': 5,
	})
	assert(msg == "🎶!🎶!🎶!🎶!🎶!")


func call_repeat(msg: String, amount: int) -> String:
	return msg.repeat(times)

You HAVE TO specify the amount of arguments your custom function uses.

You can also define and register variadic functions, like sum here:

func _ready():
	var ginja = Ginja.new()
	ginja.add_function_variadic("sum", self, "call_sum")
	var answer = ginja.render("{{ sum(a, b, c) }}", {
		'a': 41,
		'b': -5,
		'c': 6,
	})
	assert(answer == "42")


func call_sum(arguments: Array) -> int:
	var total := 0
	for argument in arguments:
		total += argument
	return total

Current Limitations

  • LEAKING (see #1 — might not be us)
  • GdNative DOES NOT WORK IN EXPORTED HTML5 BUILDS
  • Single character unicode strings (like 🎶) behave oddly and yield empty strings sometimes
  • No personal plans for testing Windows or Mac, contribs welcome
  • Variadic user-defined functions need at least one parameter when called
  • Limited to Inja capabilities. Eg: Inja offers no filters
  • include and extend might not work in exported projects (to check)
  • Error when fetching callback Object from its RID: (but it works)
ERROR: Condition "_instance_bindings != nullptr" is true.
   at: set_instance_binding (core/object/object.cpp:1771)

Can't figure out if it's our fault or not, for this one. Idea: try Ref<Object> instead of RID&.

Support

Unsupported in HTML5 builds, beware ! See the state of GdNative on HTML5.

  • Linux x86_64
  • Linux x86_32 (to test)
  • Windows x86_64 (to test)
  • Windows x86_32 (to test)
  • Mac x86_64 (to test)

Shader and gdscript generation is still useful during development, even if we can't rely on it at runtime for now.

Install

The installation is as usual, through the Assets Library. (todo)

Meanwhile, dowload the latest Release (on the right), or clone this repository and build the shared objects.

The shared libs were not added to this repository, since they are build artifacts. (CI is coming)

Then you can copy the addons/ directory of this project into yours.

Finally, enable the plugin in Scene > Project Settings > Plugins.

Build

Build the shared libraries:

cd gdextension_generator
CORES=4 TARGET=debug build.sh
CORES=4 TARGET=release build.sh

Want to do this using CI, but we need a custom action, or to find a docker image with everything we need.

Setup Dev Notes

Use homebuilt Godot 4 from master, not alpha9.

Submodules

Ideally:

git submodule add -b 4.x https://github.com/godotengine/godot-cpp gdextension_generator/godot-cpp

Pragma 'til the 4.x branch exists:

git submodule add https://github.com/godotengine/godot-cpp gdextension_generator/godot-cpp

Remember to init the submodules!

Build

The first step is always to compile the godot-cpp library:

cd gdextension_generator/godot-cpp
scons target=debug

Then we can compile our shared library:

cd gdextension_generator
scons target=debug

It outputs in the addons/goutte.template.inja/bin/ directory.

The libs are voluminous because they are not stripped, you may do so:

strip addons/goutte.template.inja/bin/libgd*

Demo

The demo uses a symlink to get as child its sibling addons/ directory. Might not hold through git across platforms, beware.

Inja

Inja has been copied from 3.3, and the #include of json have been changed to use the local file: #include "json.hpp". Both are warts, and enlightened suggestions are welcome.

Vanilla GdScript Implementation

Just thoughts on a vanilla gdscript fallback for Ginja.

ANTLR

Needs gdscript templates and runtime first. Those are 404 for now.

CUSTOM

Would allow for easier customization of the enclosing markup tokens. Much more work. Faster code, though. (if done well)

godot-addon-ginja's People

Contributors

goutte avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

godot-addon-ginja's Issues

Leaked instances

We are leaking…

WARNING: ObjectDB instances leaked at exit (run with --verbose for details).
     at: cleanup (core/object/object.cpp:2024)
Leaked instance: JSON:-9222565545075801786
Leaked instance: JSON:-9222566094831615624
Leaked instance: JSON:-9222566644587429511
Leaked instance: JSON:-9222567194343243398
Leaked instance: JSON:-9222567744099057285
Leaked instance: JSON:-9222568293854871172
Leaked instance: JSON:-9222568843610685059
Leaked instance: JSON:-9222569393366498943
Hint: Leaked instances typically happen when nodes are removed from the scene tree (with `remove_child()`) but not freed (with `free()` or `queue_free()`).
Orphan StringName: JSON
Orphan StringName: set_lstrip_blocks
Orphan StringName: set_line_statement
Orphan StringName: Ginja
Orphan StringName: set_statement
Orphan StringName: set_expression
Orphan StringName: add_function
Orphan StringName: set_comment
Orphan StringName: RefCounted
Orphan StringName: add_function_variadic
Orphan StringName: add_callback
Orphan StringName: render
Orphan StringName: set_trim_blocks
StringName: 13 unclaimed string names at exit.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.