I’m thinking of situations like health pickups. Let’s say you’ve got 3 prefabs: small, medium, and large health pickups. They each are running a health pickup script with an exposed variable called “healthAmount”. The only difference between the 3 prefabs are a different mesh and a different healthAmount value.
Now, if these pickups are sprinkled all over the game, you don’t want to have any individual instances overriding the healthAmount b/c you want to maintain consistency and player expectations. However, for game balance tweaking, a designer may want to modify the healthAmount value on the prefab itself.
So, my question is, is this possible? Is there a way to allow healthAmount to be modified on the prefab, but not on the instances? Or is this a case where we just need to rely on the designers to be responsible? The only other option I can think of is storing the values in private script variables which then would require a programmer to tweak.
it might be possible with a custom editor, but I’m not sure how you would differentiate the original prefab from one of it’s instances.
If you make them private members of the pickups, though, you could have them self-set based on the value in a separate object. You’ll need some singleton-like behavior to get this to work, here’s a very simple example in js:
I created this script called “globals” and attached it to my GameManager class; if you don’t have anything like that, you could attach it to the camera or player, anything that exists exactly once in the scene.
var globalA:float=100;
static var instance:globals;
function Awake ()
{
instance=this;
}
static function GetGlobalA() : float
{
return instance.globalA;
}
then I created a dummy cube and attached this script…
private var localA:float;
function Awake ()
{
localA=globals.GetGlobalA();
}
this way, you’ve protected the value from being edited per-instance, while still allowing a designer to edit the values on whatever object you attached the global script to. Has the side-effect of allowing all these related settings to be edited in one place, rather than having to go to each type of health prefab to edit them individually.
If you need the values to be changeable even while running the game in the editor, you could just eliminate localA entirely and just call globals.GetGlobalA() when you need value A, ex, when the health item is picked up. In fact, you should probably do so regardless, for efficiency’s sake.
I’ve used a pseudo-singleton setup in this example just to keep it as simple as possible; search for “singleton” to find more robust examples and info on singletons if you’re interested in extending this idea and making the system more robust.
Here is a script that I think will do everything you need. It has some enumerated types for the standard values but if the designer needs to overide he can.
enum healthSizeType { small, medium, large};
var healthSize : healthSizeType = healthSizeType.medium;
var healthSizeOveride = [10,50,100] ; // Dont touch on pain of death
private var health=0;
function Start()
{
switch(healthSize)
{
case 0:
Debug.Log("small");
health=healthSizeOveride[0];;
break;
case 1:
Debug.Log("medium");
health=healthSizeOveride[1];;
break;
case 2:
Debug.Log("large");
health=healthSizeOveride[2];;
break;
}
Debug.Log(health);
}
Thanks for the suggestions. Here’s the solution I ended up going with. I made a TweakableData prefab containing only the following script:
public class TweakableData : MonoBehaviour {
public int smallHealthSize;
public int mediumHealthSize;
public int largeHealthSize;
void Start( ) {
Debug.LogError( "TweakableDataManager should NOT be placed in a scene or otherwise instantiated. Modify the variables directly on the TweakableDataManager prefab." );
}
// Gets the TweakableData referenced from the SceneManager
public static TweakableData getTweakableData( ) {
GameObject smObj = GameObject.FindGameObjectWithTag( "SceneManager" );
if ( !smObj ) {
Debug.LogError( "No Scene Manager object found in this scene!" );
}
SceneManager sm = smObj.GetComponent< SceneManager >( );
return sm.tweakableDataPrefab.GetComponent< TweakableData >( );
}
}
Then, I added a reference to that prefab in the SceneManager prefab (a GameObject that is required to be placed in every scene of the game).
Now, designers can mess with the variables exposed on the TweakableData prefab without ever touching any instanced objects or writing any code. And instanced objects just access the TweakableData variables by calling TweakableData.getTweakableData( ).smallHealthSize for example.
Note also that this script will produce errors if anyone attempts to instantiate the prefab or forgets to put a SceneManager object in the scene.
I think this covers everything I was looking for. Hope this helps someone else.