- Home /
JS to C# conversion is a complete disaster
I've spent literally all day trying to convert a JS script for an object recycler to C#. I know absolutely nothing about JS so I had to do a lot of Googling and documentation searching. But I think it's all the for loops that done me in because they're throwing a whole list of errors at me. Can anyone try to tell what I did wrong? Here is the original JS script:
#pragma strict
import System.Collections.Generic;
private class PoolInfo extends System.Object {
public var poolName : String;
public var prefab : GameObject;
public var poolSize : int;
public var canGrowPoolSize : boolean = true;
}
private class Pool extends System.Object {
private var list : List.<PoolObject> = new List.<PoolObject>();
public var canGrowPoolSize : boolean;
public function Add(poolObject : PoolObject) {
list.Add(poolObject);
}
public function Count() : int {
return list.Count;
}
public function ObjectAt(index : int) : PoolObject {
var result : PoolObject;
if(index < list.Count) {
result = list[index];
}
return result;
}
}
static var instance : EasyObjectPool;
var poolInfo : PoolInfo[];
private var poolDictionary : Dictionary.<String, Pool> = new Dictionary.<String, Pool>();
function Start () {
//set instance
instance = this;
//check for duplicate names
CheckForDuplicatePoolNames();
//create pools
CreatePools();
}
private function CheckForDuplicatePoolNames() {
for (var index : int = 0; index < poolInfo.Length; index++) {
var poolName = poolInfo[index].poolName;
if(poolName.Length == 0) {
Debug.LogError(String.Format("Pool {0} does not have a name!",index));
}
for (var internalIndex : int = index + 1; internalIndex < poolInfo.Length; internalIndex++) {
if(poolName == poolInfo[internalIndex].poolName) {
Debug.LogError(String.Format("Pool {0} & {1} have the same name. Assign different names.", index, internalIndex));
}
}
}
}
private function CreatePools() {
for (var currentPoolInfo in poolInfo) {
var pool : Pool = new Pool();
pool.canGrowPoolSize = currentPoolInfo.canGrowPoolSize;
for(var index : int = 0; index < currentPoolInfo.poolSize; index++) {
//instantiate
var go : GameObject = Instantiate(currentPoolInfo.prefab);
var poolObject : PoolObject = go.GetComponent(PoolObject);
if(poolObject == null) {
Debug.LogError("Prefab must have PoolObject script attached!: " + currentPoolInfo.poolName);
} else {
//set state
poolObject.ReturnToPool();
//add to pool
pool.Add(poolObject);
}
}
Debug.Log("Adding pool for: " + currentPoolInfo.poolName);
poolDictionary[currentPoolInfo.poolName] = pool;
}
}
function GetObjectFromPool(poolName : String) : PoolObject {
var poolObject : PoolObject;
if(poolDictionary.ContainsKey(poolName)) {
var pool : Pool = poolDictionary[poolName];
//get the available object
for (var index : int = 0; index < pool.Count(); index++) {
var currentObject : PoolObject = pool.ObjectAt(index);
if(currentObject.IsAvailableForReuse()) {
//found an available object in pool
poolObject = currentObject;
break;
}
}
if(poolObject == null) {
if(pool.canGrowPoolSize) {
Debug.Log("Increasing pool size by 1: " + poolName);
//find the prefab for this poolname
for (var currentPoolInfo in poolInfo) {
if(poolName == currentPoolInfo.poolName) {
var go : GameObject = Instantiate(currentPoolInfo.prefab);
poolObject = go.GetComponent(PoolObject);
//set state
poolObject.ReturnToPool();
//add to pool
pool.Add(poolObject);
break;
}
}
} else {
Debug.LogWarning("No object available in pool. Consider setting canGrowPoolSize to true.: " + poolName);
}
}
} else {
Debug.LogError("Invalid pool name specified: " + poolName);
}
return poolObject;
}
Here is my hack-job in C#:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EasyObjectPool : MonoBehaviour {
private class PoolInfo{
public string poolName;
public GameObject prefab;
public int poolSize;
public bool canGrowPoolSize = true;
}
private class Pool{
private List<PoolObject> list = new List<PoolObject>();
public bool canGrowPoolSize;
public void Add (PoolObject poolObject){
list.Add(poolObject);
}
public int Count (){
return list.Count;
}
public GameObject ObjectAt ( int index ){
PoolObject result;
if(index < list.Count) {
result = list[index];
}
return result;
}
}
static public EasyObjectPool instance ;
PoolInfo[] poolInfo;
private Dictionary<string, Pool> poolDictionary = new Dictionary<string, Pool>();
void Start () {
instance = this;
CheckForDuplicatePoolNames();
CreatePools();
}
private void CheckForDuplicatePoolNames() {
for (int index = 0; index < poolInfo.Length; index++) {
poolName= poolInfo[index].poolName;
if(poolName.Length == 0) {
Debug.LogError(String.Format("Pool {0} does not have a name!",index));
}
for (int internalIndex = index + 1; internalIndex < poolInfo.Length; internalIndex++) {
if(poolName == poolInfo[internalIndex].poolName) {
Debug.LogError(String.Format("Pool {0} & {1} have the same name. Assign different names.", index, internalIndex));
}
}
}
}
private void CreatePools() {
foreach(int currenPoolInfo in poolInfo){
Pool pool = new Pool();
pool.canGrowPoolSize = currentPoolInfo.canGrowPoolSize;
for(int index = 0; index < currentPoolInfo.poolSize; index++) {
//instantiate
GameObject go = Instantiate(currentPoolInfo.prefab);
PoolObject poolObject = go.GetComponent<PoolObject>();
if(poolObject == null) {
Debug.LogError("Prefab must have PoolObject script attached!: " + currentPoolInfo.poolName);
} else {
//set state
poolObject.ReturnToPool();
//add to pool
pool.Add(poolObject);
}
}
Debug.Log("Adding pool for: " + currentPoolInfo.poolName);
poolDictionary[currentPoolInfo.poolName] = pool;
}
}
public GameObject GetObjectFromPool ( string poolName ){
PoolObject poolObject;
if(poolDictionary.ContainsKey(poolName)) {
Pool pool = poolDictionary[poolName];
//get the available object
for (int index = 0; index < pool.Count(); index++) {
PoolObject currentObject = pool.ObjectAt(index);
if(currentObject.IsAvailableForReuse()) {
//found an available object in pool
poolObject = currentObject;
break;
}
}
if(poolObject == null) {
if(pool.canGrowPoolSize) {
Debug.Log("Increasing pool size by 1: " + poolName);
foreach (int currentPoolInfo in poolInfo) {
if(poolName == currentPoolInfo.poolName) {
GameObject go = Instantiate(currentPoolInfo.prefab);
poolObject = go.GetComponent<PoolObject>();
//set state
poolObject.ReturnToPool();
//add to pool
pool.Add(poolObject);
break;
}
}
} else {
Debug.LogWarning("No object available in pool. Consider setting canGrowPoolSize to true.: " + poolName);
}
}
} else {
Debug.LogError("Invalid pool name specified: " + poolName);
}
return poolObject;
}
}
Would be helpful if you posted the errors and in which lines they occurred in the code you posted 'here' (not the original - the line numbering will differ) so we can quickly identify the problem ins$$anonymous$$d of having us to analyze/compare the two snippets and figure things out for your.
Well, I'm showing 12 different errors so that would be quite a bit of information.
Lines 62 and 63 and 67- The name 'poolName' does not exist in the current context
Line 64 and 68- The name 'String' does not exist in the current context
Line 76 and 123- Cannot convert type 'EasyObjectPool.PoolInfo' to 'int'
Line 109- Cannot implicitly convert type 'UnityEngine.GameObject' to 'PoolObject'
Line 111- Type 'PoolObject' does not contain a definition for 'IsAvailableForReuse' and no extension method 'IsAvailableForReuse' of type 'PoolObject' could be found
Line 119 and 147- Use of unassigned local variable 'poolObject'
Line 147- Cannot implicitly convert type 'PoolObject' to 'UnityEngine.GameObject'
Basically, I've misunderstood some important things about my for loops
Answer by robertbu · Jun 19, 2014 at 04:03 AM
Here is a quick pass on the code. You did not provide the PoolObject class, so I had to do a bit of guessing. You'll need to compare this code with your original...there were a fair number of changes. Note I did not compare your translation to the original Javascript...I just went through and fixed compiler errors so that most if not all of the errors should be resolved.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EasyObjectPool : MonoBehaviour {
private class PoolInfo{
public string poolName;
public GameObject prefab;
public int poolSize;
public bool canGrowPoolSize = true;
}
private class Pool{
private List<PoolObject> list = new List<PoolObject>();
public bool canGrowPoolSize;
public void Add (PoolObject poolObject){
list.Add(poolObject);
}
public int Count (){
return list.Count;
}
public GameObject ObjectAt ( int index ){
PoolObject result;
if(index < list.Count) {
result = list[index];
}
return result;
}
}
static public EasyObjectPool instance ;
PoolInfo[] poolInfo;
private Dictionary<string, Pool> poolDictionary = new Dictionary<string, Pool>();
void Start () {
instance = this;
CheckForDuplicatePoolNames();
CreatePools();
}
private void CheckForDuplicatePoolNames() {
for (int index = 0; index < poolInfo.Length; index++) {
string poolName= poolInfo[index].poolName;
if(poolName.Length == 0) {
Debug.LogError(string.Format("Pool {0} does not have a name!",index));
}
for (int internalIndex = index + 1; internalIndex < poolInfo.Length; internalIndex++) {
if(poolName == poolInfo[internalIndex].poolName) {
Debug.LogError(string.Format("Pool {0} & {1} have the same name. Assign different names.", index, internalIndex));
}
}
}
}
private void CreatePools() {
foreach(PoolInfo currentPoolInfo in poolInfo){
Pool pool = new Pool();
pool.canGrowPoolSize = currentPoolInfo.canGrowPoolSize;
for(int index = 0; index < currentPoolInfo.poolSize; index++) {
//instantiate
GameObject go = Instantiate(currentPoolInfo.prefab) as GameObject;
PoolObject poolObject = go.GetComponent<PoolObject>();
if(poolObject == null) {
Debug.LogError("Prefab must have PoolObject script attached!: " + currentPoolInfo.poolName);
} else {
//set state
poolObject.ReturnToPool();
//add to pool
pool.Add(poolObject);
}
}
Debug.Log("Adding pool for: " + currentPoolInfo.poolName);
poolDictionary[currentPoolInfo.poolName] = pool;
}
}
public GameObject GetObjectFromPool ( string poolName ){
PoolObject poolObject = null;
if(poolDictionary.ContainsKey(poolName)) {
Pool pool = poolDictionary[poolName];
//get the available object
for (int index = 0; index < pool.Count(); index++) {
PoolObject currentObject = pool.ObjectAt(index);
if(currentObject.AvailableForReuse()) {
//found an available object in pool
poolObject = currentObject;
break;
}
}
if(poolObject == null) {
if(pool.canGrowPoolSize) {
Debug.Log("Increasing pool size by 1: " + poolName);
foreach (PoolInfo currentPoolInfo in poolInfo) {
if(poolName == currentPoolInfo.poolName) {
GameObject go = Instantiate(currentPoolInfo.prefab) as GameObject;
poolObject = go.GetComponent<PoolObject>();
//set state
poolObject.ReturnToPool();
//add to pool
pool.Add(poolObject);
break;
}
}
} else {
Debug.LogWarning("No object available in pool. Consider setting canGrowPoolSize to true.: " + poolName);
}
}
} else {
Debug.LogError("Invalid pool name specified: " + poolName);
}
return poolObject;
}
}
This is beautiful, thank you. I knew I was doing the for loops wrong.
I am getting two errors, though
Line 45- The type or namespace name 'Dictionary`2' could not be found. Are you missing a using directive or an assembly reference?
Line 18- The type or namespace name 'List`1' could not be found. Are you missing a using directive or an assembly reference?
PoolObject is the name of the second script that works with this one, which I didn't have any problems converting. Are these two errors co$$anonymous$$g from this script not being able to access the other one?
You sure you have a using System.Collections.Generic;
at the top?
Are these two errors co$$anonymous$$g from this script not being able to access the other one?
I wouldn't think so. These two lines look fine. If you will post the PoolObject class, I'll see if I have any trouble.
Actually, I had forgot to reinsert System.Collections.Generic but strangely it added some more errors to the script.
Here is the PoolObject script as I converted it to C#. It doesn't give any errors but like I said earlier, I known virutally nothing of JS syntax so not sure if I got it right (really just that last line I'm kind of unsure about):
public class PoolObject : $$anonymous$$onoBehaviour {
[HideInInspector]
public bool availableForReuse = true;
void Activate () {
availableForReuse = false;
gameObject.SetActive(true);
}
void ReturnToPool () {
gameObject.SetActive(false);
availableForReuse = true;
}
public bool AvailableForReuse () {
//true when isAvailableForReuse & inactive in hierarchy
return availableForReuse && (gameObject.activeInHierarchy == false);
}
}
Don't see anything wrong with the last snippet. What errors you're getting?