Jump to content

Photo

I need some help with an array


  • Please log in to reply
13 replies to this topic

#1 Master Maniac

Master Maniac

    Earth, Wind, Fire, and Water.

  • Members
  • Real Name:kris

Posted 15 December 2008 - 07:50 PM

how would I make an array that includes all the enemies on a screen?

and also, enemies Vx and Vy can be manipulated, correct?

#2 Elmensajero

Elmensajero

    Doyen(ne)

  • Members
  • Real Name:John
  • Location:Raleigh, North Carolina

Posted 15 December 2008 - 08:14 PM

I think this will work, although I am not sure what the current limitations are with arrays. Change "MAXENEMIES" to whatever value you want, this is what controls the size of the array. Check this site to see all of the properties of each npc you can control.

CODE

import "std.zh"

const int MAXENEMIES=100;

ffc script enemycontrol
{
    void run()
    {
        npc e[MAXENEMIES];

        Waitframes(4);
        
        for(int i=0;i<Screen->NumNPCs() && i<MAXENEMIES;i++)
        {
            e[i]=Screen->LoadNPC(i);
        }
        
        //add rest of the script here
    }
}


(Thanks goes to Gleeok for giving me the idea of actually doing this through a script posted at AGN.)

Edited by Elmensajero, 15 December 2008 - 08:19 PM.


#3 Master Maniac

Master Maniac

    Earth, Wind, Fire, and Water.

  • Members
  • Real Name:kris

Posted 15 December 2008 - 08:32 PM

O_O

not as simple as i remember it being...

i just need it to find all the enemies and be able to edit their Vx and Vy...

and also, i need to put this into a global script.
I'm making a vaccuum-like (how the hell do you spell that word?) effect that pulls enemies into a black hole and kills the ones that actually get there.


#4 Joe123

Joe123

    Retired

  • Members

Posted 16 December 2008 - 04:35 AM

That won't work, cause you're loading NPC 0.
I'm also not sure whether you can pass even a constant as the size of an array, I'm pretty sure it has to be a number.

If you're doing it in the global script, you needn't really load them as an array, you only need one enemy pointer which you check all enemies with.


CODE
    for(int i=1;i<=Screen->NumNPCs();i++){
        npc e = Screen->LoadNPC(i);
        if(npc is close enough){//do some collision checking and movement routines here...
            move npc over;
        }
    }

I've got a code that sucks things in at home aswell if you want (wrote it for Kirby), but I can't get hold of it at the moment cause I'm at school.

Enemies don't have Vx and Vy values that you can manipulate easily like ffcs I'm afraid.

#5 Master Maniac

Master Maniac

    Earth, Wind, Fire, and Water.

  • Members
  • Real Name:kris

Posted 16 December 2008 - 10:47 AM

ooh that kinda sucks...

i do need a vaccuum, but i need it to act a certain way.

when the enemies are farther away from it, it pulls them quickly, and the closer they get, the slower they pull.
and i thought i could manage this with the s=d/t formula... i noticed that if you put an ffc's coordinates in, it would achieve that effect because it checks every frame for the ffc's coordinates and therefore changes the speed of its movement.

but like you just said, that cant be done with enemies... so could i have a look at your vaccuum? please? lol

#6 Joe123

Joe123

    Retired

  • Members

Posted 16 December 2008 - 11:25 AM

CODE
int SuctionSpeed = 2;
int SuctionRange = 48;

        for(i=1;i<=Screen->NumNPCs();i++){
            npc e = Screen->LoadNPC(i);
            if((Distance(Link->X,Link->Y,e->X,e->Y) <= SuctionRange){
                int m;
                if(Link->X-e->X != 0) m = (Link->Y-e->Y)/(Link->X-e->X);
                if(Link->X-e->X > 0) e->X += SuctionSpeed;
                else e->X = e->X -= SuctionSpeed;
                int c = e->Y-m*e->X;
                e->Y = m*e->X+c;
            }
        }


That code goes through all enemies on the screen, checks whether they are within 'SuctionRange' pixels of Link, and then moves them over to him at a rate of 'SuctionSpeed' pixels per frame.
If you want to alter the speed they're moved at dependant on the distance, all you'd have to do is have a bit of code to change SuctionSpeed depending on how far away the enemies are.
Obviously if you want to change where they're sucked to from Link to something else, just change the Link->X and Link->Y to the coordinates.

Good to see you back by the way.
Where did you go?

#7 Master Maniac

Master Maniac

    Earth, Wind, Fire, and Water.

  • Members
  • Real Name:kris

Posted 16 December 2008 - 11:41 AM

my computer died lol

so i had to get a new one, and here i am with vista. works better than i had expected...

but yeah, thanks for this piece of code =) ill be sure to post a demo quest of what im using this for, if it works. (knowing my scripting luck, it wont but still)

thanks. i really appreciate it. and its good to see you again.

EDIT: and suction speed is in pixels per frame, correct?

Edited by Master Maniac, 16 December 2008 - 11:43 AM.


#8 Joe123

Joe123

    Retired

  • Members

Posted 16 December 2008 - 12:15 PM

Yeah so if you want it to suck slower than 1 pixel per frame, you'll need a counter or summat.
Also, I just realised it won't work if the enemy is directly to the above or below Link (at the same Y position), due to divide by zero.
I can fix that if you want.

Moved to Scripting Practise.

#9 Master Maniac

Master Maniac

    Earth, Wind, Fire, and Water.

  • Members
  • Real Name:kris

Posted 16 December 2008 - 12:24 PM

my bad about the location... lol i didnt know how much the forum had changed =)

anyway, yes that would be nice joe, adding the suction from directly above and below.

I'm almost done with the script. just have to add in the FFC setup for it and im done.

here is what i got so far:

CODE
//Black hole wand script.

//lets see if I can do this properly again... this shouldnt be too difficult now...
//also thanks to joe for the help with this script.

//VARIABLES!!

int blackholetile;

//thats all ill do for now... this is just for warmup so i dont want to do anything too complex.

bool blackhole=false;

item script blackholewand(){
    blackhole=true;
}



//global part of the script below.

if(blackhole){

    int seconds_exist= 5;

    int SuctionSpeed = 2;
    int SuctionRange = 48;

    int frames_exist=seconds_exist*60;

    int exist;

    bool blackhole_existing;

    blackhole_cast=Screen->LoadFFC(1);

    if(!blackhole_existing){
        exist=0;
    }

    if(Link->Dir=DIR_UP){

        blackhole_cast->X=Link->X;
        blackhole_cast->Y=Link->Y-3;
        blackhole=false;
        blackhole_existing=true;
    }

    if(Link->Dir=DIR_DOWN){

        blackhole_cast->X=Link->X;
        blackhole_cast->Y=Link->Y+3;
        blackhole=false;
        blackhole_existing=true;

    }

    if(Link->Dir=DIR_LEFT){

        blackhole_cast->X=Link->X-3;
        blackhole_cast->Y=Link->Y;
        blackhole=false;
        blackhole_existing=true;

    }

    if(Link->Dir=DIR_RIGHT){

        blackhole_cast->X=Link->X+3;
        blackhole_cast->Y=Link->Y;
        blackhole=false;
        blackhole_existing=true;

    }

    while(blackhole_existing){

        exist++;

        if(exist=frames_exist){
            blackhole_exist=false;
        }

        for(i=1;i<=Screen->NumNPCs();i++){
            npc e = Screen->LoadNPC(i);
            if((Distance(blackhole_cast->X,blackhole_cast->Y,e->X,e->Y) <= SuctionRange){
                int m;

                if(blackhole_cast->X-e->X != 0) m = (blackhole_cast->Y-e->Y)/(blackhole_cast->X-e->X);

                if(blackhole_cast->X-e->X > 0) e->X += SuctionSpeed;

                else e->X = e->X -= SuctionSpeed;

                int c = e->Y-m*e->X;

                e->Y = m*e->X+c;

                if(e->X>=blackhole_cast->X-8&&e->X<=blackhole_cast->X+8&&e->Y>=blackhole_cast->Y-8&&e->Y<=blackhole_cast->Y+8){

                    e->HP=0;
                }
            }
        }


i know i know its not perfect, and its kinda buggy, but i wanted to do something small before I got into quest design again.

eventually it will have the screen quake and everything and it will have a better capatability with larger that 16x16 sprites for black holes lol

but this is the best i got so far. I'm pretty sure im missing some vital stuff... but im back to the nooblet level of scripting until i get back into the swing of things

#10 Gleeok

Gleeok

    It's dangerous to dough alone, bake this.

  • Members
  • Real Name:Pillsbury
  • Location:Magical Land of Dough

Posted 16 December 2008 - 12:35 PM

Wow Joe, that script really sucks... Silence! that one never gets old! icon_eek.gif


Anyways MM, I made a script that fires a 2x2 whirlwind that sucks in enemies as it moves across the screen a while ago. Take a look at it, or feel free to use it.


QUOTE
That won't work, cause you're loading NPC 0.


Actually, I like Elemensajo's method because the pointers stay valid. You just add Screen->LoadNPC(i+1); to it.

#11 Joe123

Joe123

    Retired

  • Members

Posted 16 December 2008 - 12:39 PM

Well, to fix the directly above/below thing, you need to edit it so that where it says '->X-e->X != 0', you move all of the movement routine into that if, then have an else that moves the npc's Y coordinate only, depending on if it's below or above.

And I've just realised how convoluted that sounded >_<
I'll go fix it myself if you don't get it.


You need 'void run' inside the item script, and obviously your global section isn't actually part of the global script yet.
Be careful with your '=' is set to and '==' is equal to.

I'm not sure if the link-up between the item script and global script is quite right, but I haven't looked too hard so it might be.



EDIT:
I know that's all you have to do to fix it.
It's not really all that applicable to the global script though, unless you set up something to check whether you've changed screens or not.
Then you need to re-load the array every time you change screens, and (if you want to do it properly) if the number of enemies on the screen increases.
Having an array is great for an ffc script, but it's less useful in the global script.

#12 Master Maniac

Master Maniac

    Earth, Wind, Fire, and Water.

  • Members
  • Real Name:kris

Posted 16 December 2008 - 12:41 PM

maybe. But i already have joe's part of the script all set up and everything...

where is that script, gleeok? my black hole is supposed to be stationary, but that would be nice to look at as a refrence

EDIT: ahh forgot the = and == difference. thanks for pointing that out lol

Edited by Master Maniac, 16 December 2008 - 12:43 PM.


#13 Gleeok

Gleeok

    It's dangerous to dough alone, bake this.

  • Members
  • Real Name:Pillsbury
  • Location:Magical Land of Dough

Posted 16 December 2008 - 12:53 PM

QUOTE(Master Maniac @ Dec 16 2008, 09:41 AM) View Post

where is that script, gleeok? my black hole is supposed to be stationary, but that would be nice to look at as a refrence


Where else would it be? hmmm... I don't have many threads here. See post #23.

#14 Master Maniac

Master Maniac

    Earth, Wind, Fire, and Water.

  • Members
  • Real Name:kris

Posted 16 December 2008 - 12:57 PM

post#23?

there are only 14 posts here...

(pardon me for my stupidity right now. its my medication.)


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users