![]() |
The MVC model!
To make a good NPC system (not merely a stand-alone project) requires a lot of practice. I myself tend to make several versions of the system: I personally have made more than a dozen MUD systems -- each time I was finished, I'd scratch it and started anew. The reasons were simple: As time went on, the complexity of the script becomes too high to actually progress any further.
There is a way to limit complexity, however! The MVC model is based on modularizing your compartments in your scripts. This approach dictates how you should go about. As a way to show this, I'll be displaying my Item system that I've honed. There are three compartments to any system. The (M)odel, the (V)isual, and the (C)ontrol. The Model is the system logic, in this example, the core item system... In simple terms, the primitive functions that work on a system. The Visuals, such as the inventory, communicates with the Control to get answers from the Model or other Visuals ( if need be ). In this example, we'll focus on the Model <=> Control <=> Visual. In no instance does the visual and model communicate directly! The Model and Visual are meant to be portable: You can change the system which they work with and still maintain them without problems. The Control, however, usually needs some reworking ( To fix the links between the model and visuals ). We'll start by the foundations of the system -- The Model. In the Item system, it becomes pretty straightforward. To design the system, you would first ask what methods that the system would have. Suggestively, I would go by this: PHP Code:
To decide the primitives, I personally like the system where you have your dynamic variables and static variables separated. In this system, the items themselves don't change ( this minimizes the memory required to run the system ). What I prefer, in this instance, is using player.clientr.items to define the item and its quantity, and player.clientr.item_[ITEMID] to define the item information. An easy way to get this to work would be having a multi-dimensional array for the item information, thus, {itemID, QUANT}. Alternatively, you could have more information: {itemID, QUANT, STATE, POSITION, FORSALE }... Something that I've been starting to do with these systems, especially for the item information, is using a mapped array. I define mapped array as an array of arrays whose structure is {key, value}, in this case... {{"itemID", itemID}, {"Quantity", QUANT}, {"State", STATE}, ...} How this information is stored, however, is up to you. For sakes of minimizing the memory footprint, I'll use {itemID, quant}. Once you have decided on the structure of the data, it is now time to do the nitty-gritty. This 'tutorial' isn't based on how you should proceed, so I won't go through why it is made as such: PHP Code:
So... The Control... The control in this scenario is based on the clientside, but this could work just as well on the serverside. In my Item control, I don't want the items to be manipulated. First, I would decide what the control would allow us to get... For simplicity sakes, I've decided on this control: PHP Code:
So... To start, I'm going to work on what I know from my Model. The values that I have access to SHOULD ONLY BE ACCESSED HERE. In no instance should my Visual interact DIRECTLY with the values. For instance, instead of returning {itemID, QUANT, STATE, POSITION, etc }, I'll have functions that are designed to return each of the properties INDEPENDANTLY. This is vital to diminish the complexity for when you change the system ( add a new property, or convert the system to one that requires more information ). PHP Code:
PHP Code:
I hope this helps! If you want other non-technical help on scripting, I have posted guidelines on how to increase productivity here: http://forums.graalonline.com/forums...ad.php?t=77413 |
I dislike your styling, but I can tell these scripts might be useful for someone in need of a simple, yet effective, basic item system.
|
Quote:
|
Although I never made it official, I guess i've always coded like this: easy to upgrade the system the base-system without screwing over everything else ^^
|
Quote:
That point being made though, neither of our comments had a place in this thread :) |
I just read in my Computing handbook about the importance of modularisation. It's great to see how it'd be done in GScript :)
|
Modularization is good, but its made a bit harder by not being able to use catchevent() for dynamic events.
|
My bad xP
|
Looks good to me ;)
|
| All times are GMT +2. The time now is 02:03 PM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.