0 votes
by (2.7k points)

In other words, how do you do this:

<<set $CurrentWeapon = "Blaster">><<set $Blaster = {"Damage" : 100, "Reload" : 0.7}>>
<<print `'$' + $CurrentWeapon + ["Damage"]`>>

BTW it is actually a lot more complex than that - too much context requried for me to literally copy and paste. However, what is important is that I need to use the value of a variable like $CurrentWeapon and place a dollar symbol in front so that I can reference the variable. I want the output to be the damage of my weapon, not simply $CurrentWeapon["Damage"]. I also need this to work inside <<if>> and <<set>> macros, not just <<print>>. How to do it?

1 Answer

0 votes
by (23.6k points)

It would be better to do it like this:

 

<<set $blaster to {
name: "Blaster",
damage: 100,
reload: 0.7
}>>
<<set $currentWeapon to $blaster>>

$currentWeapon.name [$currentWeapon.damage]

 

by (2.7k points)

That's what I tried. However, say I upgrade the blaster, changing it's damage to 110:

<<button "Upgrade">><<set $Blaster["Damage"] += 10>><</button>>

Referencing it through the $Currentweapon variable does not include the changed damage, it includes the old damage instead. And I cannot refresh the variable every loop in the normal way because I have about 20 weapons and 3 characters, each one can have any one of those weapons equipped. In other words, this does not work:

<<repeat 0.1s>><<set $CurrentWeapon = $Blaster>><</repeat>>

I need this:

<<repeat 0.1s>><<set $CurrentWeapon = `"$" + $CurrentWeapon["Name"]`>><</repeat>>

But it does not work. Any way to fix it?

by (23.6k points)
edited by

Your upgrading doesn't work because you're doing it incorrectly. This is how you'd alter the object properties correctly:

<<set $blaster.damage += 10>>

The whole setup:

<<set $blaster to {
name: "Blaster",
damage: 100,
reload: 0.7
}>>
<<set $currentWeapon to $blaster>>

<span id="example">$currentWeapon.name [$currentWeapon.damage]</span>

<<button "Upgrade">><<set $blaster.damage += 10>><<replace "#example">>$currentWeapon.name [$currentWeapon.damage]<</replace>><</button>>

Now the damage is correctly displyed as 110.

 

Also - I'm not sure what you're using the <<repeat>> macro for, but you should probably start looking for a better solution. It's not good to have a permanent <<repeat>> running in the background.

by (2.7k points)
But what if I want to upgrade the Railgun, but I currently have the Blaster equipped? And how can I then equip it and upgrade it again later?

Anyway, I am using the <<repeat>> macro because I have real-time reloading, which requires a repeated timer.
by (23.6k points)

You don't need anything equipped to upgrade it. If you want to upgrade the railgun you just write:

<<set $railgun.damage += 10>>

If you now equip it later, the damage is going to be the upgraded one.

by (2.7k points)
But then how to upgrade Railgun when equipped without tons of if-statements... ?

Anyway I solved it by using a widget in-passage to figure out the damage given the upgrades and normal damage.
by (159k points)

Anyway, I am using the <<repeat>> macro because I have real-time reloading, which requires a repeated timer.

It is likely that your real-time reloading occurs based on the end-user (player) selecting an option in-game, that option should instigate the updating of the screen instead of using a <<repeat>> macro like the following: (your previous example)

<<repeat 0.1s>><<set $CurrentWeapon = `"$" + $CurrentWeapon["Name"]`>><</repeat>>

Timer Based Event Handlers (like one created by the <<repeat>> macro) interrupt the end-user's ability to interact with the page every time the related timer event fires, and that interruption lasts for the time it takes to process the code related to that timer event.

Eg. In your example the interruption would occur every 10th of a second (or 10 times every second) and that interruption would last for the time it took to execute the code: to determine the value of `"$" + $CurrentWeapon["Name"]` and to update the $CurrentWeapon variable.

In general a Timer Based Event Handler is not the best way to update a page, especial if the update occurs as a result of a manual interaction made by the end-user or as the result of some background code, and if the usage of Timer Based Event Handlers is really required then they should be used sparingly. (eg. only one Timer Based Event Handler per page).

by (23.6k points)
edited by

You upgrade your railgun when equipped the same way you do when it is not equipped.

<<set $railgun.damage += 10>>

Unless there is some pivotal part of your setup I am unaware of, I'm afraid I don't understand your problem. If you want a single upgrade button that upgrades whatever weapon is currently equipped you can handle that with a simple <<for>> loop.

Edit: After testing around a bit, I found out that you actually don't even need the <<for>> loop. You can just say:

<<set $currentWeapon.damage += 10>>

This will upgrade whatever weapon you have equipped without any additional requirements.

by (2.7k points)

But if I do this:

<<set $railgun.damage += 10>>

It does not change the damage when the railgun is equipped, as the Railgun's damage is accessed through the CurrentWeapon variable, to make sure you can only fire the weapon that is currently equipped.

by (23.6k points)

Doing it the way I described will also alter $currentWeapon.damage, so the damage is changed in the way you'd want. You can see that in the setup I have written up there:

<<set $blaster to {
name: "Blaster",
damage: 100,
reload: 0.7
}>>
<<set $currentWeapon to $blaster>>

<span id="example">$currentWeapon.name [$currentWeapon.damage]</span>

<<button "Upgrade">><<set $blaster.damage += 10>><<replace "#example">>$currentWeapon.name [$currentWeapon.damage]<</replace>><</button>>

  By pressing the Upgrade button you can see the value of $currentWeapon.damage changing when you upgrade $blaster.damage.

by (159k points)

It does not change the damage when the railgun is equipped

If a Passage transition occurs after the $currentWeapon variable has been set to reference the object contained within the $railgun variable, then the object referenced by the $currentWeapon variable is no longer the same object referenced by the $railgun variable which is why updating the damage property of the $railgun doesn't effect the damage property of the $currentWeapon.

This is why @idling reassigns the current object referenced by $railgun to the $currentWeapon variable within their previous example, to make the two variables reference the same object again until the next Passage transition occurs 

...