Problem Javascript to C#

Hi everyone,

I’m trying to rewrite my script I made in JavaScript to C#.
I have some difficulty doing some things:

How can I do the following in C# :

Wait for a function to completely finish:

function MyFunction01()
{
...
...
return true;
}

function MyFunction02()
{
yield MyFunction01();
Debug.Log("Finished");
}

Pass a function as parameters:

function getNewID()
{
var newID = ...;
...
return newID;
}

function myFunction(x : int, y : int, z : int)
{
...
}

myFunction(getNewID(),getNewID(),getNewID())

And here the last thing, but not least, very strang, giving me a null reference when trying to acces the Tile variable, it seams that in C# I can’t use the Tile Variable between the if/ else if:

JavaScript (Working)

function myFunction()
{
	var WallTiles : Object[] = Resources.LoadAll("Dungeon/Walls");
	for(var y=0;y<generator.ysize;y++)
	{
		for(var x=0;x<generator.xsize;x++)
		{
			var TheCell : int = generator.getCell(x,y);

			if(TheCell == 1)
			{
				var typeOfTile : Array = generator.getDirectionArray(x,y);
				if(typeOfTile[0] == 0) Tile = Instantiate(WallTiles[Random.Range(0,WallTiles.Length)],Vector3(x*tileSize,0,-y*tileSize),Quaternion.identity); //WALl
				else if(typeOfTile[0] == 1) Tile = Instantiate(CornerTiles[Random.Range(0,CornerTiles.Length)],Vector3(x*tileSize,0,-y*tileSize),Quaternion.identity); //CORNER
				else if(typeOfTile[0] == 2) Tile = Instantiate(DoubleCornerTiles[Random.Range(0,DoubleCornerTiles.Length)],Vector3(x*tileSize,0,-y*tileSize),Quaternion.identity); //DOUBLE CORNER
				Tile.transform.parent = dungeonParent.transform;
				Tile.transform.Rotate(Vector3(0,90 * typeOfTile[1],0));

				//Create Objects
				if(typeOfTile[0] == 0) PopulateObject(Tile, x , y);
			}
		}
	}
}
    
C#

private void myFunction()
{
	Object[] WallTiles = Resources.LoadAll("Dungeon/Floors");
	for(int y=0;y<generator.ysize;y++)
	{
		for(int x=0;x<generator.xsize;x++)
		{

			int TheCell = generator.getCell(x,y);

			GameObject Tile;
			
			//Create Walls
			if(TheCell == 1)
			{
				int[] typeOfTile = generator.getDirectionArray(x,y); 
				if(typeOfTile[0] == 0)  Tile = Instantiate(WallTiles[Random.Range(0,WallTiles.Length)],new Vector3(x*tileSize,0,-y*tileSize),Quaternion.identity) as GameObject; //WALl
				
				else if(typeOfTile[0] == 1)  Tile = Instantiate(CornerTiles[Random.Range(0,CornerTiles.Length)],new Vector3(x*tileSize,0,-y*tileSize),Quaternion.identity)  as GameObject ; //CORNER
				
				else if(typeOfTile[0] == 2)  Tile = Instantiate(DoubleCornerTiles[Random.Range(0,DoubleCornerTiles.Length)],new Vector3(x*tileSize,0,-y*tileSize),Quaternion.identity)  as GameObject ; //DOUBLE CORNER	

				Tile.transform.parent = dungeonParent.transform;
				Tile.transform.Rotate(new Vector3(0,90 * typeOfTile[1],0));
				
				if(typeOfTile[0] == 0) PopulateObject(Tile as GameObject, x , y)
			}
		}
	}
}

Thank you very much if you take the time to answer me, I’m kind of stuck right now to continue my game :s

Well, moving from top to bottom, unless you have a function that contains a yield itself, you don’t need to do anything special. The function will be evaluated like it was part of the calling function’s code. If you want to emulate that behavior exactly, replace yield with yield return and have the called function return either a IEnumerable or an IEnumerator.

For the second one, that function call you wrote will work in C# without changes. You aren’t actually passing the function, the function is evaluated, then is given to the function you are passing it into as a parameter. If you want to pass the functions themselves, look up delegates in C#.

Lastly, I think you aren’t actually ever setting the value of tile for just one or two cases, but since it works in JS, I don’t really know why that wouldn’t work. I need the actual error to know what the problem is.

Well, macfanpro said most of this already…

In C# every function needs to specify it’s return type. If a function doesn’t return any value you have to use the type “void”. Your first function returns a boolean (JS) / bool (C#) so you have to declare it like this:

bool MyFunction01()
{
    ...
    ...
    return true;
}

Coroutines (functions that contain one or more yield-statements) returns an IEnumerator. In JS that is done implicitly but in C# you have to specify the return type.

IEnumerator MyFunction02()
{
    yield MyFunction01();
    Debug.Log("Finished");
}

Also to start such a coroutine you have to use StartCoroutine(MyFunction02()) which is done behind the scenes in JS.

Like already said in your next case you don’t pass a function to another function. You just pass the returned value as parameter to the function. and it works like in JS

int getNewID()
{
    int newID = ...;
    ...
    return newID;
}

void myFunction(int x, int y, int z)
{
    ...
}

myFunction(getNewID(),getNewID(),getNewID());

Your “main” function problems:

Make sure that you initialize your variables. In your case if typeOfTile[0] is 3 or greater Tile would not be assigned and as long as there is one possible case the compiler will throw at least a warning. When you declare it like this: GameObject Tile = null; there will be no warning and it you get a null-ref-exception you know where it comes from.

You shouldn’t use the “as”-cast unless you need it’s special behaviour. A normal c-style-cast will throw a cast-exception if it can’t cast the reference into the desired type. The as-cast will return null when the casting fails. Using the as cast can result in nullreference exceptions and you don’t have a clue where it comes from.

Use the c-style cast:

Tile = (GameObject)Instantiate(...);

Hey, Thanks a lot macfanpro & Bunny83,
Taking the time to make a good and constructive answer :wink:

That help me a lot, everything is working :wink:

Actually, 2 of my errors came from the Resources.LoadAll.

Object[] WallTiles = Resources.LoadAll("Dungeon/Walls");
WallTiles[Random.Range[0,WallTiles.Length]);

This was giving me a Transform and not a GameObject, so it’s why I had null exception,
so I just put the typeof I wanted:

Object[] WallTiles = Resources.LoadAll("Dungeon/Walls",typeof(GameObject))

And everything works fine.

Just to finish, I really don’t get the Yield & Coroutine things, I read the doc, read your explanations, but I still don’t get it:

For instance, I have my generateDungeon function, and inside some function called, but I want to wait before each are finished before going forward:

public void generateDungeon(float _id, float _entryID, float _exitID)

{
...
generator.createDungeon(x, y, dungeon_objects); <- Wait before continue
...
Build3DDungeon(); <- Wait before continue
...
CreateMiniMap();  <- Wait before continue
...
Populate();  <- Wait before continue
...
SaveDungeon();
}

I tried this:

yield return Build3DDungeon();
or
yield return StartCoroutine(Build3DDungeon());

with Build3DDungeon() an Ienumerator function

But I got the error: not all code paths return a value

Does Ienumerator have to return something ? Like a bool ?

Thanks Again !
125125125