Basic Convert C# to JS

Heya guys, I need help with converting these lines into JS because I got them from a source but I use JS in my project. Im not good at converting and also I heard that its not good to mix the two languages together in a project.

       public void FireWeapon(Vector3 orig, Vector3 dir) {
        if(weaponData==null) {
            weaponData = gameObject.GetComponentInChildren<WeaponData>();
            if(weaponData==null) {
                Debug.LogError("Did not find any WeaponData in our children!");
                return;
            }
        }
       
        if(cooldown > 0) {
            return;
        }
       
        Debug.Log ("Firing our gun!");
       
        Ray ray = new Ray(orig, dir);
        Transform hitTransform;
        Vector3   hitPoint;
       
        hitTransform = FindClosestHitObject(ray, out hitPoint);
       
        if(hitTransform != null) {
            Debug.Log ("We hit: " + hitTransform.name);
           
            // We could do a special effect at the hit location
            // DoRicochetEffectAt( hitPoint );
           
            Health h = hitTransform.GetComponent<Health>();
           
            while(h == null && hitTransform.parent) {
                hitTransform = hitTransform.parent;
                h = hitTransform.GetComponent<Health>();
            }
           
            // Once we reach here, hitTransform may not be the hitTransform we started with!
           
            if(h != null) {
                // This next line is the equivalent of calling:
                //                    h.TakeDamage( damage );
                // Except more "networky"
                PhotonView pv = h.GetComponent<PhotonView>();
                if(pv==null) {
                    Debug.LogError("Freak out!");
                }
                else {
                   
                    TeamMember tm = hitTransform.GetComponent<TeamMember>();
                    TeamMember myTm = this.GetComponent<TeamMember>();
                   
                    if(tm==null || tm.teamID==0 || myTm==null || myTm.teamID==0 || tm.teamID != myTm.teamID ) {
                        h.GetComponent<PhotonView>().RPC ("TakeDamage", PhotonTargets.AllBuffered, weaponData.damage);
                    }
                }
               
            }
           
            if(fxManager != null) {
               
                DoGunFX(hitPoint);
            }
        }
        else {
            // We didn't hit anything (except empty space), but let's do a visual FX anyway
            if(fxManager != null) {
                hitPoint = Camera.main.transform.position + (Camera.main.transform.forward*100f);
                DoGunFX(hitPoint);
            }
           
        }
       
        cooldown = weaponData.fireRate;
    }

    void DoGunFX(Vector3 hitPoint) {
        fxManager.GetComponent<PhotonView>().RPC ("SniperBulletFX", PhotonTargets.All, weaponData.transform.position, hitPoint);
    }

    Transform FindClosestHitObject(Ray ray, out Vector3 hitPoint) {
       
        RaycastHit[] hits = Physics.RaycastAll(ray);
       
        Transform closestHit = null;
        float distance = 0;
        hitPoint = Vector3.zero;
       
        foreach(RaycastHit hit in hits) {
            if(hit.transform != this.transform && ( closestHit==null || hit.distance < distance ) ) {
                // We have hit something that is:
                // a) not us
                // b) the first thing we hit (that is not us)
                // c) or, if not b, is at least closer than the previous closest thing
               
                closestHit = hit.transform;
                distance = hit.distance;
                hitPoint = hit.point;
            }
        }
       
        // closestHit is now either still null (i.e. we hit nothing) OR it contains the closest thing that is a valid thing to hit
       
        return closestHit;
       
    }

}

That’s a lot of code you are asking to convert, have you tried yourself?

Here’s the usual links I post :

http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?

Here’s resummarizing some of the information in the first link :

VARIABLES

// C#
TYPE label;

// uJS
var label : TYPE;

FUNCTIONS WITH PARAMETERS

// C#
public void DoStuff( TYPE label ) {
}

// uJS
public function DoStuff( label : TYPE ) {
}

FUNCTIONS WITH RETURN VALUES

// C#
public TYPE DoStuff() {
   TYPE label = someValue;
   return label;
}

// uJS
public function DoStuff() : TYPE {
   var label : TYPE = someValue;
   return label;
}

GETCOMPONENT

// C#
TYPE label = GetComponent< TYPE >();

// uJS
var label : TYPE = GetComponent.< TYPE >();

Try converting with this information, see how you go. If you get stuck, post reply with what you have converted :slight_smile:

1 Like

@AlucardJay ok heres the converted script I made using the guide you’ve given me but I only narrowed it down to 6 errors
this is what I have

    function FireWeapon(orig : Vector3, dir : Vector3) {
        if(weaponData==null) {
            var weaponData = gameObject.GetComponentInChildren(WeaponData);
            if(weaponData==null) {
                Debug.LogError("Did not find any WeaponData in our children!");
                return;
            }
        }
       
        if(cooldown > 0) {
            return;
        }
       
        Debug.Log ("Firing our gun!");
       
        var ray = Ray(orig, dir);
        var hitTransform : Transform;
        var hitPoint : Vector3;
       
        var hitTransform = FindClosestHitObject(ray, out hitPoint);
       
        if(hitTransform != null) {
            Debug.Log ("We hit: " + hitTransform.name);
           
            // We could do a special effect at the hit location
            // DoRicochetEffectAt( hitPoint );
           
            var h = hitTransform.GetComponent(Health);
           
            while(h == null && hitTransform.parent) {
                var hitTransform = hitTransform.parent;
                var h = hitTransform.GetComponent(Health);
            }
           
            // Once we reach here, hitTransform may not be the hitTransform we started with!
           
            if(h != null) {
                // This next line is the equivalent of calling:
                //                    h.TakeDamage( damage );
                // Except more "networky"
                var pv = h.GetComponent(PhotonView);
                if(pv==null) {
                    Debug.LogError("Freak out!");
                }
                else {
                   
                    var tm = hitTransform.GetComponent(TeamMember);
                    var myTm = this.GetComponent(TeamMember);
                   
                    if(tm==null || tm.teamID==0 || myTm==null || myTm.teamID==0 || tm.teamID != myTm.teamID ) {
                        h.GetComponent(PhotonView).RPC ("TakeDamage", PhotonTargets.AllBuffered, weaponData.damage);
                    }
                }
               
            }
           
            if(fxManager != null) {
               
                DoGunFX(hitPoint);
            }
        }
        else {
            // We didn't hit anything (except empty space), but let's do a visual FX anyway
            if(fxManager != null) {
                hitPoint = Camera.main.transform.position + (Camera.main.transform.forward*100f);
                DoGunFX(hitPoint);
            }
           
        }
       
        cooldown = weaponData.fireRate;
    }

    function DoGunFX(hitPoint : Vector3) {
        fxManager.GetComponent(PhotonView).RPC ("SniperBulletFX", PhotonTargets.All, weaponData.transform.position, hitPoint);
    }

    function FindClosestHitObject(ray : Ray, out hitPoint : Vector3) : Transform {
       
        var hits = Physics.RaycastAll(ray);
       
        var closestHit = null;
        var distance : float = 0;
        var hitPoint = Vector3.zero;
       
        foreach(hit : RaycastHit in hits) {
            if(hit.transform != this.transform && ( closestHit==null || hit.distance < distance ) ) {
                // We have hit something that is:
                // a) not us
                // b) the first thing we hit (that is not us)
                // c) or, if not b, is at least closer than the previous closest thing
               
                var closestHit = hit.transform;
                var distance = hit.distance;
                var hitPoint = hit.point;
            }
        }
       
        // closestHit is now either still null (i.e. we hit nothing) OR it contains the closest thing that is a valid thing to hit
       
        return closestHit;
       
    }

2158405–142615–NetworkCharacter.js (4.69 KB)

error: line 20: expecting ), found ‘hitPoint’
error: line 20: Unexpected token: ).
error: line 78: Unexpected token: hitPoint.
error: line 86: Unexpected token: hit.
error: line 86: ‘;’ expected. Insert a semicolon at the end.
error: line 86: Unexpected token: hit.
just fixed 6th error , Just added closed bracket

great job, you did really well.

Don’t forget to typecast all variables. While you can implicitly typecast in uJS, it’s good to know what type of variable you’re dealing with, shows understanding what the code is doing, and helpful when coding in other languages. eg :

var weaponData : WeaponData = ....
var ray : Ray = ....
var hitTransform : Transform = ....
var h : Health = ....
var pv : PhotonView = ....
var tm : TeamMember = ....
var myTm : TeamMember = ....

Raycasts in uJS don’t use the keyword out :

var hitTransform : Transform = FindClosestHitObject(ray, hitPoint);
function FindClosestHitObject(ray : Ray, hitPoint : Vector3) : Transform {

in the while loop, the variables hitTransform and h have already been declared, so you don’t declare them again with var, you are assigning a value to them :

while(h == null && hitTransform.parent) {
    hitTransform = hitTransform.parent;
    h = hitTransform.GetComponent(Health);
}

same thing in your foreach loop, the variables closestHit distance and hitPoint have already been declared.

in uJS a foreach loop is just for with the variable declared as normal :

for ( var hit : RaycastHit in hits) {

Wow ok, thank you so much !! I really needed that! Thank you for taking the time out to help me! Now I have a better understanding of converting C# to JS, or otherwise, However, adding those things just made some errors so I decided to delete it all and try to find a way to make damage happen. Thanks