Jump to content

Photo

Scripting one thing to happen every frame and another to happen...


  • Please log in to reply
2 replies to this topic

#1 kmack08

kmack08

    Newbie

  • Members

Posted 03 April 2021 - 09:45 AM

Is it possible to script something to happen run every frame (using Waitframe( ) in the loop) WHILE ALSO scripting something else (like a sound effect to play) to happen every x number of frames?  I can't figure out how to do it.

 

For example, let's say every frame I want a FFC to shoot a flame but only every 60 frames I want it to play a sound effect...how might I go about achieving this?



#2 Jamian

Jamian

    ZC enthusiast

  • Members

Posted 03 April 2021 - 10:09 AM

Use an int counter, for example:

int tics = 0;
while (true){
	// do something every frame here
	tics++;
	if (tics == 60){
		// do something every 60 frames here
		tics = 0;
	}
	Waitframe();
}

Edited by Jamian, 03 April 2021 - 10:10 AM.

  • Emily likes this

#3 Deedee

Deedee

    Bug Frog Dragon Girl

  • Moderators
  • Real Name:Deedee
  • Pronouns:She / Her, They / Them
  • Location:Canada

Posted 03 April 2021 - 01:23 PM

Modulo is very helpful for this. Modulo is the % sign, and it represents the remainder of a number divided by another number. 7 divided by 4 equals 1, but has a remainder of 3; 8 divided by 4 equals 2, but has a remainder of 0. It sounds complex but it's not actually that bad.

The double slashes below (//) represents a comment; anything after it is me explaining stuff.

if (i % 4 == 0) //triggers if i equals 0, 4, 8, 12, 16...
if (i % 4 == 1) //triggers if i equals 1, 5, 9, 13, 17...
if (i % 4 == 2) //triggers if i equals 2, 6, 10, 14, 18...
if (i % 4 == 3) //triggers if i equals 3, 7, 11, 15, 19...

if (i % 5 == 0) //triggers if i equals 0, 5, 10, 15, 20...
if (i % 5 == 1) //triggers if i equals 1, 6, 11, 16, 21...

++i; //Adds 1 to i. Same as i++, but it runs faster.
i%=30; //this will wrap i to a value between 0 and 30; once it reaches or goes over 30 it wraps back around to 0. 31 becomes 1, 62 becomes 2, etc.

So if you want something to happen every 60 frames... How would you do that using what I posted above? 


  • Emily likes this


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users