QB CULT MAGAZINE
Issue #5 - July-October 2000

RPG Tutorial - Object Systems

By QbProgger <qbprogger@tekscode.com>

If you are new to RPG's this article is not about how to make a tile engine so close this damned browser right now. Yeah, you heard me, this isn't for new people, so close this now. Yup.

The hardest thing that RPG makers face nowadays is the object system. The object system is everything from a soda can to the guy who sells magazines behind the counter in the dirty book store (yeah, we all know you put one in your game, don't be shy ;)). Anyways, I see awful examples of such an object system every day of my life. I see NPC's that just sit there...and don't do anything. I see NPC's that are incapable of nothing more than a random movement and message. I see objects that are limited to their original script. I see many things that are horrible and should be eliminated from each and every engine on this planet.

The first thing that a person should do if they want to make a complete RPG is make a complete and totally functional object system. A functional and complete object system will allow NPC's to walk through doors, activate scripts, be controlled, hold items, talk, and drop items. Instead of having one character and other "npcs" which are controlled through some other routine, we should put all of the objects and the character on the same exact level. Here's an example.

(Psuedo code, do not copy into your program or it will screw up )

SUB UpdateNPCS

FOR uNF=1 to ActiveNPCS
    SELECT CASE uNF
        CASE CurrentlyControlledCharacter
            MoveNPC uNF, Keyboardismovinghorizontal, Keyboardismovingvertical
        CASE ELSE
            SELECT CASE ObjectData(uNF)
                CASE RandomlyMoves
                    MoveNPC uNF, RandomPlaceX, RandomPlaceY
                CASE IsMovingToAPoint
                    MoveNPC uNF, ObjectPointToMoveTo(uNf,0), ObjectPointToMoveTo(uNF,1)
                CASE IsStandingStill
                    AnimateNPC uNF
            END SELECT
    END SELECT
NEXT

END SUB

SUB MoveNPC Number,MoveToX,MoveToY

Test=Map(MoveToX,MoveToY)

SELECT CASE Test
    CASE Walkable
        ObjectLocation(Number,0)=MoveToX:ObjectLocation(number,1)=MoveToY
    CASE IsAScriptTile
        ActivateScript
END SELECT

END SUB

(End of psuedo code)

With this example (you REALLY need to change it to suit your own coding style) you can EASILY make moving npcs and npcs that can do everything from activate scripts to whatever. And, to control a different object, just change the CurrentlyControlledCharacter variable. This allows a person to update over 200 npcs in a VERY short time, causing a very professional look to your object system. Also, in the SELECT CASE ObjectData(unf) you can make it so that objectdata(5) (NPC #5) is a value of 3. Let's say that an NPC with the attribute of 3 goes after the player and tries to attack him. All you have to do is add in code under there and it's nice, neat, and organized and will work very quickly without sacrificing code speed. Now you can have differently behaving NPCs in your engine, which will impress your players =].

Not so hard, eh? I bet you didn't think that a totally efficient and active object system would be so short. Now that you have this out of the way, you can add things like an items list for each NPC (watch your RAM usage!!). Then, you could possibly trade and sell items to every NPC.