Where do I set initial views in room script?

Started by thiezar, Wed 19/10/2011 17:22:24

Previous topic - Next topic

thiezar

here's an axample of troubling code


function room_FirstLoad()
{
  myObject.SetView(1, 0);
}

function room_Load()
{
  if(flag==1){   //flag is a global variable
    myObject.Animate(0,5,eRepeat,eNoblock);
  }
  else{
    myObject.Animate(1,5,eRepeat,eNoblock);
  }
}

Ok, now since the room_Load function is executed BEFORE room_FirstLoad, it occours an error because myObject tries to animate without a view.
I don't want to set every time the view in the room_Load function...it will be unefficient.

Is there a better and more elegant method to solve this problem?

Khris

#1
Quote from: thiezar on Wed 19/10/2011 17:22:24I don't want to set every time the view in the room_Load function...it will be unefficient.
Uh, why not?
That's the solution to your problem right there:

Code: ags
function room_Load()
{
  myObject.SetView(1);
  if(flag==1) myObject.Animate(0,5,eRepeat,eNoblock);
  else myObject.Animate(1,5,eRepeat,eNoblock);
}


If you are really uncomfortable with doing it once every time the room is loaded, move the .SetView line to game_start().
Right, completely forgot about that.

pcj

Quote from: LeKhris on Wed 19/10/2011 23:07:49If you are really uncomfortable with doing it once every time the room is loaded, move the .SetView line to game_start().

I don't think you can do that with Objects since no room is loaded.
Space Quest: Vohaul Strikes Back is now available to download!

thiezar

Quote from: LeKhris on Wed 19/10/2011 23:07:49
Uh, why not?

Oh, c'mon! Do I really have to set a view every time a room is loaded? It's so unelegant and unnecessary :-(

Quote from: LeKhris on Wed 19/10/2011 23:07:49
If you are really uncomfortable with doing it once every time the room is loaded, move the .SetView line to game_start().
Right, completely forgot about that.

Well, that would be a good solution indeed. But unfortunetely, like pcj said, rooms aren't loaded yet in game_start.

I'm disappointed about this because i have this problem in all rooms and only a weak solution to solve it.
I think it would be better if room_FirstLoad occours before fade in. In this way it could be used like a "very starting script" for all initial settings related to the room.

monkey0506

Code: ags
function room_Load()
{
  if (Game.DoOnceOnly("roomX_FirstLoad"))
  {
    // do what you want here
  }
  // do normal stuff here
}


And don't give me some crap about checking a condition being inelegant or inefficient. If you don't want to use Game.DoOnceOnly create your own room variable.

geork

#5
Do you have this in EVERY room? if so, and if you don't like the unelegance of just using room_load(), you could:
Code: ags

 //Global Script
  int R = 1 //or starting room
  bool VisitedRoom[(Game.RoomCount + 1)] //it's probabely not actually Game.RoomCount, but I can't     
                                                                     //remember the real command :P remeber +1, as array counts 
                                                                     //starts with 0.
function assign(){
  int i = 1;
  while(i != (Game.RoomCount + 1)){
    VisitedRoom[i] = false;
    i ++;
  }
}
game_start(){
  assign(); //this is good practice  ;D
}
repeatedly_execute(){
  if(R != cEgo.Room){
     if(VisitedRoom[cEgo.Room] == false){
       myObject.SetView(1, 0);
       VisitedRoom[cEgo.room] = true;
      R = cEgo.Room;
     }
     if(flag==1){   //flag is a global variable
       myObject.Animate(0,5,eRepeat,eNoblock);
     }
     else{
       myObject.Animate(1,5,eRepeat,eNoblock);
     }
   }
}

 (UNTESTED) This will only assign the object view the first time the character enters the room, and AFTER THAT will ask it to animate. However, this solution is a bit clunky and uses up more resources...but in a small or middle sized game, this shouldn't matter too much...
 if you need to assign different views to the object in question depending on room, you can add another array at the top such as
Code: ags
int OViews[Game.RoomCount]; 
then in assign() specify which number for which object, and when setting the view
Code: ags
 myObject.SetView(OViews[cEgo.Room],0); 

 Probabely not helpful...but hey it's just an idea   ;)

EDIT oops, looks like monkey got there first...with a much better solution  ;D

monkey0506

I didn't mean to come across as rude as I might have, but seriously, "setting a view" is no more than setting a variable. In this case (objects) it has the overhead of one function call, but you wanted it set in a separate function anyway.

Setting variables is not inefficient...just so you know. :)

Calin Leafshade

on a separate note, FirstLoad running after Load is a little odd. Add it to the bug tracker, oh wait..

thiezar

Quote from: monkey_05_06 on Thu 20/10/2011 20:55:05
Setting variables is not inefficient...just so you know. :)

Ok, I know inefficiency occours in loops with exponential complexity, but it's still unnecessary to set a variable when there's no need.
Anyway I think I'll use your solution. It's much better than the alternative :-)

I'd like to thank geork for its appreciated reply anyway.

geork

Hahaha!  I was just having a bit of fun  8), i'm in debugging stage, so it's all a bit of a drag on the programming thing... glad to have, um, lightened the mood :D

SMF spam blocked by CleanTalk