Spawn Entity from picking up an Item

Aerisetta

Active member
I want to create a book ITEM.
When enemy dies, they drop the book
If the player GETs the item, I want it to spawn a STORY
Different enemy drops a different books (coded into level spawn)

Currently, I am using a didhitscript to spawn it
BOOK1 > didhitscript BOOK1.c > spawn story1
BOOK2 > didhitscript BOOK2.c > spawn story2
It works

However, I want to make many many books that spawn different things. So each book being a separate entity with a separate didhitscript seems really unscalable.

I was hoping I could declare a ANI_FOLLOW1 in the level script so that I can always spawn the same book entity, but the story that spawns is determined by which follow I declared.

Could that work? Any example or idea how this could work?
 
Got it, seems to work! please review

Spawned in stage

Code:
spawn    NunA
coords 1100 650
item    book
itemalias    book1
at 1300

spawn    Priest_Harp
coords 1300 750
item    book
itemalias    book2
at 1300

book.c
Code:
#import "data/scripts/library/spawn.h"

void main()
{//Script for book items
    void self = getlocalvar("self");
    char Item = getentityproperty(self,"name");

    if(Item=="book1"){
      changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW1"));
    } else if(Item=="book2"){
      changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW2"));
    } else if(Item=="book3"){
      changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW3"));
    } else if(Item=="book4"){
      changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW4"));
    } else if(Item=="book5"){
      changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW5"));
    } else if(Item=="book6"){
      changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW6"));
    }
}

The Item
Code:
name    Book
health    1
type    item
shadow    0
icon    data/chars/misc/applicon.gif
animationscript    data/scripts/saving.c
didhitscript    data/scripts/book.c


anim idle
    loop    0
    delay    1
    offset    8 14
    bbox    0 0 16 16
    frame    data/chars/misc/Book/Book.png
    frame    data/chars/misc/Book/Book.png
    
anim follow1
    delay    5
    offset    800 800
    frame    data/chars/dialogues/empty.gif
    @cmd    spawnA "story" 0 0 0 "Lore_Church_01"
    frame    data/chars/dialogues/empty.gif
    
anim follow2
    delay    5
    offset    800 800
    frame    data/chars/dialogues/empty.gif
    @cmd    spawnA "story" 0 0 0 "Lore_Church_07"
    frame    data/chars/dialogues/empty.gif
 
book.c
Code:
#import "data/scripts/library/spawn.h"

void main()
{//Script for book items
    void self = getlocalvar("self");
    char Item = getentityproperty(self,"name");

    if(Item=="book1"){
      changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW1"));
    } else if(Item=="book2"){
      changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW2"));
    } else if(Item=="book3"){
      changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW3"));
    } else if(Item=="book4"){
      changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW4"));
    } else if(Item=="book5"){
      changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW5"));
    } else if(Item=="book6"){
      changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW6"));
    }
}

Maybe instead of using "Alias", you would use hp?
This way, you won't have to check every time what kind of book it is.

For example, when spawning an item, instead of "Alias", set "Health", like this:

Code:
spawn    NunA
coords 1100 650
item    book
itemhealth 1
at 1300

Thus, by defining hp in that way, you can specify which animation your script should set.

book.c
Code:
void Self = getlocalvar("self");
int Animation = getentityproperty(Self, "health");
       
changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW" + Animation));
 
Maybe instead of using "Alias", you would use hp?
This way, you won't have to check every time what kind of book it is.

For example, when spawning an item, instead of "Alias", set "Health", like this:

Code:
spawn    NunA
coords 1100 650
item    book
itemhealth 1
at 1300

Thus, by defining hp in that way, you can specify which animation your script should set.

book.c
Code:
void Self = getlocalvar("self");
int Animation = getentityproperty(Self, "health");
      
changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW" + Animation));
oh that's cool too

which way is better i wonder? as long as it works and is easily scalable, i dont mind either way
 
oh that's cool too

which way is better i wonder? as long as it works and is easily scalable, i dont mind either way

Well, it depends on which way you are more comfortable with. If you do, that hp will tell the script what animation to run. Then you won't have to waste time adding more "if" to the script. Speaking of which, in my opinion, it would be easier if you used switch instead of if and didn't add the command to change the animation every time, but just added it once at the end of the script:

Code:
void main(){
    
    void Self = getlocalvar("self");
    char Item = getentityproperty(Self,"name");
    int Animation;

    switch(Item)
    {
        case "book1": Animation = 1; break;
        case "book2": Animation = 2; break;
        case "book3": Animation = 3; break;
        case "book4": Animation = 4; break;
        case "book5": Animation = 5; break;
        case "book6": Animation = 6; break;
        default: Animation = 1;
    }

    changeentityproperty(Self, "animation", openborconstant("ANI_FOLLOW" + Animation));
}
 
Code:
changeentityproperty(Self, "animation", openborconstant("ANI_FOLLOW" + Animation)); }

Welcome to the community @RedEye Samurai, but don't do this. Ever.

Concatenation of openborconstant() identifiers causes a huge performance penalty.

Keep the constant upstream, like this:

C:
void main(){
   
    void Self = getlocalvar("self");
    char Item = getentityproperty(Self,"name");
    int Animation;

    switch(Item)
    {
        default:
        case "book1": Animation = openborconstant("ANI_FOLLOW1"); break;
        case "book2": Animation = openborconstant("ANI_FOLLOW2"); break;
        case "book3": Animation = openborconstant("ANI_FOLLOW2"); break;
        case "book4": Animation = openborconstant("ANI_FOLLOW3"); break;
        case "book5": Animation = openborconstant("ANI_FOLLOW4"); break;
        case "book6": Animation = openborconstant("ANI_FOLLOW5"); break;
    }

    changeentityproperty(Self, "animation", Animation);
}
 
Back
Top Bottom