A
/ \ _ Play Now Nemesis on
| | | | _______ _ _ _______ _______ _ _______
| |\ | | | ____| | \ / | | ____| / ____| | | / ____|
/-------/-------------------------------------------------------------------,
O= Home <=XX| About News Pics Adventurers Wizards Download Connect >
\-------\-------------------------------------------------------------------'
| | \ | | |____ | | | | | |____ ___ \ \ | | ___ \ \
| | \_| |_______| |_| |_| |_______| |_______/ |_| |_______/
\ /
V
Indirection (Poe, Nov 23 1993, 48)
At times I like to bring in new concepts for people who either didn't know
about them, or never really understood what was happening when they used them.
Indirection is a very useful way of accessing modules of code, objects, and
variables in memory. First, I should talk about the concept of pointers.
Without knowing, most LPC coders use pointers all the time. Pointers are
variables which "point" at areas in memory (primary or secondary). They are
very useful to people who know how to use them.
Consider that you have a variable called 'bopeep' of type integer.
Somewhere in memory, the computer has allocated a few bytes to store this. It
happens that in Nemesis it's 4 bytes. If you want to use the number in that
memory location you use the variable name 'bopeep'.
For example: printf("%d\n", bopeep);
Every variable has a numerical address in memory. There is a specific
operator you can use to find the address of the variable, here's how:
&bopeep
The '&' (address of) operator when added to the beginning of a variable
makes the statement take on the numerical value of the address. This in itself
is not very useful. But this is where pointers come in.
Consider that you have a variable and you want to pass it to a function
which will change the variable without having to use a return value... Well, in
that case you need a pointer. To use a pointer first you need to declare it
like any other variable. If we want a pointer to bopeep we need to declare a
pointer of the same type, integer.
Example: int *sheep;
This declares a pointer with the name sheep, but we haven't assigned
any value to it yet... right now it's just pointing off somewhere (who knows).
Now we need to assign the sheep to bopeep ;).
We can do: sheep = &bopeep;
In the example above we are assigning a pointer 'sheep' to the memory
location of bopeep. Sheep is "pointing at" bopeep.
Example: *sheep = 1;
"That which is pointed to by sheep gets 1." This places the value 1
into the memory location being pointed to by sheep. Since sheep is pointing at
bopeep, the variable bopeep now has the value of 1.
In LPC we have objects. Objects are simply pointers to places in memory
where programs are loaded. Since we have all these functions, we don't usually
realize that they are pointers. The functions take care of the dereferencing
for us. Similarly, strings are actually arrays of characters. The name of a
string is a pointer to the first member of the array, but strings can also be
accessed as an array. Functions like printf take a pointer and when they go to
display the information, they print out each member of the array until they come
to a NULL character that marks the end.
Since objects are pointers to loaded programs, we can use a special
operator to access functions in them. The '->' member access operator, tells
the computer to look into the object and call the function.
Example: this_player()->query_name();
This tells the computer to look for the object called this_player() and
do the function query_name() in that loaded program.
The member access operator can also be used with a string file name.
This comes in handy when an object hasn't been loaded into memory. If the
object hasn't been compiled and loaded, the computer loads it and calls the
function in it.
Example: "players/poe/object/sheep"->baa();
I hope you have the chance to use some of these concepts in your coding,
they've been very useful to me.
>>-Poe-> Chris.
P.S. (Admins) Please feel free to correct me if I've stated anything wrong.
This board is to document how to code various stuff in Nemesis.
Within Nemesis it is located in the inner guild in the village.
If you have written documents that should be placed here or
if you think something important is missing please contact an
Archwizard - see 'help admin' or https://nemesis.de/lpc/help/admin .
Nowadays especially new features etc. are posted here.
The board contains 49 notes:
- living (Lynx, Sep 19 1991, 28)
- short descriptions (Lynx, Sep 19 1991, 28)
- building guilds (Lynx, Sep 19 1991, 28)
- Ok. prompts (Lynx, Sep 20 1991, 28)
- Yell command (Lynx, Sep 21 1991, 28)
- simulated efuns (Lynx, Sep 22 1991, 28; Kiri, Jan 28 2017, 62)
- Re: short descriptions -> inventory display (Lynx, Sep 24 1991, 28)
- ships (Junky, Sep 30 1991, 30; revised by Kiri, Jun 4 2016, 62)
- introducing a new quest (Lynx, Oct 8 1991, 28)
- storing data in the player (Lynx, Oct 8 1991, 28)
- setting a user-definable prompt (Snake, Jan 6 1992, 66)
- Test Players (Junky, Jan 9 1992, 40; revised by Kiri, Jul 27 2016, 62)
- the colourful magazine (Snake, Feb 13 1992, 66; Kiri, Jun 4 2016, 62)
- new room support: room2.h (Junky, Mar 27 1992, 55)
- Skills (Junky, Mar 27 1992, 55)
- Indent within ed. (Lynx, Jul 25 1992, 49)
- Re: Indent within ed. (Poe, Aug 4 1992, 41)
- Properties (Junky, Aug 25 1992, 55)
- Timedependent objects (Junky, Sep 11 1992, 55)
- lfun produce (Lynx, Feb 9 1993, 60)
- ED editor F.Y.I. (Poe, Mar 7 1993, 45)
- conditional operator (Poe, Aug 13 1993, 46)
- Indirection (Poe, Nov 23 1993, 48)
- lfun search for all objects (Kiri, Jun 1 2016, 62)
- obj/monster: set_alias & set_brave (Kiri, Jun 4 2016, 62)
- Re: simulated efuns (Kiri, Jun 4 2016, 62)
- all "dead" (non-living) objects: query_name (Kiri, Jun 7 2016, 62)
- obj/monster: second_life supports argument (Kiri, Jun 10 2016, 62)
- obj/postoffice: generic post office (Kiri, Jun 11 2016, 62)
- obj/living: room property P_NO_MAGIC finally! (Kiri, Jun 12 2016, 62)
- simul_efun: valid_id & living: find_id (Kiri, Jul 17 2016, 62)
- obj/sign: improved (Kiri, Jul 18 2016, 62)
- obj/item: the replacement of obj/treasure (Kiri, Jul 19 2016, 62)
- obj/text: improved (Kiri, Jul 19 2016, 62)
- obj/key: improved (Kiri, Jul 19 2016, 62)
- obj/map: ready for persistence (Kiri, Jul 19 2016, 62)
- obj/treasure: legacy but persistent (Kiri, Jul 26 2016, 62)
- obj/chest & obj/bag: improved & persistent (Kiri, Jul 27 2016, 62)
- wiz command: localcmd (Kiri, Aug 12 2016, 62)
- obj/monster: set_cap_name, query_name & a hack (Kiri, Aug 12 2016, 62)
- obj/door2: a new door system (Kiri, Aug 12 2016, 62)
- room2.h: tutorial revised / changes & fixes (Kiri, Aug 12 2016, 62)
- keeping track of object versions (Kiri, Aug 17 2016, 62)
- About Colours (Kiri, Sep 17 2016, 62)
- clean_up: a garbage collection! (Kiri, Dec 27 2016, 62)
- containers: query_put_prep (Kiri, Jun 15 2017, 62)
- Re: obj/board: changes (Kiri, Jun 16 2017, 62)
- new 'more' via obj/more (Kiri, Sep 13 2017, 62)
- Re: New & Changed [Wiz] Commands (Kiri, Sep 26 2017, 62)
This page was generated in LPC
Imprint / Impressum