RPGMaker MV ES6 Part 1 : Objects

Introduction

When developing plugins in RPGMaker MV (RMMV), there are two things I tend to use often. Those two things would be arrays, and objects. Objects are the focus of today’s post. In RMMV, objects are used for a couple purposes: namespacing, holding data, being used as a class. All of these are staple uses even in ES5.

Today, however, we will be going over some new things you can do with objects in RMMV to make your life easier; these include: object assignment,  shorthand object properties, method properties.

Object Assignment

Originally in RMMV, if you wanted to assign properties to an object, you had to add each property by hand when setting up the data structure, or use some sort of loop. Now, those are both viable options, but what happens when you want to put two objects together? Adding new properties to an existing object would require checking properties in both objects, then you’d have to make sure not to overwrite properties on the object you want to add properties to. Now, we have the ability to use object assignment.

Object assignment allows you to put one or more objects into a destination object; there is no need to use a loop to assign the new properties to the object.

Example

//Created two new objects for combining

var object1 = {playerHp: 300, playerMP: 400};
var object2 = {playerName: ‘Ron’, playerClass: ‘Hero’};
var combinationObject = {};

//Params destination, source Object1, source object2

Object.assign(combinationObject, object1, object2);

After these lines the combinationObject will have all the properties of both object1, and object2; the other option in ES5 would have been to recreate a new object, then loop through both object1, and object2, in order to place the necessary fields in our new object. That’s one feature available to us, but there is more.

Shorthand Object Properties

Shorthand object notation is a straightforward concept, it allows us to shorthand the creation of objects with already defined variables. In this case I’ll show you the difference between ES5, and ES6.

Example

//Shorthand Object Properties
var x = 5;var y = 10;
//ES5 JavaScriptvar objA = {x: x, y : y};//ES6 JavaScript
var objB = {x, y};

 

The difference is minimal, but we can declare objects with their variable names as keys. This can simplify our code, and make it easier to read when creating new objects. This leads me to the last feature, method properties.

Method Properties

Method properties are similar to shorthand object properties; the point being, to make it easier for us to define methods for an object. If we were to do this in ES5, we’d have to define a new function on an object in order to create a method for it; this can make it harder to read our code. Now, we can have more code clarity; here’s an example of the difference.

Example

//Method Properties
//ES5 JavaScriptvar namespace1 = {  Test: function() {    console.log('Testing our namespace1');
 }

};

//ES6 JavaScript

var namespace2 = {

 Test() {

   console.log(‘Testing our namespace2’);

 }

};

As you can see both do the same thing, but one requires a lot less typing. And, we can declare our properties in a clearer manner.

Conclusion

All of these can help your code in a small way, try to start using them. I know that making the switch to ES5 to ES6 can be hard, but you can use both to maximize your productivity as a developer.

If you want to see more tutorials like this, stay tuned to the blog, or send me a message on twitter @EISKino if you have any ideas for tutorials. I hope you have a wonderful day!

Leave a Reply

Your email address will not be published.