Passing Classes to functions without Typing

ok, so I’m trying to use classes to create a group of vars that define specific level parameters. Then I want to be able to run the same function on the classes.

Problem is, the function’s variables can’t be typed to one specific class, and if I don’t type the variable, I get a “‘variable’ is not a member of ‘Object’”

here’s basically what I’m trying to do:

class Level01Stats{
    var 1 = 0;
    var 2 = 0;
    etc....
}

class Level02Stats{
    var 1 = 0;
    var 2 = 0;
    etc....
}

var level01 : Level01Stats;
var level02 : Level01Stats;

function Update(){
    DoData(level01);
    DoData(level02);
}

function DoData(currentLevel){
    var dataprocessing = CheckData(currentLevel);
}

Just to complicate things, I’m also doing this stuff in separate scripts (which all work if I type the currentLevel variable).

Is there a better way to do this? I’d rather not have hundreds of lines of code to run the same function…

There is a better way! Interfaces!

Unfortunately, I can’t find the JavaScript syntax for interfaces (google has failed me!) so hopefully one of the gurus here can chime in to supply that. But in C#:

public interface LevelStats
{
	int SomeVar1;
	int SomeVar2;
}

public class Level01Stats : LevelStats
{
	public int SomeVar1;
	public int SomeVar2;
}

public class Level02Stats : LevelStats
{
	public int SomeVar1;
	public int SomeVar2;
}




public Level01Stats level01;
public Level02Stats level02;

private void Update()
{
	DoData(level01);
	DoData(level02);
}

private void DoData(LevelStats currentLevel)
{
	Debug.Log("SomeVar1: " + currentLevel.SomeVar1);
}

Notice that the DoData is typed against the LevelStats interface and not against the concrete class Level01Stats or Level02Stats.

There are other ways of doing this: you could have created a base class instead of an interface and shared the common properties. I suggest you do some googlings for “polymorphism”.

This page shows interfaces in JS; however, are Level01Stats and Level02Stats actually the same? If so, it seems to me that you’d be better off just using an array.

–Eric

Thanks for the link, that was the page I was trying to look for. (I think I was googling “Javascript update” instead of “Mono upgrade”)

Yeah, I thought he wanted an interface (same name variables, but different declarations) If these stats are supposed to be the same:

public class LevelStats
{
	var SomeVar1 : int;
	var SomeVar1 : int;
}

var level01 : LevelStats;
var level02 : LevelStats;

function Update()
{
	DoData(level01);
	DoData(level02);
}

function DoData(currentLevel : LevelStats)
{
	Debug.Log("SomeVar1: " + currentLevel.SomeVar1);
}

And definitely utilize arrays if possible if you’re going to be managing a lot of levels.

The problem of using Arrays is that some of the level data is strings, some of it is Vector3 info. Array’s can’t have different data types can they?

oh, and each level stat has to have different data in it. (basically it defines the entire level)…

Why don’t you have an array of the LevelStats class?

–Eric

I think Arrays are the right way to go. The main reason I was using classes was so that I could have an easy method to manage all the level data in the inspector, but after reading up on BuiltIn Arrays, I learned that they can do it too.

Since most of the data is Vector3 info (for now) I think I can come up with code for the strings that I was using, since there’s only a handful of choices the strings represent anyway.

Oh, and interfaces won’t work, cause I’m using Unity iPhone 1.7. I’d have to pay the $400 to upgrade to Unity3 to get the interfaces stuff…

As I said, why don’t you use an array of the LevelStats class?

class LevelStats {
	var someVar1 : int;
	var someVar2 : int;
}

private var levelStats : LevelStats[];

function Start () {
	levelStats = new LevelStats[10]; // Assuming you have 10 levels
}

–Eric

That looks exactly like what I need.

I can tell with my builtin array plan that the data would quickly get unmanageably.

two questions though…

Would that work with separate data types (like var1 is an int, var2 is a vector3 and var3 is a string)?

How do you access the individual vars in the array?

So I get two errors when I tried it:

“Cannot Convert (LevelStats) to LevelStats”
Type ‘LevelStats’ does not support slicing

Just an oversight/typo on Eric’s part, he left out the [ ] array brackets:

class LevelStats 
{
	var var1 : int;
	var var2 : Vector3;
	var var3 : string;
}

private var levelStats : LevelStats[];

function Start () {
	levelStats = new LevelStats[10]; // Assuming you have 10 levels
	
	for (var i : int = 0; i < levelStats.Length; i++)
	{
		levelStats[i] = new LevelStats();
	}
	
	//level 1
	levelStats[0].var1 = 1;
	levelStats[0].var2 = new Vector3(1, 1, 1);
	levelStats[0].var3 = "Level 1";
}

Not necessarily the way I would go about initializing your levels, but demonstrates your question as to differently typed fields and accessing their values.

Yeah, typo, sorry bout that…

–Eric

I really would like to store all the individual level data as their own classes to keep things easy to edit in the inspector. How would I go about assigning the variables without having to go level by level?

assuming:

class LevelStats 
{
	var var1 : int;
	var var2 : Vector3;
	var var3 : string;
}

class Level01 
{
	var var1 : int = 10;
	var var2 : Vector3 =  (3,2,1);
	var var3 : string = "triangle";
}

private var levelStats : LevelStats[];
private var level01 : Level01;

is there a way I can do that in a for loop?

Just do

var levelStats : LevelStats[];

Having separate variables makes it harder rather than easier.

–Eric

I think you nailed exactly what I needed, Eric!

Just to make sure, here’s what I’m thinking:

class LevelStats 
{
	var var1 : int;
	var var2 : Vector3;
	var var3 : string;
}

var levelStats : LevelStats[];
var level01 : LevelStats;
var level02 : LevelStats;
etc...

function Start(){
     levelStats = new LevelStats[10];
     levelStats[0] = level01;
     levelStats[1] = level02;
     etc....
}

This way I can have the level stats editable inside the inspector in a nice cleanly sorted way, and have the level stats info be available where I need it.

I did a quick test of it, and it seems to work just fine…

No. :slight_smile:

var levelStats : LevelStats[];

That’s it. Don’t use the separate level01 etc. variables. You’re not gaining anything, except to make it more complicated and harder to deal with.

–Eric

I got it now. Thanks for all your help!

By the way, a hint for arrays of classes like this is that is you option-click on the arrow to expand the LevelStats array, all the sub-elements will be automatically expanded too, so you don’t have to open them all individually.

–Eric