Fading Characters Function

Started by Lazarus, Wed 23/11/2005 09:56:04

Previous topic - Next topic

Lazarus

I'm trying to create a function where I can fade out characters, but I just cannot get it too work. I have created a function for objects andÃ,  that works ok, but now I am stuck with this one so any help would be grateful thanks


int transparent;

function FadeCharacterOut (int charID) {
Ã,  while (transparent <= 100) {
Ã,  SetCharacterTransparency (charID, transparent);
Ã,  transparent += 1;
Ã,  Wait (1); }
}
*Currently using AGS Editor 2.70 (Build 2.70.601)*

Gilbert

You need, at least to reset the variable each time to call the function, otherwise it won't work if transparent was already changed to 100+ (by an earlier call or some other functions).
int transparent;

function FadeCharacterOut (int charID) {
Ã,  transparent=0;
Ã,  while (transparent <= 100) {
Ã,  SetCharacterTransparency (charID, transparent);
Ã,  transparent += 1;
Ã,  Wait (1); }
}

Lazarus

that seemed to do the trick it now works thank,

the problem I have now is that the character fades to quickly is there away to slows this down abit?

the function I use for objects which is similar fades gradually.

thanks
*Currently using AGS Editor 2.70 (Build 2.70.601)*

Gilbert

Try increasing the wait value so it waits longer after each update, like for example Wait(2).

Under default setting of 40FPS, 1 game loop is 1/40 seconds, so 100 loops is approximately 2.5 seconds, if the wait values you used in the object function and the character function are the same, the duration should be the same.

Lazarus

#4
ok that didn't work the character still faded away to quickly even increasing the wait upto 10, I just had to wait longer for the function to finish any other ideas?
thanks

*Note it seems to be a problem with the test character I was using, I imported the ROGER character to try in a test game, but when using another character I have he faded out slowly*
*Currently using AGS Editor 2.70 (Build 2.70.601)*

Gilbert

Roger uses 256 colour sprites, you need to import hi-colour+ images for the sprites for transparency to work properly, otherwise it can only be either visible (full opacity) or invisible (full transparency).

Lazarus

Hi I have another question at the moment I have this function
Code: ags
function FadeCharacterOut (int charID, int speed) {
Ã,  transparent=0;
Ã,  while (transparent <= 100) {
Ã,  SetCharacterTransparency (charID, transparent);
Ã,  transparent += 1;
Ã,  Wait (speed); }
} 


What I would like is some sort of speed function that can replace the "wait" command and is if possible non blocking, because if someone enters a speed value of 0, the "wait" command doesn't like it
*Currently using AGS Editor 2.70 (Build 2.70.601)*

Gilbert

If you need it non-blocking you need to tweak it using the repeatedly_execute(_always)() events.

In that case define on top of script:
int transparent, fadespeed, fadecounter, fading=0;

Rewrite the function:
function FadeCharacterOut (int charID, int speed) {
Ã,  if (fading) return 1; //aborts if fading is currently being executed
Ã,  transparent=0;
Ã,  fadespeed = speed;
Ã,  fadecounter=fadespeed;
Ã,  fading=1;
}


Then in "repeatedly_execute" ("_always" if you don't want it to be blocked by anything) add:
if (fading) {
Ã,  if (transparent < 100) {
Ã,  Ã,  fadecounter--;
Ã,  Ã,  if (fadecounter<0) {
Ã,  Ã,  Ã,  fadecounter=fadespeed;
Ã,  Ã,  Ã,  transparent += 1;
Ã,  Ã,  Ã,  SetCharacterTransparency (charID, transparent);
Ã,  Ã,  }
Ã,  } else fading=0;
} 


Lazarus

Just realised that I'm not using that function before, instead I'm using this one

Code: ags
function FadeObjectOut (int objectid,int value,int speed) {
		transparent=(object[objectid].Transparency);
	while (transparent < value) {
		transparent ++;	Ã,  
		SetObjectTransparency (objectid, transparent);
		Wait (speed); }
}


and I'm turning these functions in a module so would I need to export the code you mentioned in the repeatedly execute?
*Currently using AGS Editor 2.70 (Build 2.70.601)*

Gilbert

If it's in a module it's even easier, since each module can have its own "repeatedly_execute()" or "repeatedly_execute_always()" script.

Since you're making a module I can assume you can use the new OO coding practise as well.

You may just do this in the module:
Code: ags

int transparent, fadespeed, fadecounter, fadelimit, fading=0;
Object *fadeobj;

function FadeObjectOut (Object *objectpoint,int value,int speed) {
Ã,  if (fading) return 1;
Ã,  fadeobj = objectpoint;
Ã,  transparent = fadeobj.Transparency;
Ã,  fading=1;
Ã,  fadespeed = speed;
Ã,  fadecounter = speed;
Ã,  fadelimit = value;
}

function repeatedly_execute_always() {
Ã,  if (fading) {
Ã,  Ã,  if (transparent < fadelimit) {
Ã,  Ã,  Ã,  fadecounter--;
Ã,  Ã,  Ã,  if (fadecounter<0) {
Ã,  Ã,  Ã,  Ã,  fadecounter=fadespeed;
Ã,  Ã,  Ã,  Ã,  transparent ++;
Ã,  Ã,  Ã,  Ã,  fadeobj.Transparency = transparent;
Ã,  Ã,  }
Ã,  } else fading=0;
} 

function on_event (EventType event, int data) {
Ã,  if (event == eEventLeaveRoom && fading) {
Ã,  Ã,  fading = 0;Ã,  //reset, so won't crash in next room
Ã,  Ã,  fadeobj.Transparency = 0;
Ã,  }
}


Just put into the header:
import functionÃ,  FadeObjectOut (Object *objectpoint,int value,int speed);

to use it.

Lazarus

Thanks for the help I'll try that later

also would that effect my other function

Code: ags
function FadeObjectIn (int objectid,int value,int speed) {
		transparent=100;
		ObjectOn(objectid);
	while (transparent > value) {
		transparent --;		Ã,  
		SetObjectTransparency (objectid, transparent);
		Wait (speed); }
}
*Currently using AGS Editor 2.70 (Build 2.70.601)*

Ashen

Well, if you want that function to be non-blocking as well, and I imagine you would, you'll need to rewrite it to match Gilbot's version.

This also means you'll either need an other variable to use in place of fading or check for a different value (so you can differentiate between fading in, and fading out), e.g.:
Code: ags

if (fadeout) {
  // Fade out code
}
if (fadein) {
  // Fade in code
}


OR:
Code: ags

if (fading) { // i.e. fading == 1
 // Fade out code
}
else if (fading == 2) {
  // Fade in code
}


Otherwise, the code would be about the same, except if (transparent < fadelimit) { would be if (transparent > fadelimit) {, and transparent ++; becomes transparent --;
I know what you're thinking ... Don't think that.

Lazarus

Thanks for the replies

After experimenting with your codes is there away to turn on objects so that I don't have to set the objects transparency before player enters screen ie..
Code: ags
object[3].Transparency =100;

and without having the "object is initially visible" ticked?

I was able to do this with my previous funtion for example:
Code: ags
function FadeObjectIn (int objectid) {
Ã,  transparent =100;
Ã,  ObjectOn(objectid); // Turning object on
Ã,  while (transparent > 0) {
Ã,  SetObjectTransparency (objectid, transparent);
Ã,  transparent --;
Ã,  Wait (1); }
}


Here's the code at the moment I'm using:
Code: ags
function FadeObjectIn (Object *objectpoint,int value,int speed) {
Ã,  if (fadein) return 1;
Ã,  fadeobj = objectpoint;
Ã,  transparent = fadeobj.Transparency;
//Ã,  object[objectpoint].Visible == true; // trying to turn object on
//	ObjectOn(objectpoint);Ã,  
Ã,  fadein=1;
Ã,  fadespeed = speed;
Ã,  fadecounter = speed;
Ã,  fadelimit = value;
}


function repeatedly_execute_always() {
Ã,  if (fadeout) {
Ã,  Ã,  if (transparent < fadelimit) {
Ã,  Ã,  Ã,  fadecounter--;
Ã,  Ã,  Ã,  if (fadecounter<0) {
Ã,  Ã,  Ã,  Ã,  fadecounter=fadespeed;
Ã,  Ã,  Ã,  Ã,  transparent ++;
Ã,  Ã,  Ã,  Ã,  fadeobj.Transparency = transparent;
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  } else fadeout=0;
Ã,  Ã,  } 
//----------------------------------------------------------------
Ã,  if (fadein) {
Ã,  Ã,  if (transparent > fadelimit) {
Ã,  Ã,  Ã,  fadecounter++;
Ã,  Ã,  Ã,  if (fadecounter>0) {
Ã,  Ã,  Ã,  Ã,  fadecounter=fadespeed;
Ã,  Ã,  Ã,  Ã,  transparent --;
Ã,  Ã,  Ã,  Ã,  fadeobj.Transparency = transparent;
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã, } else fadein=0;
Ã,  Ã, } 
}
//----------------------------------------------------------------

function on_event (EventType event, int data) {
Ã,  if (event == eEventLeaveRoom && fadeout) {
Ã,  fadeout = 0;Ã,  //reset, so won't crash in next room
Ã,  fadeobj.Transparency = 0;
Ã,  }
//----------------------------------------------------------------
Ã,  if (event == eEventLeaveRoom && fadein) {
Ã,  fadein = 0;Ã,  //reset, so won't crash in next room
Ã,  Ã, fadeobj.Transparency = 100;
Ã,  }
}


Also the nonblocking isn't quite what I was expecting for example
when running a script where two or more objects fade in or out it goes staight to the last object, the only way to stop this was by using the wait command ie..
Code: ags
FadeObjectOut(oDoll, 50, 2);
Wait(200);
FadeObjectIn(oFly, 50, 5);
Wait(200);
FadeObjectIn(oTom, 0, 5);


so is there away to make these blocking instead
thanks again
*Currently using AGS Editor 2.70 (Build 2.70.601)*

Lazarus

Also I have a problem with

Code: ags
function FadeCharacterOut (int CharID, int speed) {
if (fading) return 1; //aborts if fading is currently being executedÃ,  
transparent=0;Ã,  
fadespeed = speed;Ã,  
fadecounter=fadespeed;Ã,  
fading=1;
}

Then in "repeatedly_execute" ("_always" if you don't want it to be blocked by anything) add:

if (fading) {Ã,  
if (transparent < 100) {
Ã,  Ã,  fadecounter--;
Ã,  Ã,  if (fadecounter<0) {
Ã,  Ã,  Ã,  fadecounter=fadespeed;
Ã,  Ã,  Ã,  transparent += 1; 
Ã,  Ã,  Ã, SetCharacterTransparency (CharID, transparent);
Ã,  Ã,  }
Ã,  } else fading=0;
} 


The problem is with "SetCharacterTransparency (CharID, transparent);"
undifined symbol 'CharID
any would be grateful thanks
*Currently using AGS Editor 2.70 (Build 2.70.601)*

Ashen

Quoteis there away to turn on objects so that I don't have to set the objects transparency before player enters screen

In theory, setting the object's transparency before you turn it on should work OK, i.e.:
Code: ags

objectpoint.Transparency = transparent;
objectpoint.Visible = true;

Is this not working for you? (You seemed to be having some trouble with turning the object on at all - note the proper usage here.)

Quote
Also the nonblocking isn't quite what I was expecting for example
when running a script where two or more objects fade in or out it goes staight to the last object, the only way to stop this was by using the wait command ie..

(code)

so is there away to make these blocking instead

Well, going back to the way you had it (fading from within the function rather than rep_ex_always, and using a Wait(..) command) would do that ...
Alternatively, changing your rep_ex_always to rep_ex and adding a Wait(1); in there should do it, e.g.:
Code: ags

  if (fadeout) {
    if (transparent < fadelimit) {
      fadecounter--;
      Wait(1); // <---
      if (fadecounter<0) {
        fadecounter=fadespeed;
        transparent ++;
        fadeobj.Transparency = transparent;
        }
      } else fadeout=0;
    } 

(And for fadein as well, obviously.)

QuoteThe problem is with "SetCharacterTransparency (CharID, transparent);"
undifined symbol 'CharID
That's because CharID is only declared within the FadeCharacterOut function - you'll need to make an int that can be accessed by the whole script (i.e. is declared at the top of the script, as with fadecounter, fadein, fadeout, etc). In fact, you might want to make that a Character pointer, rather than an int - SetCharacterTransparency(..) is obsolete in V2.7+, and replaced by Character.Transparency. You can keep the int if you'd rather, just use character[CharID].Transparency = transparent; (AGS Pointers tutorial, if you need it.)
I know what you're thinking ... Don't think that.

Lazarus

The object now turns on but doesn't fade in, it appears instantly here'sÃ,  the code:
Code: ags
function FadeObjectIn (Object *objectpoint,int value,int speed) {
	if (fadein) return 1;
	fadeobj = objectpoint;
	transparent = fadeobj.Transparency;

	objectpoint.Transparency = transparent;
	objectpoint.Visible = true;

	fadein=1;
	fadespeed = speed;
	fadecounter = speed;
	fadelimit = value;
}


am I placing it in the right place?

Also using the "Wait(1);" command in the function causes the mouse pointer and the status line to flash like mad until the function finishes.
*Currently using AGS Editor 2.70 (Build 2.70.601)*

Ashen

#16
That's the right place, but it's a little pointless there.
I mixed a up a few lines between the 'old' and 'new' versions of the function. The old version set transparent to 100 (i.e. totally invisible) which would work fine. The new version sets it to the Object's current transparency (I imagine so objects can be faded to/from partially transparent as well as totally invisible).  Since the object's Transparency property is 0 (fully visible) - even though it's switched off - it appears at full visibility, rather than fading. Perhaps checking in the object is starting from On or Off would help? E.g.:
Code: ags

function FadeObjectIn (Object *objectpoint,int value,int speed) {
  if (fadein) return 1;
  fadeobj = objectpoint;
  if (fadeobj.Visible == false) {
    fadeobj.Transparency = 100;
    fadeobj.Visible = true;
    // 'fadeobj' and 'objectpoint' both refer to the same object - I've kept the        useage consistant for clarity
  }
  transparent = fadeobj.Transparency;
  fadein=1;
  fadespeed = speed;
  fadecounter = speed;
  fadelimit = value;
}


QuoteAlso using the "Wait(1);" command in the function causes the mouse pointer and the status line to flash like mad until the function finishes.

Ah, yes it might do that ... I can't think what else to suggest. Seperating the FadeIn/Out functions with Wait(..) commands might be the easist way, unfortunately. Unless you rigged some sort of FadeIn/OutQueued functions? It would be a little extra work, but might ultimately be neater.
I know what you're thinking ... Don't think that.

Lazarus

#17
That seems to do the trick the object now fades in, I also added an opening brace on the end of "if (fadeobj.Visible == false)"
hope thats right.
Code: ags
function FadeObjectIn (Object *objectpoint,int value,int speed) {
Ã,  if (fadein) return 1;
Ã,  fadeobj = objectpoint;
Ã,  if (fadeobj.Visible == false) { // <--- Brace here?
Ã,  Ã,  fadeobj.Transparency = 100;
Ã,  Ã,  fadeobj.Visible = true;
 // 'fadeobj' and 'objectpoint' both refer to the same object - I've kept the useage consistant for clarity
	}
Ã,  transparent = fadeobj.Transparency;
Ã,  fadein=1;
Ã,  fadespeed = speed;
Ã,  fadecounter = speed;
Ã,  fadelimit = value;
}

the only problem now is trying to sort out is getting the functions to work seperate because the only way to get two or more objects to fade in or out, is to revert back to
Code: ags
function repeatedly_execute_always() {

and the in a script to use a wait command otherwise it seems to miss the first command and go staight to the last fade object, if that makes any sense eg,
Code: ags
FadeObjectIn(oDoll,0,2);
Wait(200);
FadeObjectOut(oFly,50,2);


As for FadeIn/OutQueued functions I wouldn't know where to start so any other help would be grateful
*Currently using AGS Editor 2.70 (Build 2.70.601)*

Ashen

That's weird, I swear that brace was there when I wrote that code...

I think the problem with fading more than one object/character is that they all try to use the same ints (transparent, fadespeed, fadecounter, fadelimit, fadein, fadeout and the fadeobj pointer) so only the last one actually registers.

Would you want them to fade in/out all at once, or one after the other?
Simultaneously, I think you'd need to turn all the variables into arrays (e.g. fadein[10], add an extra parameter to the FadeIn/Out functions (maybe not  - it could be handled internally), and a while loop to rep_ex_always, to make it check all instances of the variables.
Consecutively, you'd need something (e.g. a struct) to store the queued fades, and something in rep_ex(_always) to start the next one if needed.

That's just off the top of my head - I can post some actual code later, when I get the chance (and if I'm not too traumatised by the Valentine's Day startup screen).
I know what you're thinking ... Don't think that.

Lazarus

Thanks again for all the help

I guess it would be better if they happened one after the other, so I wouldn't need to use the "wait" between commands.

And yes the bootup picture is a bit frighteningÃ,  :o
*Currently using AGS Editor 2.70 (Build 2.70.601)*

SMF spam blocked by CleanTalk