increments to variables?

I’m trying to increment a variable and I don’t know how to do that in java scripting.

I’m used to something like this

myValue.gotHit${num} ++;

Where ${num} adds a variable to my variable.

This will allow me to greatly simplify my scripts where I am often checking for a different object’s state.

Shell scripting are we. :smile:

It looks like you want multiple gotHit variables with a different number added to each one. In that case an array would help a lot. Then you could do things like

myValue.gotHit[num]++;

EDIT:

Or are you trying to add num to gotHit? In that case you would want

myValue.gotHit += num;

Yes multiple gotHit values, I want to track anywhere between 1 and twenty bad guys in my game.

thanks.

I’m somewhat done with changing my logic so that it is one set of scripts that work for all baddies so that I can mass produce baddies and only have one script.

I seem to have bonked out on something though before I can test this. In my AI script, I need to set up a static var with the number of the baddie, which of course will change depending on the number that gets assigned in the interface when the baddie is assembled. So even if I create thirty baddies, I go through one at a time in the interface and set the baddieNum to that number of my bad guy.

I’m thinking something like this:

var baddieNum = 1; // just to initialize it

static var getHit[baddieNum] ;

The second line doesn’t work above, and I didn’t expect it to, but I’m not sure what else to try. It’s static because I need to access it from my BADDIE_ATTACK script for instance.

I’m feeling like this is close but I’m in a syntax zone that I’m not comfortable with.

http://unity3d.com/support/documentation/ScriptReference/Array.html

Try something like this (assuming it’s an array of integers)

static var getHit : int[] = new int[baddieNum];

Alternatively you can define it as

public var getHit : int[];

and you will be able to set the length of array and each element of the array from the inspector. You should be able to access it from other scripts too because it’s public.

It would appear I lied about accessing the public variable from other scripts. I forgot that Unity turns the script into a class, if you want to use it from other scripts it has to be static.

okay I’m not really clear on how to go about implementing this. Still searching for an answer.

I’m not sure that an array is what I’m looking for, as I’m only talking about numbers here.

baddie1
baddie2
baddie3

In other scripting language I’ve been able to write something like this:

var num = 1;
baddie$(num) //which gets parsed depending on what num is set to.

This seems like this should far more straight forward than trying to fit arrays into script. Also the (num) gets used in multiple situations.

In terms of AI if the characters is attacking or not, blah.isAttacking(num)

every time i insert the num variable it should be able to parse this. I know there is a simpler way to do this.

must be.

b

But arrays are the simple way of doing this. :slight_smile:

You have a large number of distinct instances of an object, arrays were made to handle that kind of stuff. I would start by defining a baddie class

class Baddie {
    public var gotHit : boolean;
    public var isAttacking : boolean;
    // etc
}

Now we fill the array up with baddies

var baddieArray : Baddie[] = new Baddie[num];

for(int i = 0; i < baddieArray.length; i++) {
    baddieArray[i] = new Baddie();
    // here we could initialize baddie properties 
}

Then you could access various properties of various baddies with a simple

baddieArray[num].someProperty

I’m sorry this looks very interesting, I don’t see this as a simple way of doing what I’m talking about.

Are you saying that Unity has no way of parsing a variable with another variable?

baddie${num)

that it cannot simply figure out the above num has already been set to 1 or 2 etc, and then look for the associated information on baddie1?

okay I succumb. I’ll take a closer look at making this happen after dinner.

and getting the kids to bed.

thanks.

okay I’m trying to wrap my head around arrays in Unity, here is where I’m not quite following below, what is baddieArray = new Baddie(); doing?
var baddieArray : Baddie = new Baddie[num];
for(int i = 0; i < baddieArray.length; i++) {
baddieArray = new Baddie();
// here we could initialize baddie properties
}
And then you say “here we can inititalize baddie properties” but I’m not sure how to use arrays in Unity, thus the not simple approach.
The array information in scripting manual is less than um… helpful. And I am not finding any tutorials that really shed light yet on setting up arrays.
I don’t really see how this would possibly be simpler than passing a number to a variable, but I’m willing to play along, I simply need to get some more information on how to properly build arrays with Unity if anyone is out there.
thanks.

The $ was used as part of being explicit in identifying another variable.

so if you have something like :

var num = 3;

do something to gethit${num}

It reads that as gethit3. This was part of proprietary software for Rhythm and Hues Studios which I wrote in for many years. I am pretty sure I’ve seen it used elsewhere, possibly c-shell and TCK/TK. I believe that Perl uses a variation on this.

This would be a simple way to attach a script to a character (Baddie if you will) then identify that character as a number, and control the various attributes of that character without building an array.

by parse I simply mean it would read it as gethit3 before executing.

I getcha. I don’t really see how that’s any different than .NET arrays other than in syntax. Here’s a bit of info on arrays; it’s in C#, though, but I can help you translate if you need it. (I started with JavaScript in Unity but moved to C# in large part due to the documentation.)

Is there something you see about arrays that does not exactly match up in functionality to what you already know?

var baddieArray : Baddie = new Baddie[num];

should have been

var baddieArray : Baddie[] = new Baddie[num];

My bad. :sweat_smile:

Adding to what Jessy said, the only difference is syntax. getHit${num} is the same as getHit[num]. Just remember that and it you should be ok.

Most languages don’t support getHit${num} because it is slow. They would have to interpret the language instead of compile the language. If Unity did this then your game would get around 2 fps.

I appreciate the help, and I’m trying again to do this but keep in mind arrays are not simple if you haven’t built an array with a particular piece of scripting language, unity is rather vague about implementing and using arrays so I’m anything but clear, but willing to do it if it is the ONLY way I’m going to move past this.

The problem being, I have a lot of information now but not seeing the connections and I don’t see it in the docs either, and yes I have read that single page on arrays one paragraph on Java arrays a hundred times.

Here is what I have thus far.

class Baddie {
public var getHit : int ;
public var isAttacking : boolean ;

}

var baddieArray : Baddie[ ] = new Baddie[num];

for(int i = 0; i < 22; i++)
{
baddieArray = new Baddie();
// here we could initialize baddie properties
}
In the area above this is where I should be defining the different properties? If so I’m unclear how the class baddie above is being tracked by the baddieArray being generated below. This all seems rather disjointed and unrelated to me with no clear path on how to finish up.
Can anyone point me to some solid information on how to create and access arrays in Unity???
please?

I really must disagree here. You apparently just haven’t found a good source of information yet. I actually don’t remember where I got my introduction, but I assume it was an old Unity tutorial.

Here’s the second thing that came up for googling “simple tutorial on .net arrays”. It looks great but it’s C#, not JavaScript. (I do suggest you use C# instead of JavaScript, because the learning material is a lot more abundant.) Either way, Unity’s JavaScript and C# are really similar anyway, in my opinion.
http://csharp.net-tutorials.com/basics/arrays/

Also, a couple tips. Please use the “Code” button to wrap up your code. It’s too hard to read when you leave it all unindented and unformatted like that. Java is not the same thing as JavaScript, and is not used in Unity.

Jessey,

I agree that I haven’t found a good source of array information, but from my perspective what I’m asking has nothing to do with really writing arrays, and I’ve done this many many times in other softwares. Since I haven’t seen how arrays should work, I have yet to be convinced this is the answer.

I have been looking though and found the docs you sent me about an hour ago, but haven’t had a chance to read them yet, so I can’t say if they are what I’m looking for or not at this point, hopefully so.

I will say though that I often feel that there are a bunch of Unity power users who answer with very complicated ways of doing things, and I am not convinced at this point that the answer (using Arrays) is what I am after and that people have actually grasped the simplicity of what I asked in my original post (possibly due to the posting as you pointed out).

It’s unclear to me because I have NOT seen the simplicity in building a Unity array yet, just mystery.

In short, (and I’m not saying this about you), I sometimes feel that there is a slight judging tone to the answers in the forum, an implication that if we who are asking the questions don’t know how to do something, or understand a short answer, than maybe we shouldn’t be doing it at all.

I appreciate the link and the pointers, I think I’ll skip changing out to C# because my logic is done, I’m simply looking for a more graceful way to handle multiple characters with one script efficiently and hope to wrap this up soon.

thanks