- Home /
This post has been wikified, any user with enough reputation can edit it.
JS Conversion to C# - ToBuiltin? Unity 3 Game Development Hotshot from Jate Wittayabundit
In order to be consistent in the game dev environment, I try to convert JS to C# and have the following problem, apparently with the ToBuiltin function: The original Code in JS from the book:
public function SaveGame (scores : int, name : String) : void {
//Submitting the score if the score is higher than the minimum score of the database
if (scores >= int_minScore) {
var a_newData : Array = new Array(as_users);
//Removing the last Array
a_newData.Pop();
//Creating new user and save it to array
var obj_user : UsersData = new UsersData();
obj_user.Init(name, scores);
a_newData.Add(obj_user);
//Setting JS Array back to Builtin
as_users = a_newData.ToBuiltin(UsersData);
//Sorting Data
SortUser(as_users);
}
for (var i: int = 0; i < int_maxUser; i++) {
as_users[i].SaveLocal(i);
}
}
Now, my translation:
public void SaveGame(int scores, string name)
{
if(scores >= int_minScore)
{
ArrayList a_newData = new ArrayList(as_users);
a_newData.RemoveAt(int_maxUser - 1);
UsersData obj_user = new UsersData();
obj_user.Init(name, scores);
a_newData.Add(obj_user);
as_users = a_newData.ToArray(UsersData);
SortUser(as_users);
}
for( int i = 0; i < int_maxUser; i++)
{
as_users[i].SaveLocal(i);
}
}
Can somebody help me with the right conversion? Thank you.
Comment
Answer by CurtisMcGill · Aug 23, 2013 at 07:26 PM
I haven't fully tested this, but would do something like...
public class test : MonoBehaviour
{
private int int_minScore = 10;
private int int_maxUser = 10;
private List<UsersData> as_users = new List<UsersData>();
public void Start()
{
// Load list
UsersData objUser = new UsersData();
for (int i = 0; i < int_maxUser; i++)
as_users[i] = objUser.LoadLocal(i);
}
public void SaveGame(int scores, string name1)
{
UsersData objUser = new UsersData();
// Submitting the score if the score is higher than
// the minimum score of the database
if (scores >= int_minScore)
{
//Removing the last Array
as_users.RemoveAt(as_users.Count - 1);
//Creating new user and save it to array
objUser.Init(name1, scores);
as_users.Add(objUser);
//Sorting Data
as_users.Sort();
}
foreach (UsersData usersData in as_users)
objUser.SaveLocal(usersData);
}
}
public class UsersData : MonoBehaviour
{
public void Init(string name1, int scores)
{
}
public UsersData LoadLocal(int num)
{
}
public void SaveLocal(UsersData udata)
{
}
}