#(TOTAL C# NEWBIE) struggling to translate gdscript code into c#

1 messages · Page 1 of 1 (latest)

cunning crag
#

(was unsure of where to post a c# question, so i came here)

ive never learned c#, but im trying to learn a little by converting some gdscript code i made into c#, and im running into a weird issue that i just do not understand. the output of the gdscript code (first image) is correct, but the c# output (second image) continuously shows the wrong values
the size of the blocks array is the same (64) in both scripts

(apologies for the probably very messy c#. like i said before, this is my first time trying to use it)

gscript code:

func _ready() -> void:
    blocks.resize(Global.chunk_size ** 3) 
    print(get_block_pos(2))

func get_block_pos(index : int) -> Vector3:
    var chunksize_minus1 : int = round(pow(blocks.size(), 1.0/3.0) - 1.0)
    var chunksize_bitwize_y : int = roundi(log(chunksize_minus1) / log(2.0))
    var chunksize_bitwise_z : int = roundi(chunksize_bitwize_y * 2.0)
    
    var blockpos : Vector3 = Vector3(index & chunksize_minus1, (index >> chunksize_bitwize_y) & chunksize_minus1, (index >> chunksize_bitwise_z) & chunksize_minus1)
    return blockpos

c# code:

public override void _Ready()
{
    blocks.Resize((int)Math.Pow(chunk_size, 3));
    GD.Print(get_block_pos(2));
}

public Vector3 get_block_pos(int index)
{
    Vector3 blockpos = new Vector3(0, 0, 0);

    int chunksize_minus1 = (int)Mathf.Pow(blocks.Count, 1.0f / 3.0f);
    int chunksize_bitwise_y = (int)(Math.Log(chunksize_minus1) / Math.Log(2));
    int chunksize_bitwise_z = chunksize_bitwise_y * 2;

    blockpos.X = index & chunksize_minus1;
    blockpos.Y = (index >> chunksize_bitwise_y) & chunksize_minus1;
    blockpos.Z = (index >> chunksize_bitwise_z) & chunksize_minus1;

    return blockpos;
}
#

ok, i just realized i never subtracted 1 from chunksize_minus1 in the c# code, but its still outputting the wrong value

unreal glacier
#

you're not rounding chunksize_bitwise_z

#

in fact you're not rounding any of them

#

converting to int here is fishy, just use floats or doubles

cunning crag
#

floats for everything including index, or just the three ints created in the function?

#

if i only have the three new variables as floats, im not able to create the vector since index is an int

#

i added Math.Round to the first two, and it works now. thank you!

#
int chunksize_minus1 = (int)Math.Round(Mathf.Pow(blocks.Count, 1.0f/3.0f) - 1.0f);
int chunksize_bitwise_y = (int)Math.Round(Mathf.Log(chunksize_minus1) / Mathf.Log(2.0f));
int chunksize_bitwise_z = chunksize_bitwise_y * 2;
#

my only coding experience is with gdscript so far so im not used to the required specificity of other coding languages, so i thought just (int) would round it