0 votes
by (350 points)

I use a show/hide text code in my game. But when the player clicks on the link, I want to change a variable besides showing the hidden text. Is this possible?

Here is the link to show/hide the text:

<<link "How old are you?">>
	<<if (ndef _description) or _description is "">>
		<<set _description to "I am 29.">>
	<<else>>
		<<set _description to "">>
	<</if>>
		<<replace "#text">>_description<</replace>>
<</link>>
<span id="text"></span>

 

1 Answer

0 votes
by (68.6k points)

You may modify variables within the <<link>>, yes.

Your example already shows you modifying a temporary variable, _description, so….

by (350 points)
Ok. But I want to change a variable is outside the hide/show code. A weapon vector, specifically. I tried many times, but no success.
by (68.6k points)
You can do that, as noted.

Assuming you're referring to an array when you say vector.  What exactly do you want to do to it?  You need to give details because we cannot guess what you want.
by (350 points)

What I want to do is:

When the player clicks the link, it will open a hidden text. So far so good. I already have the code for this. But I want to add a new weapon to the player's inventory. In terms of code, I want to add the "shotgun" to the vector weapons_vet using the following code:

<<for $count=0; $count <= $weapons_vect.length; $count++>>
	<<if $weapons_vect[$count] is "empty">>
		<<set $weapons_vect[$count] to "shotgun"; $weapon_damage[$count] to 5; $weapon_equip to $weapons_vect[$count]; $weapon_equipdamage to $weapon_damage[$count]; $count to $weapons_vect.length + 1>>
	<<elseif $weapons_vect[$count] is "shotgun">>
		<<print "You already have a shotgun.">>
	<<else>>
		<<break>>
	<<endif>>
<<endfor>>

 

by (350 points)

But if I can only add simple code inside the << link >> code it would be fine. A code like this:

<<set $weapons_vect[0] to "shotgun"; $weapons_damage[0] to 5>>

 

by (68.6k points)

You may place the loop within the <<link>> before or after the replace code.  I'm unsure what you've tried previously, however, there should be no problems doing so.  If you can show what you've tried, we can probably tell you what's wrong.


That said.  Your loop example is needlessly complicated and kind of broken.

  1. You should likely be using a temporary variable for your loop index.
  2. You should be using <<break>> after adding the shotgun, rather than modifying $count.
  3. You're using <<print>> unnecessarily here.
  4. Where you currently have <<break>> now should probably be a <<continue>>, otherwise the first member which isn't "empty" or "shotgun" will end the loop.  That said, since there's no additional clauses an nothing after the <<if>> in the loop, you don't need the <<else>> clause or <<continue>> at all.
  5. This may or may not be an issue, however, you're not checking to see if a member with the value "empty" comes before one with "shotgun", which would mean that you could add multiple shotguns.  That may not be possible with how you're using this or may be intended, so disregard this entry if either is true.

An example of what it should probably look like (without changing it too much):

<<for _i = 0; _i < $weapons_vect.length; _i++>>
	<<if $weapons_vect[_i] is "empty">>
		<<set
			$weapons_vect[_i] to "shotgun";
			$weapon_damage[_i] to 5;
			$weapon_equip to $weapons_vect[_i];
			$weapon_equipdamage to $weapon_damage[_i]
		>>
		<<break>>
	<<elseif $weapons_vect[_i] is "shotgun">>
		You already have a shotgun.
	<</if>>
<</for>>

Then again, have an array populated with the string "empty" is already kind of bizarre, so….  I'm assuming it's supposed to be representing fixed slots or something to that effect.

That said, if you're going to be using the loop in multiple places you may wish to make a widget out of it.  For example: (goes in a separate widget-tagged passage)

<<widget "addweapon">>\
<<silently>>
	<<set
		_weapon to $args[0];
		_damage to $args[1];
		_error  to ""
	>>
	<<for _i = 0; _i < $weapons_vect.length; _i++>>
		<<if $weapons_vect[_i] is "empty">>
			<<set
				$weapons_vect[_i] to _weapon;
				$weapon_damage[_i] to _damage;
				$weapon_equip to _weapon;
				$weapon_equipdamage to _damage
			>>
			<<break>>
		<<elseif $weapons_vect[_i] is _weapon>>
			<<set _error to "You already have a " + _weapon + ".">>
		<</if>>
	<</for>>
<</silently>>\
<<if _error>>_error<</if>>\
<</widget>>

Usage would be:

<<addweapon "shotgun" 5>>

 

by (350 points)

Thank you very much for the help!

I changed my code after your tips:

<<widget "addweapon">>\
<<silently>>
	<<set
		_weapon to $args[0];
		_damage to $args[1];
		_pickup to ""
		_error  to ""
	>>
	<<for _count = 0; _count <= $armas_vet.length; _count++>>
		<<if $weapons_vect[_cont] is "empty">>
			<<set
				$weapons_vect[_count] to _weapon;
				$weapons_damage[_count] to _damage;
				$weapon_equip to _weapon;
				$weapon_equipdamage to _damage
				_pickup to "You got a " + _weapon + ". Now your attack damage is " + _damage + "."
			>>
			<<break>>
		<<elseif $weapons_vect[_count] is _weapon>>
			<<set _error to "You already have a " + _weapon + ".">>
			<<break>>
		<</if>>
	<</for>>
<</silently>>\
<<if _pickup>>_pickup<</if>>\
<<if _error>>_error<</if>>\
<</widget>>

But I tried in several ways to include the above code inside the <<link>> code and no attempt worked.

by (68.6k points)

You broke the first and second <<set>> by adding additional statements without terminating the previous one with a semi-colon.  It may be better for you to either terminate all assignment statements or use separate <<set>> invocations for each variable.

Also, you don't need a separate temporary variable for pickup and errors messages, so that's redundant.

For example:

<<widget "addweapon">>\
<<silently>>
	<<set _weapon to $args[0]>>
	<<set _damage to $args[1]>>
	<<set _message to "">>
	<<for _count = 0; _count <= $armas_vet.length; _count++>>
		<<if $weapons_vect[_cont] is "empty">>
			<<set $weapons_vect[_count] to _weapon>>
			<<set $weapons_damage[_count] to _damage>>
			<<set $weapon_equip to _weapon>>
			<<set $weapon_equipdamage to _damage>>
			<<set _message to "You got a " + _weapon + ". Now your attack damage is " + _damage + ".">>
			<<break>>
		<<elseif $weapons_vect[_count] is _weapon>>
			<<set _message to "You already have a " + _weapon + ".">>
			<<break>>
		<</if>>
	<</for>>
<</silently>>\
<<if _message>>_message<</if>>\
<</widget>>

 

...