IOS scripting advice

hi

i am currently making a 2D side scroller. I want to try to keep the speed up so i just wanted some general advice for game scripting and management. I am also using SM2 to keep draw calls as low as poss.

My plan is as follows.

function fixed update
{a basic script to update object positions relative to the screen scrolling
+collision detection}

function update{
player input

a manager script that uses tags to make objects active/inactive/enemy/mutant/super mutant

delta time test is game running at optimum speed ?
yes invoke AI script

no skip AI }

function AI{ a selection of small scripts to test for coniditons
AI would iterate through objects to find tags and then test for proximity etc}

Does the above sound OK? Any advice?

2 more question? is iterating through objects testing tags efficient or inefficient? i have seen a post about using a game manager here script i guess this would be highly efficient? I am trying to avoid instantiate and destroy by using tags and to assign and manage game behviour such as being active or attacking on that basis.

Also is all invoked code completed before the frame update? Sorry i know this last question is stupid:sweat_smile:! but i am wondering if to use yeild to pause AI scripts so it doesnt slow up the game too much.

thnx

Your plan seems OK as far as it goes but there isn’t really much detail to give advice about.

Comparing tags isn’t as inefficient as people seem to think but using functions like FindGameObjectsWithTag each frame is definitely a slow way of doing things. It is often better to use arrays to keep track of different types of objects (for example, you might have one array for enemies, another for shots, etc).

brilliant thanks for that. thats exactly the kind of information i needed.

I am trying to be logical and organised about my scripting as i already have a bunch of scripts doing most of the game functions but they are messy and i really wanted to take a step back and try and organise it a bit better.

I currently use arrays to manage items but i thought using tags would allow me to manage changes to game objects better by changing their tags.

I am under the impression that moving items between arrays is also slow eg adding to and subtracting from arrays.

I am trying to get a good plan in place before proceeding hence the very basic description at the moment.

thanks your advice is very much appreciated.

actually i can see .NET arrays could offer me the speed i am looking for and the only real limitation is not being able to resize. That sounds like a pretty good deal to me.

i have actually been using .NET arrays in my messy code already but didnt realise that they offered such a performance benefit.

Maybe it’s just me, but I’ve never noticed instantiate and destroy slowing my game down at all, and i use those two functions a lot.

Here are a few tips if this is your first iphone game:

  1. GameObject.FindWithTag shouldnt be used ever frame. If you need to access a variable from a certain object ever frame, this is what i do:
var theScriptWeWantAVariableFrom : TheNameOfThatScript;

function Start ()
{
      theScriptWeWantAVariableFrom = GameObject.FindWithTag("TheTagYouWant").GetComponent(TheNameOfThatScript) as TheNameOfThatScript;
}

function Update()
{
     //now you can access any variable from that script by typing theScriptWeWantAVariableFrom.theVariableWeWant
}

and if you need to access a variable from another object or script that doesn’t ever change, save it once as it’s own variable so that your script doesn’t need to access another script for it every frame.

I hope that made sense to you. It took me a while to get it my first time around.

  1. Static variable typing. Basically when you are declaring variable, make sure you set it to a specific type, like float or int or Vector3.
var health; //dont do this
var health : float; //do this
  1. Raycast with layers. If you are doing any raycasting, instead of raycasting everything in the scene, only raycast on a layer that has stuff that you need. AKA Have enemy bullets only raycast against the player collider. Then maybe do a raycast every 20 or so frames to see if the enemy has a clear shot at the player and knows when to shoot and when not to.

  2. if you’re building for the 4th gen iphone and itouch, set the screen resolution to low in the player settings. this makes it display at 320 * 480 instead of 640 * 960. Sacrificing the full resolution of the new retina display with speed your game way way up.

  3. then there is obvious stuff like use small texture sizes (512 or lower is what i go with), keep the number of objects in your scene low as possible, use low-poly meshes, try to keep the vert count low (like under 2k for best performance).

  4. one last thing, i dont know if it makes a huge difference, but for any script that deals with the transform attached to the object of that script, make a “thisTransform” variable. I guess unity still has to “think” to find the transform of an object when you just type “transform”. By saving a “thisTransform” variable, you save the path to that transform.

var thisTransform : Transform;

function Start ()
{
     thisTransform = transform;
}

function Update ()
{
     //move the object forward at 6 units a second
     thisTransform.position += Vector3(0,0,6 * Time.deltaTime);
}

and here’s a video i found helpful. It’s not specifically for iOS development, but it’s still has some good information:
http://unity3d.com/support/resources/unite-presentations/performance-optimization

Some optimization tips:

  • Use performant shaders - very important.
  • Cache things like .transform, .gameObject, etc in variables
  • Avoid allocating memory at runtime. String manipulations for instance can hurt your performance.
  • Avoid creating new objects. Use pooling or just create everything you need at startup and disable it.

Good luck!

wow thanks guys that is great. i am trying to be really efficient with the scripting cos i am trying to make a kind of defender clone so i want to hit good speeds and no lagging.

i bought the SM2 package specifically to help me achieve best performance. I think concentrating on native arrays for object management and then AI functions with yields.

I am gonna try getting the script to test the performance using deltatime (i think) and if it is lagging to skip AI until performance picks up. As it is a clone of an 8bit game the AI is pretty basic, just proximity, fire and chase for the boss.

what i really want to give the player is a feeling of seamless scrolling and movement. otherwise the game will be a turkey. if i cant manage that i wont go further with the project. At the moment the demo i have on iphone is pretty smooth but the scripting is very messy and the enemies do not shoot back yet.

i’ll let you know if i discover anything worth knowing.

sorry for this dumb question, but which are the performant shaders?

::: AIMED AT eteeski :::: You Sir, are fantastic. I want to gush. Your variable/object naming is the best I’ve seen. Please force all others to follow in your steps. It’s the first time this crusty new born in the world of programming has seen the light. THANKYOU!

i must admit i was wondering about performant shaders. there are no shaders available on IOS?!? right so i guess this means the shaders available on the other build stuff like the alpha blended shader which really delivers a performance boost in comparison to specular etc.

the link as well is very useful i am now trying to construct the entire code base using native array iterations if it doesnt perform well on iphone i will do a web based version plus and ipad.

i am testing an ipad and do notice better performance but i dont know if that is just due to arm7 or if ipad offers other performance enhancements. i have to say deving for these devices is great fun and the potential for the future is exciting.

I think multiple users on the router configuration mode may be Tcl at the same time without interference, because each shell session starts a separate server process Tcl Tcl interpreter. TTY user interface is intended to serve each process is represented in Tcl server name, and may look like the CLI command show method.

One of the benefits of built in arrays, is that even though the memory is all allocated, you don’t necessarily have to cycle through the entire array. For example, after 50 enemies die (of say, 200), you can quickly run through all 200 to see what the last ‘live’ array member is, and adjust your main for-loop accordingly.

While this could result in inconsistant performance (versus stable performance just keeping the main loop the same length regardless of array-state), used intelligently it could allow you to optimise for various enemy types, etc etc.

oh i get it the .net arrays are fast because they are being held in memory constantly! it didnt occur to me that was why but it makes sense.

with Csharp you explicitly have to alloc them dont you?? i guess js is doing it for you.