0 votes
by (880 points)
class meleeWeapon{
	constructor(name,cost,weight,effects,maxdamage, mindamage,description,attributes,sellprice){
	this.name=name;
	this.cost=cost;
	this.weight=weight;
	this.effects=effects;
	this.maxDamage= maxDamage;
	this.minDamage=minDamage;    
        this.description=description ;
	this.attributes=minDamage;
        this.sellPrice=sellPrice;
 
};
	
meleeWeapon= new weapon(`Beginner's Blade`, 50, light; none, 6,0,`The beginning blade of many a traveler.`,'slashing', 25);
meleeWeapon2= new weapon(`Rusted Blade`, 5, light; -2 `to attack` , 4,0,`A rusted blade dulled by disuses but better for protection than luck.`,'dull''slashing', 3);

Im setting up a class for melee, ranged and items. I was wondering if im doing this right?Also for things that have multiple attributes(rusted blade) did i do that right with 'dull''slashing' or does it need a form of punctuation?

Also the rusted blade has an effect on -2 for attack but has the same damage as the beginner blade -2 from the 6, or is their a different way of doing that?

1 Answer

0 votes
by (159k points)
selected by
 
Best answer

warning: The class definition functionality is a relative recent addition to the JavaScript language and as such isn't supported by all the different bands or versions of web-browsers currently in use. Thus if you are planing to use it or any other ES6 feature in your story you should consider transpiling that code down to ES5 before using it in your project.

That being said there are a number of issues with your example:

1. Class names generally start with an uppercase letter.
2. Your constructor is missing it's end curly brace. }
3. There is an extra space at the end of your description assignment.
4. You have an unnecessary semi-colon at the end of your class definition.
5. Your meleeWeapon and meleeWeapon2 variable assignments are missing there scope indicators (eg. var) and even with one those variables are not likely to be accessable within your Passage content.
6. Your meleeWeapon and meleeWeapon2 variable assignments are being assigned instances of a weapon class but your class definition is named MeleeWeapon.
7. You are incorrectly using back-ticks to delimit the String values being passed to the class constructor, and they should be delimited using quotes (either single or double)
8. The weight argument being passage to the class constructor is followed by a semi-colon instead of a coma.
9. There are a number missing quotes and comas in both of the class constructors.
10. There are nine arguments in the class's constructor, however there are more than nine being passed to the meleeWeapon2 variable assignment.

The following is a partially corrected version of your original example.

class MeleeWeapon {
	constructor(name, cost, weight, effects, maxdamage, mindamage, description, attributes, sellprice) {
		this.name = name;
		this.cost = cost;
		this.weight = weight;
		this.effects = effects;
		this.maxDamage = maxDamage;
		this.minDamage = minDamage;
		this.description = description;
		this.attributes = minDamage;
		this.sellPrice = sellPrice;
	}
}
	
var meleeWeapon = new MeleeWeapon("Beginner's Blade", 50, 'light', 'none', 6, 0,
		'The beginning blade of many a traveler.', 'slashing', 25);

var meleeWeapon2 = new weapon('Rusted Blade', 5, 'light', '-2 to attack', 4, 0,
		'A rusted blade dulled by disuses but better for protection than luck.',
		'dull slashing', 3);

 

by (880 points)
edited by
Ok, i got this from a Udemy course for text based rpg done in visual basic, so thats why i wrote the class like that. I thought it would work all the same.Does that mean i cant use the course for this?
Also am i supposed to keep all these variables and classes in a passage placed somewhere?
If making it as a class will be a problem, what should i change it to?

also what do you mean by" transpiling that code down to ES5 before using it in your project."

what is es5 and es6?Im using chrome and firefox.
by (159k points)

> what is es5 and es6?
The modern JavaScript language is an implementation of the ECMAScript specification, and that specification has multiple versions, two of which are known as ES5 and ES6.

The JavaScript runtimes found in web-browsers are built to support specific versions of ECMAScript (which ones they support are up to the developers to deside), although they may also include selected features from later version of the specification. Versions of modern web-browser released since 2010 (ish) generally support all the features of the ES5 specification.

> ...am i supposed to keep all these variables and classes in...

Variables and object definitions can be kept in a number of locations, depending on how you like to code and what you're planing to do with them.

Based on the code in your example it appears as if the weapon class definitions are Static, and by that I mean that the properties of a specific weapon don't change once it has been defined. If this is true then I would suggest defining them on the special 'global-like' setup object that the story format supplies for this purpose.

There are a number of ways you can define your custom object types:

A. Using a Generic Object from a TwineScript passage.

/* Using the <<silently>> macro to suppress the visual output of the following. */
<<silently>>
	/* Define the contain to store all the weapon definitions in. */
	<<set setup.weapons to {}>>
	/* Define the actual weapons. */
	<<set setup.weapons["Beginner's Blade"] to {
		name         : "Beginner's Blade",
		cost         : 50,
		weight       : 'light',
		effects      : 'none',
		maxdamage    : 6,
		mindamage    : 0,
		description  : 'The beginning blade of many a traveler.',
		attributes   : ['slashing'],
		sellprice    : 25
	}>>
	<<set setup.weapons["Rusted Blade"] to {
		name         : "Rusted Blade",
		cost         : 5,
		weight       : 'light',
		effects      : '-2 to attack',
		maxdamage    : 4,
		mindamage    : 0,
		description  : 'A rusted blade dulled by disuses but better for protection than luck.',
		attributes   : ['dull', 'slashing'],
		sellprice    : 3
	}>>

	/* Tracking the player's current weapon. */
	<<set $wielding to "Rusted Blade">>
<</silently>>\

/* Displaying the name and description of the currently wielded weapon. */
\The player is wielding a $wielding, which looks like <<= setup.weapons[$wielding].description>>


B. Using a Generic Object defined in JavaScript and referenced in a TwineScript passage.

The following code is added to your project's Story Javascript area, it defines the weapons using Generic Objects.

/* Using a Generic Object. */
setup.weapons = {};
setup.weapons["Beginner's Blade"] = {
	name         : "Beginner's Blade",
	cost         : 50,
	weight       : 'light',
	effects      : 'none',
	maxdamage    : 6,
	mindamage    : 0,
	description  : 'The beginning blade of many a traveler.',
	attributes   : ['slashing'],
	sellprice    : 25
};
setup.weapons["Rusted Blade"] = {
	name         : "Rusted Blade",
	cost         : 5,
	weight       : 'light',
	effects      : '-2 to attack',
	maxdamage    : 4,
	mindamage    : 0,
	description  : 'A rusted blade dulled by disuses but better for protection than luck.',
	attributes   : ['dull', 'slashing'],
	sellprice    : 3
};

The following code is added to a Standard Passage, it makes use of the pre-defined weapons.

/* Tracking the player's current weapon. */
<<set $wielding to "Rusted Blade">>\

/* Displaying the name and description of the currently wielded weapon. */
\The player is wielding a $wielding, which looks like <<= setup.weapons[$wielding].description>>


C. Using a Constructor Function defined and used in JavaScript, and referenced in a TwineScript passage.

The following code is added to your project's Story Javascript area, it defines the MeleeWeapon constructor function, and then uses that function to create some weapons.

setup.MeleeWeapon = function (
	name, cost, weight, effects, maxDamage, minDamage, description,
	attributes, sellPrice) {

	this.name = name;
	this.cost = cost;
	this.weight = weight;
	this.effects = effects;
	this.maxDamage = maxDamage;
	this.minDamage = minDamage;
	this.description = description;
	this.attributes = minDamage;
	this.sellPrice = sellPrice;
};

setup.weapons = {};
setup.weapons["Beginner's Blade"] = new setup.MeleeWeapon(
	"Beginner's Blade", 50, 'light', 'none', 6, 0,
	'The beginning blade of many a traveler.', ['slashing'], 25
);
setup.weapons["Rusted Blade"] = new setup.MeleeWeapon(
	"Rusted Blade", 5, 'light', '-2 to attack', 4, 0,
	'A rusted blade dulled by disuses but better for protection than luck.', ['dull', 'slashing'], 3
);

The following code is added to a Standard Passage, it makes use of the pre-defined weapons.

/* Tracking the player's current weapon. */
<<set $wielding to "Rusted Blade">>\

/* Displaying the name and description of the currently wielded weapon. */
\The player is wielding a $wielding, which looks like <<= setup.weapons[$wielding].description>>


D. Using a Constructor Function defined in JavaScript within a TwineScript passage.

The MeleeWeapon constructor function is that same as the previous JavaScript example, the only difference is that the defining of the different weapons if moved to a Standard Passage like so.

<<silently>>
<<set setup.weapons to {}>>
<<set setup.weapons["Beginner's Blade"] = new setup.MeleeWeapon("Beginner's Blade", 50, 'light', 'none', 6, 0,
	'The beginning blade of many a traveler.', ['slashing'], 25
)>>
<<set setup.weapons["Rusted Blade"] = new setup.MeleeWeapon("Rusted Blade", 5, 'light', '-2 to attack', 4, 0,
	'A rusted blade dulled by disuses but better for protection than luck.', ['dull', 'slashing'], 3
)>>

/* Tracking the player's current weapon. */
<<set $wielding to "Rusted Blade">>\
<</silently>>\

/* Displaying the name and description of the currently wielded weapon. */
\The player is wielding a $wielding, which looks like <<= setup.weapons[$wielding].description>>

 

by (880 points)
Would i also do this if i am making a Monster or race class?
...