SOLVED: Can integers be used in defining sprites (as in view, loop, frame)?

Started by johanvepa, Fri 03/01/2014 00:53:46

Previous topic - Next topic

johanvepa

Can I tell AGS to use the function char.setgraphic(view, loop, frame) and collect the values from a global integer?

Ego is doing an animation, and for reasons too long to explain for the purpose here, I'd like this animation done with char.LockViewFrame and an integer counting down to 0 in rep_exec (every time timer is 0, set next graphic and reset timer).

I have integers corresponding to the values of which loop and frame ags must use. My integer "characterloop" will - due to other functions in the script which I will omit here - be set to the value 2 at the same time that char.LockViewFrame must use loop 2 of the current view. When my integer "characterframe" has the value 4, char.LockViewFrame must use frame 4 of the current loop of the current view.

As you can see, this gets rather long-winded (in room's rep_exec, with a default character view of lets say view no. 32):

Code: ags

if (timer > 0)
  (
  timer -= 1;
  )
else
  (
  if (characterloop == 1)
    (
    if (characterframe == 1)
      (
      ego.LockViewFrame (32, 1, 1);
      )
    else if (characterframe == 2)
      (
      ego.LockViewFrame (32, 1, 2);
      )
    else if (characterframe == 3)
      (
      ego.LockViewFrame (32, 1, 3);
      )
    )

  else if (characterloop == 2)
    (
    if (characterframe == 1)
      (
      ego.LockViewFrame (32, 2, 1);
      )
    else if (characterframe == 2)
      (
      ego.LockViewFrame (32, 2, 2);
      )
    else if (characterframe == 3)
      (
      ego.LockViewFrame (32, 2, 3);
      )
    )
  timer = 10;
  )




What I would like is to have the function char.LockViewFrame look for its values in the integers, leaving me to code something like:


Code: ags

if (timer > 0)
  (
  timer -= 1;
  )
else
  (
  ego.LockViewFrame (32, characterloop, characterframe);
  timer = 10;
  )



Can this be done?


monkey0506

Quote from: Nanuaraq on Fri 03/01/2014 00:53:46Can I tell AGS to use the function char.setgraphic(view, loop, frame) and collect the values from a global integer?

There's no Character.SetGraphic function because Character's don't really have a "graphic" property of any sort. They simply display the appropriate slot from their assigned view. If you want to get the sprite slot ID for the current frame, you can do this:

Code: ags
ViewFrame *frame = Game.GetViewFrame(player.View, player.Loop, player.Frame);
if (frame == null) AbortGame("Something highly impossible happened.");
int sprite = frame.Graphic;


Quote from: Nanuaraq on Fri 03/01/2014 00:53:46Ego is doing an animation, and for reasons too long to explain for the purpose here, I'd like this animation done with char.setgraphic and an integer counting down to 0 in rep_exec (every time timer is 0, set next graphic and reset timer).

The Character.Frame property can be directly manipulated, so you could just increment that until it reaches the value of Game.GetFrameCountForLoop(view, loop).

Quote from: Nanuaraq on Fri 03/01/2014 00:53:46
Code: ags

if (timer > 0)
  (
  timer -= 1;
  )
else
  (
  if (characterloop == 1)
    (
    if (characterframe == 1)
      (
      ego.setgraphic (32, 1, 1);
      )
    else if (characterframe == 2)
      (
      ego.setgraphic (32, 1, 2);
      )
    else if (characterframe == 3)
      (
      ego.setgraphic (32, 1, 3);
      )
    )

  else if (characterloop == 2)
    (
    if (characterframe == 2)
      (
      ego.setgraphic (32, 2, 1);
      )
    else if (characterframe == 2)
      (
      ego.setgraphic (32, 2, 2);
      )
    else if (characterframe == 3)
      (
      ego.setgraphic (32, 2, 3);
      )
    )
  timer = 10;
  )


You absolutely must never write code like this again. I don't care why, I don't care what it's for. Just don't do it man.

Quote from: Nanuaraq on Fri 03/01/2014 00:53:46What I would like is to have the function char.setgraphic look for its values in the integers, leaving me to code something like:


Code: ags

if (timer > 0)
  (
  timer -= 1;
  )
else
  (
  ego.setgraphic (32, int characterloop, int characterframe);
  timer = 10;
  )



Can this be done?

You could easily add a custom extender method to allow setting the view, loop, and frame properties at once. Given the nature of your question, it might be better to first sort out simpler matters before adding that confusion into the mix.

Secret Fawful

Monkey, start telling people why they shouldn't write code a certain way, or help them understand their mistakes, instead of just telling them what to do. Otherwise you're unhelpful and rude and you're just tearing people down.

monkey0506

Without being unhelpful or rude, anyone who writes code like:

Code: ags
if (a == 1) func(1);
else if (a == 2) func(2);
else if (a == 3) func(3);
else if (a == 4) func(4);
else if (a == 5) func(5);
else if (a == 6) func(6);
else if (a == 7) func(7);
else if (a == 8) func(8);
else if (a == 9) func(9);
else if (a == 10) func(10);


Is simply not utilizing a functioning brain*. Elementary levels of logic are simultaneously being employed and swept under the rug in this code snippet, and that's not the least of the problems here.

*To be fair, my brain malfunctions all the time. Half the code I publish doesn't even work, even theoretically.

Khris

Nanuaraq:

Regardless of the circumstances, of course you can use integer variables instead of actual integers at any time. That's basically why variables were "invented" in the first place.

So instead of
Code: ags
  if (a == 1) doSomething(1);
  else if (a == 2) doSomething(2);
  else if (a == 3) doSomething(3);

all you do is
Code: ags
  doSomething(a);

You can also do this:
Code: ags
  doSomething(a * 4 + 7);


Don't take monkey's reaction personal, it *is* a really basic thing. We're just wondering why you'd think this *wasn't* possible.

(And as far as malfunctioning brains are concerned, let's not forgot who actually is a Mormon here.)

Gilbert

Monkey, even if your brain doesn't work all the time there's no excuse in being rude to beginners, especially in a... beginners' forum. Things considered to be elementary logic to you may not apply to all people, especially for those who are not familiar with programming, like me.

You have been warned.

johanvepa

Thank you very much for your reply, Khris. I apologize right off the bat for not testing my code before asking the forum. I usually take a look at your signature "what have you tried" before asking anything, and this time, I didn't even try my own coding  :D

The code above was written at 2 AM with a headache and five hours before having to wake up and be a father to my child and the thing with the global integers had been buzzing in my skull for days because I never have time to sit down and try stuff. I've also edited some of the more grave mistakes above, like typing setgraphic instead of LockViewFrame.

Anyway, what I needed to know was exactly what I would've known, had I tried. That, yes, the function LockViewFrame DOES work with a global integer. I don't have to type what number of frame I want it to show, I can simply ask it to get the value from a global integer. Yes. Simple. I like simple.

I guess I didn't think it possible because I've become used to the idea of typing in specific values in the functions of AGS and using global integers for purposes usually other than function values. I dunno. I use global integers for displaying numbers in the Display function, so I should have known. Guess I wasn't thinking clearly.

Anyway, having tried out a few ups and downs with my newfound solution, I've cleaned up my code considerably and am satisfied with the results for now. Thank you again.

SMF spam blocked by CleanTalk