[solved] get current graphic from player

Started by eri0o, Fri 13/04/2018 00:37:32

Previous topic - Next topic

eri0o

Hello,

I need to get the current graphic from the player, I need to do this every instant, I am using this information to draw things on a surface, so at instant t=7 I need the player frame for instant t=7, and not t=6.

I decided to use the following:

Code: ags

int getPlayerGraphic(){
  ViewFrame * ViewFrame_Player;
  ViewFrame_Player = Game.GetViewFrame(player.View,  player.Loop, player.Frame);
  return  ViewFrame_Player.Graphic;
}


but apparently, this gets in the current instant, the graphic that was on screen on the previous instant, so using this information gets a slightly delay.

So I evolved to do the following thing:

Code: ags

int i_view;
int i_loop;
ViewFrame * ViewFrame_Player;

function repeatedly_execute_always(){
  i_view = player.View;
  i_loop = player.Loop;
}

function late_repeatedly_execute_always(){
  if(IsGamePaused() == 1  || !System.HasInputFocus){
    return;  
  }
  
  ViewFrame_Player = Game.GetViewFrame(i_view,  i_loop, player.Frame);
}

static int PlayerViewframe::getGraphic(){
  return  ViewFrame_Player.Graphic;
}


which sometimes works, but in some rare occasions, crashes my software, because player.Frame sometimes is updated and i_view and i_loop isn't, and my loop for down and up have different frame count than left and right. And sometimes, it doesn't update correctly.

So is there a right way of getting the current graphic the player has ?

Crimson Wizard

#1
Quote from: eri0o on Fri 13/04/2018 00:37:32
So is there a right way of getting the current graphic the player has ?

Maybe this is nitpicking, but I think the question is wrong.
Your very first code example IS the right way of getting the current graphic.
The issue is that you are using that information when it is no longer relevant.

The game loop in AGS goes as this:
- repeatedly_execute
- repeatedly_execute_always (or vice versa, idk)
- game updates its state (animations etc)
- late_repeatedly_execute_always
- rendering

So, we may suppose that you'd need to both query the view, loop, frame and graphic AND set your debug "thing" to draw itself according to that graphic - all in the late_repeatedly_execute_always.

eri0o

CW you are right! I didn't knew I could just use late_repeatedly_execute_always under a RoomN.asc, but apparently I can!!! I also now don't understand the difference between Room_RepExe and repeatedly_execute.

Crimson Wizard

#3
Do you mean it got solved? I did not test this myself.

Quote from: eri0o on Fri 13/04/2018 02:43:38I also now don't understand the difference between Room_RepExe and repeatedly_execute.

They are the same thing. One of the AGS design quirks, repeatedly_execute is automatically called in all script modules except rooms, and in rooms you must connect to the event system. But _always functions are called like in normal modules.
And room function does not have to be called Room_RepExec, you may also call it repeatedly_execute, if you register it in the events pane with correct name.

eri0o

#4
Yes, it got solved! I think each .asc listed under script (not rooms) repeatedly_execute, are called from top to bottom and me not paying attention lead to ultimate confusion!

Crimson Wizard

#5
Quote from: eri0o on Sat 14/04/2018 14:16:56
Yes, it got solved! I think each .asc listed under script (not rooms) repeatedly_execute, are called from top to bottom and me not paying attention lead to ultimate confusion!

Yes ofcourse, all callbacks in modules are called from top to bottom in AGS (in the order of module dependency), this is also how ClaimEvent function works: it stops event propagation in certain module, allowing you do a trick when particular script disables all others for a while.

SMF spam blocked by CleanTalk