Jump to content

Photo

How do I update a array by setting constants within a function?


  • Please log in to reply
2 replies to this topic

#1 Shadowblitz16

Shadowblitz16

    Illustrious

  • Members

Posted 02 November 2016 - 07:33 PM

hello I was just wondering how to do something similar to what Tango.zh does where you set styles within a function and then later run them?

 

I'm trying to make a small death style library where users can setup death styles and then run them in whatever order they choose

 

for example a gameboy style boss might have 3 death styles that run in this order..
1) stop, show message while flashing and playing dying sound

2) spawn explosions

3) play death effect and die

 

so I want to be able to set up these death styles and call them in order to achieve this effect

 

 



#2 Timelord

Timelord

    The Timelord

  • Banned
  • Location:Prydon Academy

Posted 12 November 2016 - 04:12 AM

I think that you are misunderstanding what's happening with the use of constants in those function calls: The constants are just handy references that resolve into numeric literals, and make code easier to read. ... Here's an example:

const int THING = 1;
const int OTHER_THING = 2;
const int YET_ANOTHER = 3;
void DoAThing(int thing){
    if ( thing == THING ) //Do this.
    if ( thing == OTHER_THING ) //Do that
    if ( thing == YET_ANOTHER  ) //Do it instead.
}

This is identical to:

void DoAThing(int thing){
    if ( thing == 1 ) //Do this.
    if ( thing == 2 ) //Do that
    if ( thing == 3 )  //Do it instead.
}

Either way, if you wanted to 'do that', you could call this as:

DoAThing(OTHER_THING);

or

DoAThing(2);

Note that you do not need to declare constants in all caps: That is simply a style choice...and that constants do not do anything directly. They are solely a way to give a numeric literal an identifier so that a user can easily understand the code. 

 

I'm also fairly certain that constants may only ever be declared at a global/file scope. Declaring them inside the body of a function, or at the scope of a script, should halt the parser and generate an error. Admittedly, it would be nice if they were something that you could scope, but alas, 'tisn't so...

 

Another very useful reason to use constants instead of hardcoding numeric literals, is that you can quickly change their value at a sweeping, global scope. In the first example above, you could change:

const int OTHER_THING = 2;

...to...

const int OTHER_THING = 106;

...and the first function (above) would still work, as it compares using the constant, so the change of its assigned value will affect both the function doing the compare, and any function calls that use it as a parameter. The second function, with its if/else compares hardcoded to numbers, on the other hand, would cease working for 'case 2'.

 

Does this answer your questions?

 

--------

 

On another note, I'd be interested in working with you on the header.


Edited by ZoriaRPG, 12 November 2016 - 04:18 AM.


#3 Shadowblitz16

Shadowblitz16

    Illustrious

  • Members

Posted 27 December 2016 - 02:46 PM

@ZoriaRPG I was using your LA death effect for a base but I am currently working on a classic style quest to get used to the zc editor

 

this is my current code but I'm not working on it at the moment

// Generalised Enemy Explosions, from LinksAwakening_v0.54.zs
// Sub-version 0.2.7
// 3rd July, 2015
                
//Enemy Death Effect Constants
const int ENEMY_DYING_EFFECT_ON = 1;
const int ENEMY_DYING_SFX = 0;
const int ENEMY_DYING_SPRITE = 91;
const int ENEMY_DYING_SPRITE_SPEED = 12;
const int ENEMY_DYING_SPRITE_WIDTH = 2;
const int ENEMY_DYING_SPRITE_HEIGHT = 2;
const int ENEMY_DYING_DELAY = 0;
//Enemy Explosion Constants
const int ENEMY_EXPLOSION_SFX = 3;
const int ENEMY_EXPLOSION_SPRITE_OFFSET = 92;
const int ENEMY_EXPLOSION_SPRITE_SPEED_OFFSET = 12;
const int ENEMY_EXPLOSION_SPRITE_WIDTH_OFFSET = 2;
const int ENEMY_EXPLOSION_SPRITE_HEIGHT_OFFSET = 2;
const int ENEMY_EXPLOSION_TIMES_OFFSET = 0;
const int ENEMY_EXPLOSION_RANDOMNESS_OFFSET = 0;
const int ENEMY_EXPLOSION_DELAY1_OFFSET = 0;
const int ENEMY_EXPLOSION_DELAY2_OFFSET = 0;
const int ENEMY_EXPLOSION_DAMAGE_OFFSET = 1;


//Exploding Enemy Death Effect Flag
const int XENEMY_DYING_EFFECT_ON = 0;
//Exploding Enemy Explosion Constants
const int XENEMY_EXPLOSION_SFX_OFFSET = 0;
const int XENEMY_EXPLOSION_SPRITE_OFFSET = 92;
const int XENEMY_EXPLOSION_SPRITE_SPEED_OFFSET = 12;
const int XENEMY_EXPLOSION_SPRITE_WIDTH_OFFSET = 2;
const int XENEMY_EXPLOSION_SPRITE_HEIGHT_OFFSET = 2;
const int XENEMY_EXPLOSION_TIMES_OFFSET = 0;
const int XENEMY_EXPLOSION_RANDOMNESS_OFFSET = 0;
const int XENEMY_EXPLOSION_DELAY1_OFFSET = 0;
const int XENEMY_EXPLOSION_DELAY2_OFFSET = 0;
const int XENEMY_EXPLOSION_DAMAGE_OFFSET = 1;


//MiniBoss Death Effect Flag 
const int MINIBOSS_DYING_EFFECT_ON = 1;
//MiniBoss Explosion Offset Constants
const int MINIBOSS_EXPLOSION_SFX_OFFSET = 0;
const int MINIBOSS_EXPLOSION_SPRITE_OFFSET = 0;
const int MINIBOSS_EXPLOSION_SPRITE_WIDTH_OFFSET = 0;
const int MINIBOSS_EXPLOSION_SPRITE_HEIGHT_OFFSET = 0;
const int MINIBOSS_EXPLOSION_TIMES_OFFSET = 0;
const int MINIBOSS_EXPLOSION_RANDOMNESS_OFFSET = 0;
const int MINIBOSS_EXPLOSION_DELAY1_OFFSET = 0;
const int MINIBOSS_EXPLOSION_DELAY2_OFFSET = 0;
const int MINIBOSS_EXPLOSION_DAMAGE_OFFSET = -1;


//MiniBoss Death Effect Flag
const int NORMBOSS_DYING_EFFECT_ON = 1;
//NormBoss Explosion Offset Constants
const int NORMBOSS_EXPLOSION_SFX_OFFSET = 0;
const int NORMBOSS_EXPLOSION_SPRITE_OFFSET = 0;
const int NORMBOSS_EXPLOSION_SPRITE_WIDTH_OFFSET = 0;
const int NORMBOSS_EXPLOSION_SPRITE_HEIGHT_OFFSET = 0;
const int NORMBOSS_EXPLOSION_TIMES_OFFSET = 0;
const int NORMBOSS_EXPLOSION_RANDOMNESS_OFFSET = 0;
const int NORMBOSS_EXPLOSION_DELAY1_OFFSET = 0;
const int NORMBOSS_EXPLOSION_DELAY2_OFFSET = 0;
const int NORMBOSS_EXPLOSION_DAMAGE_OFFSET = -1;


//MiniBoss Death Effect Flag
const int MAINBOSS_DYING_EFFECT_ON = 1;
//MainBoss Explosion Offset Constants
const int MAINBOSS_EXPLOSION_SFX_OFFSET = 0;
const int MAINBOSS_EXPLOSION_SPRITE_OFFSET = 0;
const int MAINBOSS_EXPLOSION_SPRITE_WIDTH_OFFSET = 0;
const int MAINBOSS_EXPLOSION_SPRITE_HEIGHT_OFFSET = 0;
const int MAINBOSS_EXPLOSION_TIMES_OFFSET = 0;
const int MAINBOSS_EXPLOSION_RANDOMNESS_OFFSET = 0;
const int MAINBOSS_EXPLOSION_DELAY1_OFFSET = 0;
const int MAINBOSS_EXPLOSION_DELAY2_OFFSET = 0;
const int MAINBOSS_EXPLOSION_DAMAGE_OFFSET = -1;



//Arrays

// A list of Exploding-enemies enemies by Enemy ID
// Add, or remove values from this list, to increase, or decrease the enemies treated 
// as Exploding-enemies for determining the number of explosions.
int EXPLODING_ENEMIES[] = {    

};

// A list of Mini-boss enemies by Enemy ID
// Add, or remove values from this list, to increase, or decrease the enemies treated 
// as Mini-bosses for determining the number of explosions.
int MINI_BOSSES[] = {    
	59,
	66,
	6,
	7,
	68,
	69,
	70,
	71,
	74,
	75
};

// A list of Normal-boss enemies, by Enemy ID
// Add, or remove values from this list, to increase, or decrease the enemies treated 
// as Normal-bosses for determining the number of explosions.
int NORM_BOSSES[] = {
	58,
	61,
	62,
	63,
    64,
	65,
	71,
	72,
    73,
	76,
	77,
	93,
    94,
	103,
	104,
	105,
    109,
	110,
	111,
	112,
    114,
	121,
	122
};

// A list of Main-boss enemies by Enemy ID
// Add, or remove values from this list, to increase, or decrease the enemies treated 
// as Main-bosses for determining the number of explosions
int MAIN_BOSSES[] = {
	78
} 
//Cause enemies to have a death animation explosion with custom sprites.
//Run before Waitdraw() in the infinite ( while(true) ) loop of your global active script. 
//You need only call this one function to run the entirety of this code.
void EnemiesExplodeOnDeath()
{
	if (IsDeadEnemies())
	{
		int deadEnemies = GetDeadEnemies();
		int typeEnemies = GetTypeEnemies(deadEnemies);
		ExplodeEnemies(deadEnemies, typeEnemies);
	}
}

bool IsDeadEnemies()
{
	for(int i=0; i<Screen->NumNPCs(); i++)
	{
		npc current_enemy = Screen->LoadNPC(i); //Declare an npc variable, 
        if (current_enemy->isValid()) 
		{
			if (current_enemy->hp <= 0 ) { return true }
		}
	}
	return false;
}

int GetDeadEnemies()
{
	int dead_enemies[16] {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
	for(int i=0; i<Screen->NumNPCs(); i++)
	{
		npc current_enemy = Screen->LoadNPC(i); //Declare an npc variable, 
        if (current_enemy->isValid()) 
		{
			if (current_enemy->hp <= 0 )
			{  
				for (int n=0; n<16; n++)
				{
					if (n == 0 ) { dead_enemies[n] = current_enemy->ID; break; }
				}
			}
		}
	}
	return dead_enemies;
}

int GetTypeEnemies(int dead_enemies)
{
	int type_enemies[16] {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
	for(int e=0; e<16(); e++)
	{
		for (int l=0; l<SizeOfArray(EXPLODING_ENEMIES); l++)
		{
			if (dead_enemies[e]->ID == EXPLODING_ENEMIES[l]) { type_enemies[e] == 1; }
		}
		for (int l=0; l<SizeOfArray(MINI_BOSSSES); l++)
		{
			if (dead_enemies[e]->ID == MINI_BOSSES[l]) { type_enemies[e] == 2; }
		}
		for (int l=0; l<SizeOfArray(NORM_BOSSES); l++)
		{
			if (dead_enemies[e]->ID == NORM_BOSSES[l]) { type_enemies[e] == 3; }
		}
		for (int l=0; l<SizeOfArray(MAIN_BOSSES); l++)
		{
			if (dead_enemies[e]->ID == MAIN_BOSSES[l]) { type_enemies[e] == 4; }
		}
	}
	return type_enemies;
}

void ExplodeEnemies(int dead_enemies, int type_enemies)
{
	int sprite  = ENEMY_EXPLOSION_SPRITE;
	int width   = ENEMY_EXPLOSION_SPRITE_WIDTH;
	int height  = ENEMY_EXPLOSION_SPRITE_HEIGHT;
	int times   = ENEMY_EXPLOSION_TIMES;
	int random  = ENEMY_EXPLOSION_RANDOMNESS;
	int delay1  = ENEMY_EXPLOSION_DELAY1;
	int delay2  = ENEMY_EXPLOSION_DELAY2;
	int damage  = ENEMY_EXPLOSION_DAMAGE;
	
	for (int e=0; e<16; e++)
	{
		if (dead_enemies[e] != 0)
		{
			if (type_enemies[e] == 4) 
			{ 
				sfx1   += MAINBOSS_DYING_SFX_OFFSET;
				sfx2   += MAINBOSS_EXPLOSION_SFX_OFFSET;
				sprite += MAINBOSS_EXPLOSION_SPRITE_OFFSET
				width  += MAINBOSS_EXPLOSION_SPRITE_WIDTH_OFFSET
				height += MAINBOSS_EXPLOSION_SPRITE_HEIGHT_OFFSET
				times  += MAINBOSS_EXPLOSION_TIMES_OFFSET
				random += MAINBOSS_EXPLOSION_RANDOMNESS_OFFSET
				delay1 += MAINBOSS_EXPLOSION_DELAY1_OFFSET
				delay2 += MAINBOSS_EXPLOSION_DELAY2_OFFSET
				damage += MAINBOSS_EXPLOSION_DAMAGE_OFFSET
			}
			else if (type_enemies[e] == 3) 
			{ 
				sfx2   += NORMBOSS_EXPLOSION_SFX_OFFSET;
				sprite += NORMBOSS_EXPLOSION_SPRITE_OFFSET
				width  += NORMBOSS_EXPLOSION_SPRITE_WIDTH_OFFSET
				height += NORMBOSS_EXPLOSION_SPRITE_HEIGHT_OFFSET
				times  += NORMBOSS_EXPLOSION_TIMES_OFFSET
				random += NORMBOSS_EXPLOSION_RANDOMNESS_OFFSET
				delay1 += NORMBOSS_EXPLOSION_DELAY1_OFFSET
				delay2 += NORMBOSS_EXPLOSION_DELAY2_OFFSET
				damage += NORMBOSS_EXPLOSION_DAMAGE_OFFSET
			}
			else if (type_enemies[e] == 2) 
			{ 
				sfx2   += MINIBOSS_EXPLOSION_SFX_OFFSET;
				sprite += MINIBOSS_EXPLOSION_SPRITE_OFFSET
				width  += MINIBOSS_EXPLOSION_SPRITE_WIDTH_OFFSET
				height += MINIBOSS_EXPLOSION_SPRITE_HEIGHT_OFFSET
				times  += MINIBOSS_EXPLOSION_TIMES_OFFSET
				random += MINIBOSS_EXPLOSION_RANDOMNESS_OFFSET
				delay1 += MINIBOSS_EXPLOSION_DELAY1_OFFSET
				delay2 += MINIBOSS_EXPLOSION_DELAY2_OFFSET
				damage += MINIBOSS_EXPLOSION_DAMAGE_OFFSET
			}
			else if (type_enemies[e] == 2) 
			{ 
				sfx2   += MAINBOSS_EXPLOSION_SFX_OFFSET;
				sprite += MINIBOSS_EXPLOSION_SPRITE_OFFSET;
				width  += MINIBOSS_EXPLOSION_SPRITE_WIDTH_OFFSET;
				height += MINIBOSS_EXPLOSION_SPRITE_HEIGHT_OFFSET;
				times  += MINIBOSS_EXPLOSION_TIMES_OFFSET;
				random += MINIBOSS_EXPLOSION_RANDOMNESS_OFFSET;
				delay1 += MINIBOSS_EXPLOSION_DELAY1_OFFSET;
				delay2 += MINIBOSS_EXPLOSION_DELAY2_OFFSET;
				damage += MINIBOSS_EXPLOSION_DAMAGE_OFFSET;
			}
			
			SpawnDeathEffect(ENEMY_EXPLOSION_SPRITE, ENEMY_EXPLOSION_SPRITE_WIDTH, ENEMY_EXPLOSION_SPRITE_HEIGHT, ENEMY_EXPLOSION_TIMES, ENEMY_EXPLOSION_RANDOMNESS, ENEMY_EXPLOSION_DELAY1, ENEMY_EXPLOSION_DELAY2, ENEMY_EXPLOSION_DAMAGE)
			if (type_enemies[e] == 4 && MAINBOSS_DYING_EFFECT_ON || 
			    type_enemies[e] == 3 && NORMBOSS_DYING_EFFECT_ON ||
			    type_enemies[e] == 2 && MINIBOSS_DYING_EFFECT_ON || 
			    type_enemies[e] == 1 && XENEMIES_DYING_EFFECT_ON)
				{
					SpawnDeathEffect(ENEMY_DEATH_SPRITE, ENEMY_DEATH_SPRITE_WIDTH, ENEMY_DEATH_SPRITE_HEIGHT, 1, 0, 0, 0)
				}
		}
	}
}

void SpawnDeathEffect(int sprite, int width, int height, int number, int randomness, int delay, int damage)
{
	for (int x = 0; x<number x++)
	{
		explosion = Screen->CreateEWeapon(EW_SCRIPT1); //Make an explosion..
		explosion->x = (deadnpc[x]->x + 7) * width;
		explosion->y = (deadnpc[x]->y + 7) * height;
		explosion->UseSprite(sprite);
		explosion->Extend = 1; //...with extended size
		explosion->TileWidth = width; //...this many tiles wide
		explosion->TileHeight = height; ///...this many tiles high
		if (damage > 0) { explosion->Damage = damage; } //...that doesn;t hurt anyone...
		
		Waitframes(delay);
	}
}



0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users