- Home /
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
function myFunction(x : int, y : int, z : int) { ... } should be: void $$anonymous$$yFunction(int x, int y, int z) {...}
Answer by ckfinite · Jul 18, 2011 at 01:11 PM
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.
Answer by Bunny83 · Jul 18, 2011 at 02:52 PM
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(...);
Answer by Ziboo · Jul 18, 2011 at 05:15 PM
Hey, Thanks a lot macfanpro & Bunny83, Taking the time to make a good and constructive answer ;)
That help me a lot, everything is working ;)
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
coroutines are used when you wnat to execute some parts later and in the meantime you yield the control back to unity and the game keeps on running. Yield just interrupts the execution at this point and it get's resumed later.
I'm not sure what your Build3DDungeon function is supposed to do. If it does something in one-run it don't need to be a coroutine. A normal function call will do. If you want to use yield inside the function you have to start it with StartCoroutine. If you want to wait for a coroutine to finish the calling function also needs to be a coroutine.
With yield return StartCoroutine(Build3DDungeon());
you wait for this coroutine to finish.
Oh and you should try to break your question into multiple questions. Asking 2 or more questions just lead to very long and confusing posts. Besides that you shouldn't use the Answer function if it's not an actual answer to the question above.
There's absolutely no problem when you answer your own question but for comments we have a comment function ;). You can also edit your question to improve it or add something if necessary.
Your answer
Follow this Question
Related Questions
translate javascript for loop to C# 2 Answers
Weird warnings after translating javascript to C# 2 Answers
Help With Java Script - C# 1 Answer
Distribute terrain in zones 3 Answers
How to make this line work in C#? 1 Answer