0 votes
by (700 points)

This is more of a technical question than an actual issue.

Say i have an array filled to the brim with objects with a whole list of values, however, these values are typically actually just 0 or false. Do i have to specify them as 0 or false or will Twine just ignore that value if i attempt to actually check for it.

Example array with objects:

<<set $Inventory = [
	{
	ID: 01,
	Name: "TestItem1",
	Equipped: false,
	Description: "This is an item",
	Strength: 1,
	Intelligence: 1,
	Agility: 0
    },
    {
	ID: 02,
	Name: "TestItem2",
	Equipped: false,
	Description: "This is another item",
	Strength: 0,
	Intelligence: 0,
	Agility: 1
    }
]>>

Assume i would be making many "items" for this array, would i have to specify it to have Strength: 0 or Intelligence: 0 every time.

 Also when pushing a new object, how would i even go about that?

Assuming i pushed/made the array like this:

<<set $Inventory = [
	{
	ID: 01,
	Name: "TestItem1",
	Equipped: false,
	Description: "This is an item",
	Strength: 1,
	Intelligence: 1
    },
    {
	ID: 02,
	Name: "TestItem2",
	Equipped: false,
	Description: "This is another item",
	Agility: 1
    }
]>>

What would happen if i ran a for loop like this?

<<for $i to 0; $i lt $Inventory.length; $i++>>
<<if $Inventory[$i].Equipped is true>>
<<set $PlayerStrength += $Inventory[$i].Strength>>
<<set $PlayerIntelligence += $Inventory[$i].Intelligence>>
<<set $PlayerAgility += $Inventory[$i].Agility>>
<</if>>
<</for>>

Since the value wouldn't exist for the object im assuming it would break the variable right?

But is the any easier way to actually make long arrays crammed full of objects with really long lists of objects, booleans, strings without actually having to manually fill out 0's and false on all values?

1 Answer

+1 vote
by (159k points)
selected by
 
Best answer

If you haven't defined a specific property (like Intelligence) on an object then code that directly references that undefined property will return the JavaScript special "undefined" value.

There are a number of ways you can solve this issue, one of the simplest is to use a custom function to create your new items. This function will assign default values to all the known properties, and will allow you to over write selected properties for each new item created.

1. Defline the setup.NewItem within your project's Story Javascript area.

This function expects the ID and Name arguments to always be supplied and accepts an optional changes object argument, which contains values for the properties you want to override.

setup.NewItem = function (id, name, changes) {

	/* A new item with default values for all properties. */
	var item = {
		ID: id,
		Name: name,
		Equipped: false,
		Description: "No description",
		Strength: 0,
		Intelligence: 0,
		Agility: 0
	};

	/* Merge the supplied changes with the defaults. */
	if (changes) {
		Object.assign(item, changes);
	}

	return item;
};


2. You would use the new setup.NewItem() function within a passage like so

<<set $Inventory to []>>

/* Create an item with default valaues. */
<<set $Inventory.push(
	setup.NewItem(1, "TestItem1")
)>>

/* Create an item with some overwritten values. */
<<set $Inventory.push(
	setup.NewItem(2, "TestItem2", {
		Description: "This is an item",
		Strength: 1,
		Intelligence: 1
	})
)>>


The following debug code demostrates the changed defaults.
note: Agility will equal zero in both outputs because it hasn't been overwritten.

Item 1:
Description: <<= $Inventory[0].Description>>
Strength: <<= $Inventory[0].Strength>>
Agility: <<= $Inventory[0].Agility>>

Item 2:
Description: <<= $Inventory[1].Description>>
Strength: <<= $Inventory[1].Strength>>
Agility: <<= $Inventory[1].Agility>>

 

by (700 points)
Thanks for the answer! :)
...