Simplifying object scripting

Started by LeChuck, Thu 11/09/2008 00:14:23

Previous topic - Next topic

LeChuck

Here's the thing; I've run out of objects in this room. No problem, it can be solved this way:

Code: ags


if (logtaken == 1) { 
     oLog1.SetPosition(167, 132); 
     oLog1.SetView(493,1); 
     oLog1.Animate(1,2,eRepeat,eNoBlock,eForwards); 
     oLog1.Visible = true;
}
else if (logtaken == 2) { 
     oLog2.SetPosition(167, 132); 
     oLog2.SetView(493,1);
     oLog2.Animate(1,2,eRepeat,eNoBlock,eForwards); 
     oLog2.Visible = true;
}



... and so on, with four total objects. Basically what this does is reuse an old object that the user has picked up previously, and reuse that object for this animation. Is there a way of making this simpler? It's basically the same thing over and over with different object names.

monkey0506

Well if oLog1, oLog2, oLog3, and oLog4 are the only objects then you could just do:

Code: ags
if (logtaken > 0) {
  object[logtaken - 1].SetPosition(167, 132);
  object[logtaken - 1].SetView(493, 1);
  object[logtaken - 1].Animate(1, 2, eRepeat, eNoBlock, eForwards);
  object[logtaken - 1].Visible = true;
}


Or if you do have other objects, but the 4 log objects are sequentially numbered:

Code: ags
if ((logtaken > 0) && (logtaken <= 4)) {
  object[oLog1.ID + (logtaken - 1)].SetPosition(167, 132);
  object[oLog1.ID + (logtaken - 1)].SetView(493, 1);
  object[oLog1.ID + (logtaken - 1)].Animate(1, 2, eRepeat, eNoBlock, eForwards);
  object[oLog1.ID + (logtaken - 1)].Visible = true;
}


At this point you might also be better off using a variable for those indices...

Code: ags
int objID = oLog1.ID + (logtaken - 1);

Khris

Or, even shorter:
Code: ags
  Object*o = object[...logtaken...];
  o.SetPosition(...)
  ...


Question though: Why are you animating the object, then setting it to visible afterwards?

LeChuck

All right you guys, it seems I've been so dependant on object based objects (...) lately that I've forgotten about the power of object[whatever] style scripting. Thanks!

Quote from: KhrisMUC on Thu 11/09/2008 09:28:28
Question though: Why are you animating the object, then setting it to visible afterwards?

I figured it was to make sure it is already animating once it becomes visible, so it wouldn't hesitate to animate if there's a slowdown. I do it all the time and it works fine. I'm sure it's not necessary, but why not? :P

monkey0506

Seeing as the animation is non-blocking his logic is valid...it would prevent the object being turned on before the animation starts...

And the object[whatever] method is object-based scripting...it's just using a global array of all the objects instead of the specific instance. ;)

Khris

Ah, right, didn't notice eNoBlock :)

SMF spam blocked by CleanTalk