- Home /
Cannot convert float to int when only using floats (C#)
I have a list called layouts that stores 2 dimensional arrays of floats, but whenever I try and access it in the format of layouts[int][float][float] each line gets the 2 identical errors:
Cannot implicitly convert type float' to
int'. An explicit conversion exists (are you missing a cast?)
This happens on line 53, 56, 78.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class floorbuilderv3 : MonoBehaviour {
//room related
public GameObject[] fourrooms = new GameObject[3];
public GameObject[] threerooms = new GameObject[3];
public GameObject[] tworooms = new GameObject[3];
public GameObject[] onerooms = new GameObject[3];
public GameObject[] startrooms = new GameObject[1];
//settings
public float room_distance = 10f;
//flooor related
int currentfloor = 0;
public int Size_Increase_Factor = 5;
List<float[,]> layouts = new List<float[,]>();
int roomquantity;
int branchnumb = 0;
int gridsize;
void Start () {
transform.position = new Vector3 (0, 0, 0);
buildnextfloor ();
}
void buildnextfloor(){
gridsize = (currentfloor+1) * 20;
roomquantity = ((currentfloor+1) + (currentfloor+1)) * Size_Increase_Factor;
float[,] floor = new float[gridsize,gridsize];
layouts.Add (floor);
Debug.Log ("gridsize: " + gridsize + ", room quantity: " + roomquantity);
//create unitialized, prioritized floor layout
creattile (-1, 1,new Vector2(Mathf.FloorToInt (gridsize/2), Mathf.FloorToInt (gridsize/2)));
//initialize the layout
initialize ();
}
/////////////////////////////////////////tile creation////////////////////////////////////
void creattile(int remaining, int branch, Vector2 location){
float temp = location.x;
if(remaining > 0)
return;
if(location.x > gridsize || location.x < 0 || location.y > gridsize || location.y < 0)
return;
if(layouts [currentfloor] [location.x, location.y] != 0f)
return;
layouts [currentfloor] [location.x, location.y] = branch;
Debug.Log ("placeholder created. grid location: " + location.x + ", " + location.y + ". branch number: " + branch);
if(remaining == -1){
int adjacents = Random.Range(2, 4);
for(int i = 0; i < adjacents; i++){
creattile((roomquantity/adjacents),2, expand(location, 0));
}
}
else if(remaining > 2){
int adjacents = Random.Range(1, 3);
for(int i = 0; i < adjacents; i++){
creattile((remaining/adjacents), branch++, expand(location, 0));
branchnumb = branch;
}
}
}
Vector2 expand(Vector2 location, int iteration){
iteration ++;
float x, y;
x = Mathf.Round (Random.Range ((location.x-1),(location.x+1)));
y = Mathf.Round (Random.Range ((location.y-1),(location.y+1)));
if(layouts [currentfloor] [location.x, location.y] != 0 && iteration < 10){
return expand(location, iteration);}
return (new Vector2(x, y));
}
//////////////////////////////////tile initialization/////////////////////////////////////
void initialize(){
int initializedrooms = 0;
for(int i = 0; i < branchnumb; i++){
for(int x = 0; x < gridsize; x++){
for(int y = 0; y < gridsize; y++){
if(initializedrooms <= roomquantity){
if(layouts [currentfloor] [x, y] == i){
placeroom(new Vector2(x, y));
}
}
}
}
}
}
void placeroom(Vector2 gridlocation){
Debug.Log ("placed room :)");
}
}
Answer by yashpal · Jan 29, 2015 at 05:35 AM
Just replace this line
if(layouts [currentfloor] [location.x, location.y] != 0f)
with this
if(layouts [currentfloor] [(int)location.x, (int)location.y] != 0f)
hope this helps you.
Answer by Naphier · Jan 29, 2015 at 10:42 AM
Vectors have float components. So layout.x and layout.y need to be converted to integers to be used as indices in an array/list. I think what you mean to do is:
layouts[currentFloor , 0] = location.x;
I'm not familiar with the syntax you're using on your list. http://www.dotnetperls.com/list might help a lot. If you're only using 2 dimensions I'd do:
List layouts = new List;
Vector2 location = new Vector2 (x,y);
layouts.Insert(currentFloor , location);
Hope that helps.
Answer by nareshkumaryakkala · Jan 29, 2015 at 10:37 AM
You have used Vector2 type for location. Vector2 values are float type.
So you have to convert location values to int.
Example line 43:
if((layouts [currentfloor]) [System.Convert.ToInt32(location.x), System.Convert.ToInt32(location.y)] != 0.0f)
My suggestion is create a simple point structure instead of vector2