It is currently 24 May 2013, 04:13

Post new topic Reply to topic  Page 1 of 1
 [ 6 posts ] 

[APPROVED] CustomInventory Items as New Functions


Author Message
 Post subject: CustomInventory Items as New Functions
PostPosted: 28 Feb 2012, 18:08 
Offline
Commander Keen  A rather ghastly dragon
User avatar
Joined: 11 Dec 2009, 20:01
Posts: 2661
Before we start, this is just a really tiny tutorial for a little trick you can take advantage of. Only really two links you need to know about, and they're things you want to know anyway:


Sometimes, you want monsters, players, or other actors to do a lot of things at once, while it's doing other things. For example, if you wanted to make a standard imp do something interesting in its See state if it's fully submerged underwater, you might do something like this:

Code:
  See:
    TROO A 0 A_JumpIf(waterlevel == 3, 2)
    TROO A 0 A_ChangeFlag("NoPain", 0)
    Goto See+3
    TROO A 0 A_ChangeFlag("NoPain", 1)
    TROO A 3 A_Chase
    TROO A 0 A_JumpIf(waterlevel == 3, 2)
    TROO A 0 A_ChangeFlag("NoPain", 0)
    Goto See+7
    TROO A 0 A_ChangeFlag("NoPain", 1)
    TROO A 3 A_Chase
    TROO B 0 A_JumpIf(waterlevel == 3, 2)
    TROO B 0 A_ChangeFlag("NoPain", 0)
    Goto See+11
    TROO B 0 A_ChangeFlag("NoPain", 1)
    TROO B 3 A_Chase
    TROO B 0 A_JumpIf(waterlevel == 3, 2)
    TROO B 0 A_ChangeFlag("NoPain", 0)
    Goto See+15
    TROO B 0 A_ChangeFlag("NoPain", 1)
    TROO B 3 A_Chase
    TROO C 0 A_JumpIf(waterlevel == 3, 2)
    TROO C 0 A_ChangeFlag("NoPain", 0)
    Goto See+19
    TROO C 0 A_ChangeFlag("NoPain", 1)
    TROO C 3 A_Chase
    TROO C 0 A_JumpIf(waterlevel == 3, 2)
    TROO C 0 A_ChangeFlag("NoPain", 0)
    Goto See+23
    TROO C 0 A_ChangeFlag("NoPain", 1)
    TROO C 3 A_Chase
    TROO D 0 A_JumpIf(waterlevel == 3, 2)
    TROO D 0 A_ChangeFlag("NoPain", 0)
    Goto See+27
    TROO D 0 A_ChangeFlag("NoPain", 1)
    TROO D 3 A_Chase
    TROO D 0 A_JumpIf(waterlevel == 3, 2)
    TROO D 0 A_ChangeFlag("NoPain", 0)
    Goto See+31
    TROO D 0 A_ChangeFlag("NoPain", 1)
    TROO D 3 A_Chase
    Loop
//Don't quote me on this, I haven't tested it. Just an example of what it might look like


Pretty obviously, this is hilariously ugly, can bug out easily if not done right, and you have to keep close track of your jumps, gotos and etc. THERE IS A BETTER WAY TO DO THIS!

In "real" programming, if you have one function that does the same thing repeatedly in a sequence with other stuff, we outsource that repeated thing to another function and call that, so it looks cleaner and is easier to troubleshoot. This is why, for example, A_MissileAttack has A_FaceTarget built in, instead of calling it with a 0-delay just beforehand. Unfortunately in Decorate, we can't define new functions (the A_ things)... But we have CustomInventory!

CustomInventory, by definition, is an item that allows you to define a Pickup state (or Use, but we want Pickup for this) with existing functions to make a powerup or other item that does anything you want it to do, with a few limitations. These functions in the Pickup and Use states are executed BY THE ACTOR WHO PICKED UP THE ITEM, not the item itself. Thus, we can slap some of those functions we have in a CustomInventory item, and A_GiveInventory it so we can execute those checks and flag changes to one call!

Yes! As the wiki page says, monsters can have items! They have no code for picking up items (for good or ill), so they either have to give it to themselves or it has to be done in ACS.

Code:
  See:
    TROO AABBCCDD 3 A_GiveInventory("ImpWaterCheck", 1) //Execute our new function!
    Loop

Actor ImpWaterCheck : CustomInventory
{
  Inventory.PickupMessage ""
  Inventory.PickupSound ""
  -CountItem
  states
  {
  Spawn:
    TNT1 A 1
    Fail //We don't want this ever spawning.
  Pickup:
    TNT1 A 0 A_Chase
    TNT1 A 0 A_JumpIf(waterlevel == 3, "Underwater")
    TNT1 A 0 A_ChangeFlag("NoPain", 0)
    Stop
  Underwater: //Because new states are just awesome
    TNT1 A 0 A_ChangeFlag("NoPain", 1)
    Stop
  }
}


It requires a new CustomInventory item, but we've just made our See state much easier to follow. Now, keep in mind, CustomInventory items CANNOT have delays! Nor would we want them to in this case because it we want all of this to happen immediately when it's given. Still, CustomInventory items can't have delays, and no matter what number you put in, it'll still execute all of it with 0 delays. Now, I really don't recommend this for just collapsing a few 0-delay things together, but it's ideal for when you have checks, jumps and things like that in a looping state, because that gets difficult to manage how the state flows.

Another thing I like to use this for is when I have a monster that is heavily built in to some ACS scripts. If you want to use an ACS script change a flag on a monster, for example, but don't want to use SetActorState (and risk interrupting an animation), you can give a CustomInventory item that does it.
Currently reading: Blood Ravens, The Dawn of War Omnibus
_________________
Total approved submissions: 78
Total approved updates: 21
Total rejected: 2


Top
 Profile  
 
 Post subject: Re: CustomInventory Items as New Functions
PostPosted: 28 Feb 2012, 19:12 
Offline
Site Admin  Site Admin
User avatar
Joined: 11 Dec 2009, 18:51
Posts: 4279
Location: Germany
Understandable and useful, I like it :)


Top
 Profile  
 
 Post subject: Re: CustomInventory Items as New Functions
PostPosted: 28 Feb 2012, 19:18 
Offline
Site Admin  Site Admin
Joined: 19 Jan 2010, 00:05
Posts: 1546
Ghastly wrote:
Now, it is also possible to put the A_Chase inside the CustomInventory, and just call it all on one line. I don't really recommend this, though, because I prefer to keep the concepts of what the monster is doing and what the monster is checking (in this case) separate.

But it'd be so much neater... I mean, you'd get this in the actor state list:

Code:
  See:
    TROO AABBCCDD 3 A_GiveInventory("ImpChaseAndWaterCheck", 1) //Execute our new function!
    Loop


This lets you use the "string of frame letters" approach to minimize copy/pasting as much as possible.
I want YOU to read the wiki.
And this one too. Also, SLADE3.


Top
 Profile  
 
 Post subject: Re: CustomInventory Items as New Functions
PostPosted: 28 Feb 2012, 20:49 
Offline
Commander Keen  A rather ghastly dragon
User avatar
Joined: 11 Dec 2009, 20:01
Posts: 2661
Yeah, DavidPH brought that up in IRC when I got some opinions. As I said, I really do prefer to keep the actor's actions and these ambient checks separate.


Top
 Profile  
 
 Post subject: Re: CustomInventory Items as New Functions
PostPosted: 27 Mar 2012, 19:58 
Offline
Commander Keen  A rather ghastly dragon
User avatar
Joined: 11 Dec 2009, 20:01
Posts: 2661
Alright, finally gave in and collapsed A_Chase into the CustomInventory. Can this be approved yet? :P


Top
 Profile  
 
 Post subject: Re: CustomInventory Items as New Functions
PostPosted: 07 Apr 2012, 19:22 
Offline
Icon of Sin  O'Neill with it.
User avatar
Joined: 13 Dec 2009, 20:48
Posts: 1164
Location: IN SPACE
Whoops! Sorry I forgot this. Can't argue with the concept since I use it myself. :P
Douglas Adams wrote:
Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.


Top
 Profile  
 
Display posts from previous:  Sort by  
Moderators: Ghastly, NeuralStunner

Post new topic Reply to topic  Page 1 of 1
 [ 6 posts ] 


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: