I probably missing something simple. Currently I am using C for some calculations, at this stage i am not using any android java code that will come later. I have experience using C for android apps but not unity.
My floats are returning really weird number like 1.03730-43 and after a few clicks it goes to 0
This is what i currently have.
jni.cpp
#include <jni.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <android/log.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
extern "C"
{
int xint(int a, int b)
{
return a * b; //works great
}
int add(float a, float b)
{
return a + b; //return (float)(a + b) also produces a weird number
}
}
I am compiling as using ndk-build.cmd. I did not set any 1.6 or 1.7 settings as i am not using java.
I am currently using JavaPluginSample from unity as a testbed.
C# code.
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System;
using UnityEngine.UI;
public class Freedom : MonoBehaviour {
[DllImport ("jni")]
private static extern float add(float x, float y);
[DllImport ("jni")]
private static extern int xint(int x, int y);
int test = 2;
float addme = 99.25f;
public Transform passedvalue;
public Transform passedvalue2;
// Use this for initialization
void Start () {
}
//linked to a Unity 4.6 button
public void youclikedme(){
addme = add (addme,addme);
passedvalue.GetComponent<Text>().text = addme + "";
test = xint (test,44); //works
passedvalue2.GetComponent<Text>().text = test + "";
}
// Update is called once per frame
void Update () {
}
}
That’s float drift. The “-43” on the end is shorthand for scientific notation “x 10 ^ -43”. So you’ve got a number which is effectively zero, but is not being stored as actual 0 for some reason.
Yeah i figured it out once i took a break… the mostly likely cause for the problem occurred to me and I checked and it was just the case.
float add(float a, float b)
Got so exhausted trying to figure out why jfloat and jint was return weird numbers. I think u dont use those unless you want to communicate with java.
To top that of all missing eclipse Ant build options, multiple ant sdk setup issues, jdk updates, missing system variables. When i was testing with a java build which i wont use for now.
Anyway I hope this might be useful for other people I think most of the C tutorials out there are too complex or incomplete (they dont have enough code to let you recompile the C files), or needs some annoying ant build to get it working.
Beyond that be nice if you could actually run these directly in the Unity Editor. I compiled 50 unity apks across many different projects to get something working.
I think this is a unity bug. The transform components have this problem too so I wouldn’t be surprised. Its been there since 4.6 if you have an older version of unity like 4.3 try the plug in there and I am willing to bet it works just fine.
Oh crap yeah, that is true >.< I usually have my scanner eyes checking lol. But anyways if you do happen to get a weird number with floats its unity being unity. I have had issues with floating points too only in 4.6 and higher.
The result, as OP even pointed out, was that their C-function was returning an int, but the C#-function was wrapped to return a float.
The C-function converted its float to an int before returning. So the raw binary data sent back was formatted as an int.
That 32-bit information then gets converted to float on the C# side. This data isn’t interpretted as int->float conversion. It thinks the int data IS a float. So it just reads the binary data as a float.
Unless I’m misinterpreting what you’re saying… what ‘strangeness’?
In editor scripts I build, the float field will display a number very close to zero but not zero. This happens regularly when the previous value was not zero and was changed using a handler. Try changing the float fields in 4.6 using the slider and you will see what I mean.