0 votes
by (120 points)
edited by

Hey all

I am trying to build some "equipment system" for days right now, but since i don't have a idea HOW to get it to work, i would like to get some help...

Basicly there should a [change your cloth] passage.

There should be a table with the following content:

Slot | Name | Picture | Change

Also there should be some DefineCloth passage, where cloth can be defined. (had a look into some other works, where this is usually done as array for easyly adding stuff later on.

As a Example for [DefineCloth]

<<set $Head[0]={
	name: "none",
	type: "Head",
	unlocked: true,
	equipped: true,
	cost: 0
}>>
<<set $Head[1]={
	name: "Glasses",
	type: "Head",
	unlocked: true,
	equipped: false,
	cost: 0
}>>
<<set $Head[2]={
	name: "Pirate hat",
	type: "Head",
	unlocked: false,
	equipped: false,
	cost: 120
}>>

My Table initialization looks like this:

<table border = "2px">
		<tr>
			<th>Slot</th>
			<th>Wearing</th>
			<th>Image</th>
			<th>Change</th>
		</tr>
        <tr>
			<td>Head</td>
			<td>
			<<for _i to 0; _i lt $Head.[$wHead].length; _i++>>
				<<if $Head.[$wHead].equipped is true>>
					<<print $Head[$wHead].name>>
				<<else>> <<set $wHead ++>>
				<</if>>
			<</for>>
			</td>
			<td>Image</td>
			<td>[[Change|Head]]</td>
		</tr>
</table>

Somehow .length seems not to work :/

Also I wonder if the locataion of the pictures should be stored inside the [DefineCloth]?

 

1 Answer

0 votes
by (144k points)

Unless you have pre-initialised your $head Array to a set size then you would use code like the following within your StoryInit special passage to add those head items to an empty Array.

<<set $head to []>>
<<set $head.push({
	name: "none",
	type: "Head",
	unlocked: true,
	equipped: true,
	cost: 0
})>>
<<set $head.push({
	name: "Glasses",
	type: "Head",
	unlocked: true,
	equipped: false,
	cost: 0
})>>
<<set $head.push({
	name: "Pirate hat",
	type: "Head",
	unlocked: false,
	equipped: false,
	cost: 120
})>>


There are a couple of issues with your original example:

1. You are not suppressing or escaping the invalid line-breaks within the HTML table structure.

By default each line-break within the contents of a Passage is automatically converted to a HTML br element, and this element is invalid within a table structure if they appear anywhere other than inside the th or td elements.

2. You are using an undefined $wHead story variable as the index reference of your $head Array.

You would generally use the _i temporary variable you defined in your <<for>> macro for that.

3. You are trying to use Dot Notation to dynamically access the elements of your $head Array.

You can't do that, and will need to use square bracket notation instead as demostrated by the following example.

\<<for _i to 0; _i lt $head.length; _i++>>
	\<<if $head[_i].equipped>>
		\<<print $head[_i].name>>
		\<<break>>
	\<</if>>
\<</for>>

4. You aren't using a <<break>> macro to stop the <<for>> macro ones the equipped item is found.


The following is a slightly modified version of your original code, it resolves the above mentioned issues as well as uses the range version of the <<for>> macro to make things a little easier.

<table border = "2px">
	\<tr>
		\<th>Slot</th>
		\<th>Wearing</th>
		\<th>Image</th>
		\<th>Change</th>
	\</tr>
	\<tr>
		\<td>Head</td>
		\<td>
			\<<for _i, _item range $head>>
				\<<if _item.equipped>>
					\<<print _item.name>>
					\<<break>>
				\<</if>>
			\<</for>>
		\</td>
		\<td>Image</td>
		\<td>[[Change|Head]]</td>
	\</tr>
\</table>

 

> I wonder if the locataion of the pictures should be stored inside the...

Personally I would store the images as external files in a location relative to the folder you save your published story HTML file in, you could then use a Relative URL to reference them.

by (190 points)
Equipment would work for stores I would think but Idea you have pretty interesting there also what others I seen have idea using paper doll system. It kind throws out making characters all together? I don't know how its simplified or customized where you can modify to make it work for twine.
Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.
...