Skip to content
This repository has been archived by the owner on Jul 1, 2019. It is now read-only.

Add flag math operations #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions source/docs/basics-of-scripting/memory-flags-and-definitions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,72 @@ Of course, you can do a bit more with flags than you can with definitions.
2B. Math With Flags
~~~~~~~~~~~~~~~~~~~

You can do more than just store raw information with flags - you can also do math! Flag math operations support addition, subtraction, multiplication, and division. The operation you specify will be performed on the value currently stored. Note that this only works for flags with numerical values - if you have stored text on a flag, you cannot use math to change the value!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line doesn't seem to obey the 120-visible-character limit. Of course it doesn't matter to the frontend user, but for backend users who will be seeing the source and editing it (hopefully), they need to be able to read the backend without having to scroll horizontally.


For example, first we'll use the ``/ex`` command to store a value in a flag:

.. code::

/ex flag server DoSomeMath 10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to put a space between DoSomeMath and 10?

Also, generally speaking since most things involving an assigned ID tend to be case insensitive, I think it's a good idea to use all lowercase names, plus underscores.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, yes insofar as I am more used to using define than flag and I probably should have checked before committing.

I use CapitalCase or whatever in my internal scripts for clarity, but if you want all-lowercase that's fine!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For internal/personal use, CapitalCase is fine. It's just that for a public script or something, it should be encouraged to not use CaptialCase since names tend to be case insensitive anyways, so it's less of a hassle to just use all lowercase plus underscores. Saves the theoretical trouble of naming two definitions "HelloWorld" and "HELLOworld" and them overriding each other.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that is good to know! I look forward to a design document, and hopefully we can augment it over time by way of me attempting contributions. 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A technical detail:

Shouldn't you put that command in a script container? I know it may seem redundant, but a task or world script may better frame the command to the user.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was a good example of being able to set flags with the command, but sure that would work too.


That command will store the value ``10`` in the server flag ``DoSomeMath``. Now, let's do some math!

.. code::

flagmath:
type: world
events:
on player breaks sand:
- flag server DoSomeMath:+:2
- narrate <server.flag[DoSomeMath]>

This script will add 2 to the value of the server flag ``DoSomeMath`` when a player breaks sand. Then, it will narrate the value of that flag. Why don't we try subtraction?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment for line 223.


.. code::

flagmath:
type: world
events:
on player breaks sand:
- flag server DoSomeMath:-:4
- narrate <server.flag[DoSomeMath]>

If you guessed that this script will subtract 4 from the server flag ``DoSomeMath`` each time a player breaks sand, you're correct! You can also do multiplication and division:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment for line 223.


.. code::

flagmath:
type: world
events:
on player breaks sand:
- flag server DoSomeMath:*:10
- narrate <server.flag[DoSomeMath]>

This script will multiply the server flag ``DoSomeMath`` by 10 each time a player breaks sand.

.. code::

flagmath:
type: world
events:
on player breaks sand:
- flag server DoSomeMath:/:3
- narrate <server.flag[DoSomeMath]>

Finally, this script will divide the server flag ``DoSomeMath`` by 3 each time a player breaks sand.

There are two more options for doing math with flags. You can ``- flag server MyAdditiveFlag:++`` to add 1 to the flag and ``- flag server MySubtractiveFlag:--`` to subtract one from the flag. The addition or subtraction will occur each time the command is run. You can use these commands to easily track simple variables. For example, this script will track zombie deaths:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment for line 223.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional commentary: I don't feel that this section is very clear on how the :++ and :-- actions work. For example, it's not clarified that they don't take a value, and that they naturally only add/subtract 1.

Additionally, flags are already introduced as a way to store values in a more persistent manner, so there's no need to reiterate that they can track simple values. Just note that these mathematical operators can be used to directly change the value of the flag, given that the flag's value is a number.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++ is increment, -- is decrement.
Addition/Subtraction are higher order math. x + y is basically repeat y { x++ }
Similar to how multiplication like x * y is the next order above addition repeat y { answer += x }


.. code::
ZombieDeathTracker:
type: world
events:
on zombie dies:
- flag server DeadZombies:++

With that script, every time a zombie dies, the ``DeadZombies`` server flag will have 1 added to it.

Using math operations on flags is a quick and easy way to keep track of numerical values and modify them as you need!

2C. Server vs. Player
~~~~~~~~~~~~~~~~~~~~~
Expand Down