# Unity Website for code library?

This topic is 1974 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've developed a class called "Range" which keeps track of a number between a min and max number. It's useful for keeping track of any data which could be represented by a progress bar (hitpoints, mana, cooldown time, etc) and generating a random number within the range. I think other people would find it useful within their projects, so I'd like to share it. Is there a section on this site for freely sharing reusable code? In case there isn't, I'm copy/pasting it below. You're free to use it in any way with no strings attached and no guarantee of fitness/correctness.

Note: My code sample is relatively trivial and generic enough to be usable in any game/app. It's 95% tested, but not very rigorously as it should be.

Range.cs - C# XNA Version:
 using System; using Microsoft.Xna.Framework; namespace EricsLib { /// <summary> /// The range class gives you a number which is between a min and max number. /// Usages: -Keeping track of hitpoints or similar metrics /// -keeping track of ability cool down timers /// -Generating random numbers within a range /// -Storing direction angles or other circular data (use looping values) /// </summary> public class Range { float m_max, m_min; //the thresholds for the meter value float m_regen; //how many points you want to regenerate/degenerate every second float m_current; //the current value of the meter bool m_loopValues; //if this is 'true', then the current value will loop between min and max values instead of being capped. public Range() { m_max = 100; m_min = 0; m_current = 100; m_regen = 0; m_loopValues = false; } /// <summary> /// Creates a range with the current value at max value and min at zero /// </summary> /// <param name="max">the highest range value allowed</param> public Range(float max) { m_max = max; m_current = max; m_min = 0.0f; m_loopValues = false; } /// <summary> /// creates a range value with the current value defaulted to the max value /// </summary> /// <param name="min">lowest range value</param> /// <param name="max">highest range value</param> public Range(float min, float max) { m_max = max; m_min = min; m_current = max; m_loopValues = false; } /// <summary> /// Creates a range /// </summary> /// <param name="min">lowest range value</param> /// <param name="max">highest range value</param> /// <param name="current">current range value</param> public Range(float min, float max, float current) { m_max = max; m_min = min; m_current = current; m_loopValues = false; } /// <summary> /// creates a range value with regeneration/decay value set so the current value increases/decreases over time /// </summary> /// <param name="min">lowest range value</param> /// <param name="max">highest range value</param> /// <param name="current">current value in range</param> /// <param name="regen">the change in current value over time</param> public Range(float min, float max, float current, float regen) { m_max = max; m_min = min; m_current = current; m_regen = regen; m_loopValues = false; } public Range(Range Copy) { if (Copy == null) return; m_max = Copy.m_max; m_min = Copy.m_min; m_current = Copy.m_current; m_regen = Copy.m_regen; m_loopValues = Copy.m_loopValues; } /// <summary> /// The current value will always be between min and max values /// </summary> public float Current { get { return m_current; //E: I'm assuming this gets truncated. } set { //do some quick bounds checking if (value > m_max) //value is greater than specified max value { if (m_loopValues) { //the remaining looped value should be seamlessly added to the minimum value if it overflows the max m_current = m_min + (value % m_max); //tested } else { m_current = m_max; } } else if (value < m_min) //value is less than specified minimum value { if (m_loopValues) { m_current = m_min + (value % (m_max - m_min)); //tested } else { m_current = m_min; } } else m_current = value; } } /// <summary> /// Sets the current value to a random number between the min and max range values /// </summary> /// <param name="SetCurrent">Flag on whether you want to set the current value to the randomly generated number</param> /// <returns>the value of current</returns> public float Random(bool SetCurrent) { double d = Calc.random.NextDouble(); float diff = m_max - m_min; diff *= (float)d; if (SetCurrent == true) { m_current = m_max - diff; } return m_max - diff; } /// <summary> /// Returns a random float which lies between min and max value of the range. /// </summary> /// <returns></returns> public float Random() { return Random(false); } public float Max { get { return m_max; } set { m_max = value; } } public float Min { get { return m_min; } set { m_min = value; } } public float Half { get { return (m_min + m_max) / 2.0f; } } /// <summary> /// Per second regeneration/decay value for the current value of the meter /// </summary> public float RegenRate { get { return m_regen; } set { m_regen = value; } } public void Update(GameTime gameTime) { //are we regenerating/decaying over time? if (m_regen != 0) { Current += m_regen * (gameTime.ElapsedGameTime.Milliseconds); } } public void SetToMax() { m_current = m_max; } public void SetToMin() { m_current = m_min; } public bool IsEmpty() { return (m_current <= m_min); } /// <summary> /// Set/Get the current value as a percentage of the min and max values /// </summary> public float Percent { get { return ((m_current - m_min) / (m_max - m_min)) * 100; } set { m_current = m_min + ((value / 100) * (m_max - m_min)); } } /// <summary> /// if this is 'true', then the current value will loop between min and max values instead of being capped. /// Default: false /// </summary> public bool LoopValues { get { return m_loopValues; } set { m_loopValues = value; } } /// <summary> /// Tells you if the current range value is at the maximum value /// </summary> /// <returns></returns> public bool IsMax() { return m_current == m_max; } /// <summary> /// Tells you if the current range value is at the minimum value /// </summary> /// <returns></returns> public bool IsMin() { return m_current == m_min; } } } 

Range.cs - C# Unity3D version:
 using System; using UnityEngine; namespace EricsLib { /// <summary> /// The range class gives you a number which is between a min and max number. /// Usages: -Keeping track of hitpoints or similar metrics /// -keeping track of ability cool down timers /// -Generating random numbers within a range /// -Storing direction angles or other circular data (use looping values) /// </summary> public class Range { float m_max, m_min; //the thresholds for the meter value float m_regen; //how many points you want to regenerate/degenerate every second float m_current; //the current value of the meter bool m_loopValues; //if this is 'true', then the current value will loop between min and max values instead of being capped. public Range() { m_max = 100; m_min = 0; m_current = 100; m_regen = 0; m_loopValues = false; } /// <summary> /// Creates a range with the current value at max value and min at zero /// </summary> /// <param name="max">the highest range value allowed</param> public Range(float max) { m_max = max; m_current = max; m_min = 0.0f; m_loopValues = false; } /// <summary> /// creates a range value with the current value defaulted to the max value /// </summary> /// <param name="min">lowest range value</param> /// <param name="max">highest range value</param> public Range(float min, float max) { m_max = max; m_min = min; m_current = max; m_loopValues = false; } /// <summary> /// Creates a range /// </summary> /// <param name="min">lowest range value</param> /// <param name="max">highest range value</param> /// <param name="current">current range value</param> public Range(float min, float max, float current) { m_max = max; m_min = min; m_current = current; m_loopValues = false; } /// <summary> /// creates a range value with regeneration/decay value set so the current value increases/decreases over time /// </summary> /// <param name="min">lowest range value</param> /// <param name="max">highest range value</param> /// <param name="current">current value in range</param> /// <param name="regen">the change in current value over time</param> public Range(float min, float max, float current, float regen) { m_max = max; m_min = min; m_current = current; m_regen = regen; m_loopValues = false; } public Range(Range Copy) { if (Copy == null) return; m_max = Copy.m_max; m_min = Copy.m_min; m_current = Copy.m_current; m_regen = Copy.m_regen; m_loopValues = Copy.m_loopValues; } /// <summary> /// The current value will always be between min and max values /// </summary> public float Current { get { return m_current; //E: I'm assuming this gets truncated. } set { //do some quick bounds checking if (value > m_max) //value is greater than specified max value { if (m_loopValues) { //the remaining looped value should be seamlessly added to the minimum value if it overflows the max m_current = m_min + (value % m_max); //tested } else { m_current = m_max; } } else if (value < m_min) //value is less than specified minimum value { if (m_loopValues) { m_current = m_min + (value % (m_max - m_min)); //tested } else { m_current = m_min; } } else m_current = value; } } /// <summary> /// Sets the current value to a random number between the min and max range values /// </summary> /// <param name="SetCurrent">Flag on whether you want to set the current value to the randomly generated number</param> /// <returns>the value of current</returns> public float Random(bool SetCurrent) { double d = Calc.random.NextDouble(); float diff = m_max - m_min; diff *= (float)d; if (SetCurrent == true) { m_current = m_max - diff; } return m_max - diff; } /// <summary> /// Returns a random float which lies between min and max value of the range. /// </summary> /// <returns></returns> public float Random() { return Random(false); } public float Max { get { return m_max; } set { m_max = value; } } public float Min { get { return m_min; } set { m_min = value; } } public float Half { get { return (m_min + m_max) / 2.0f; } } /// <summary> /// Per second regeneration/decay value for the current value of the meter /// </summary> public float RegenRate { get { return m_regen; } set { m_regen = value; } } public void Update() { //are we regenerating/decaying over time? if (m_regen != 0) { Current += m_regen * Time.deltaTime; } } public void SetToMax() { m_current = m_max; } public void SetToMin() { m_current = m_min; } public bool IsEmpty() { return (m_current <= m_min); } /// <summary> /// Set/Get the current value as a percentage of the min and max values /// </summary> public float Percent { get { return ((m_current - m_min) / (m_max - m_min)) * 100; } set { m_current = m_min + ((value / 100) * (m_max - m_min)); } } /// <summary> /// if this is 'true', then the current value will loop between min and max values instead of being capped. /// Default: false /// </summary> public bool LoopValues { get { return m_loopValues; } set { m_loopValues = value; } } /// <summary> /// Tells you if the current range value is at the maximum value /// </summary> /// <returns></returns> public bool IsMax() { return m_current == m_max; } /// <summary> /// Tells you if the current range value is at the minimum value /// </summary> /// <returns></returns> public bool IsMin() { return m_current == m_min; } } } 

##### Share on other sites
If you want to share code publicly, the proper place to do it is on something like github or bitbucket. And instead of saying "You're free to use it in any way with no strings attached and no guarantee of fitness/correctness", it would be better include a proper source code license file with your code.

Also, if you want your code to be "tested", you should actually write tests for it. If you don't know how, do some research on unit testing and NUnit. You can include the test code with your code above. Otherwise, I not consider this to be "tested" code, now matter how many manual tests you performed.

Hope that helps.

##### Share on other sites

And instead of saying "You're free to use it in any way with no strings attached and no guarantee of fitness/correctness", it would be better include a proper source code license file with your code.

Agreed.

@slayemin: Here's some licenses that you might want:

Suggestion for "free to do whatever": MIT license
Suggestion for "free to do whatever but give me credit and share changes": LGPL

Suggestion for "I don't care at all, just use the code if you want!": Public domain ((this isn't actually a license) [size=2]This only works for countries that have public domain, and you can just ignore the countries that don't - people in those countries will use it anyway and it becomes their problem)

For art, music, photographs, and other non-code works, see Creative Commons. Edited by Servant of the Lord

• 9
• 11
• 21
• 10
• 14
• ### Similar Content

• Good day,

I just wanted to share our casual game that is available for android.

Description: Fight your way from the ravenous plant monster for survival through flips. The rules are simple, drag and release your phone screen. Improve your skills and show it to your friends with the games quirky ranks. Select an array of characters using the orb you acquire throughout the game.

Trailer:

• Hello fellow devs!
Once again I started working on an 2D adventure game and right now I'm doing the character-movement/animation. I'm not a big math guy and I was happy about my solution, but soon I realized that it's flawed.
My player has 5 walking-animations, mirrored for the left side: up, upright, right, downright, down. With the atan2 function I get the angle between player and destination. To get an index from 0 to 4, I divide PI by 5 and see how many times it goes into the player-destination angle.

In Pseudo-Code:
angle = atan2(destination.x - player.x, destination.y - player.y) //swapped y and x to get mirrored angle around the y axis
index = (int) (angle / (PI / 5));
PlayAnimation(index); //0 = up, 1 = up_right, 2 = right, 3 = down_right, 4 = down

Besides the fact that when angle is equal to PI it produces an index of 5, this works like a charm. Or at least I thought so at first. When I tested it, I realized that the up and down animation is playing more often than the others, which is pretty logical, since they have double the angle.

What I'm trying to achieve is something like this, but with equal angles, so that up and down has the same range as all other directions.

I can't get my head around it. Any suggestions? Is the whole approach doomed?

Thank you in advance for any input!

• By khawk
Watch the latest from Unity.

• By GytisDev
Hello,
without going into any details I am looking for any articles or blogs or advice about city building and RTS games in general. I tried to search for these on my own, but would like to see your input also. I want to make a very simple version of a game like Banished or Kingdoms and Castles,  where I would be able to place like two types of buildings, make farms and cut trees for resources while controlling a single worker. I have some problem understanding how these games works in the back-end: how various data can be stored about the map and objects, how grids works, implementing work system (like a little cube (human) walks to a tree and cuts it) and so on. I am also pretty confident in my programming capabilities for such a game. Sorry if I make any mistakes, English is not my native language.
Thank you in advance.
• By Ovicior
Hey,
So I'm currently working on a rogue-like top-down game that features melee combat. Getting basic weapon stats like power, weight, and range is not a problem. I am, however, having a problem with coming up with a flexible and dynamic system to allow me to quickly create unique effects for the weapons. I want to essentially create a sort of API that is called when appropriate and gives whatever information is necessary (For example, I could opt to use methods called OnPlayerHit() or IfPlayerBleeding() to implement behavior for each weapon). The issue is, I've never actually made a system as flexible as this.
My current idea is to make a base abstract weapon class, and then have calls to all the methods when appropriate in there (OnPlayerHit() would be called whenever the player's health is subtracted from, for example). This would involve creating a sub-class for every weapon type and overriding each method to make sure the behavior works appropriately. This does not feel very efficient or clean at all. I was thinking of using interfaces to allow for the implementation of whatever "event" is needed (such as having an interface for OnPlayerAttack(), which would force the creation of a method that is called whenever the player attacks something).

Here's a couple unique weapon ideas I have:
Explosion sword: Create explosion in attack direction.
Cold sword: Chance to freeze enemies when they are hit.
Electric sword: On attack, electricity chains damage to nearby enemies.

I'm basically trying to create a sort of API that'll allow me to easily inherit from a base weapon class and add additional behaviors somehow. One thing to know is that I'm on Unity, and swapping the weapon object's weapon component whenever the weapon changes is not at all a good idea. I need some way to contain all this varying data in one Unity component that can contain a Weapon field to hold all this data. Any ideas?

I'm currently considering having a WeaponController class that can contain a Weapon class, which calls all the methods I use to create unique effects in the weapon (Such as OnPlayerAttack()) when appropriate.