Giter Club home page Giter Club logo

ecmascript's Introduction

JavaScript language binding for godot game engine

This module implements the JavaScript script language support for godot game engine.

QuickJS is used as the ECMAScript engine.

QuickJS is a small and embeddable Javascript engine. It supports the ES2020 specification including modules, asynchronous generators and proxies.

It optionally supports mathematical extensions such as big integers (BigInt), big floating point numbers (BigFloat) and operator overloading.

It is also possible to replace the ECMAScript engine like V8 or SpiderMonkey but not in the near plan.

This project is still in development so it may contain bugs.


Compile

  • Clone the source code of godot
  • Clone this module and put it into godot/modules/ and make sure the folder name of this module is ECMAScript
  • Recompile godot engine

Usage

How to export script class to godot
  1. Define your ECMAScript class inhirent from godot class and export it as default entry
// The default export entry is treat as exported class to godot
export default class MySprite extends godot.Sprite {
	
	// this is _init() in GDScript
	constructor() {
		super();
	}
	
	_ready() {
		
	}
	
	_process(delta) {
		
	}
}
  1. Attach the script file to the node or resource object like you did with GDScript
How to export signals
export default class MySprite extends godot.Sprite {};
// register game_over signal to MySprite class
godot.register_signal(MySprite, 'game_over');
How to export properties
export default class MySprite extends godot.Sprite {
	_process(delta) {
		// Yes! We can use operators in JavaScript like GDScript
		this.position += this.direction * new godot.Vector2(delta, delta);
	}
};
// export 'direction' properties to MySprite godot inspector
godot.register_property(MySprite, 'direction', new godot.Vector2(1, 0));

About the API

Nothing is exported to the global namespace. All godot api are define in the godot namespace.

We didn't change any api name so you don't need to change your habbit at all.

GDScript ECMAScript
null null
int number
float number
String String
Array Array
Dictionary Object
Object godot.Object
Resource godot.Resource
Vector2 godot.Vecotor2
Color godot.Color
sin(v) godot.sin(v)
print(v) godot.print(v)
PI godot.PI
Color.black godot.Color.black
Control.CursorShape godot.Control.CursorShape
Label.Align.ALIGN_LEFT godot.Label.Align.ALIGN_LEFT
API specification:
  • Keys of Dictionary are converted to String in JavaScript
  • Signals are defined as constants to its classes
     godot.Control.resized === 'resized' // true
    
  • Additional functions
    • godot.register_signal(cls, signal_name) to register signals
    • godot.register_property(cls, name, default_value) to define and export properties
    • godot.register_class(cls, name) to register named class manually
    • godot.set_class_tags(tooled, icon) to set tool and icon of the class
    • godot.requestAnimationFrame(callbacl) to add a callback function to be called every frame
    • godot.cancelAnimationFrame(request_id) to cancel an frame request previously scheduled
    • godot.get_type(val) Returns the internal type of the given Variant object, using the godot.TYPE_*
  • Using signals in the ECMAScript way
    • Allow passing functions for godot.Object.connect, godot.Object.disconnect and godot.Object.is_connected
      	this.panel.connect(godot.Control.resized, (size) => {
      		console.log('The size of the panel changed to:', size);
      	});
    • Using await to wait signals
      await godot.yield(this.get_tree().create_timer(1), godot.SceneTreeTimer.timeout);
      console.log('After one second to show');

You can run the menu command from godot editor to dump the api declearations.

Project > Tools > ECMAScript > Generate TypeScript Declaration

Demo

You can try demos in the ECMAScriptDemos

ecmascript's People

Contributors

geequlim avatar

Watchers

 avatar  avatar

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.