ID:150846
 
okay, i have this:

mob/player/proc
thing()
world << "thing"


how do i call thing from within a global proc? i tried, usr.thing(), src.thing(), and thing(). but none of em will work. i understan dwhy, but is there a way to call it?


btw, these aren't real proc names and the such in my game. just a demo.
On 7/18/01 2:26 pm XgavinX wrote:
okay, i have this:

mob/player/proc
thing()
world << "thing"


how do i call thing from within a global proc? i tried, usr.thing(), src.thing(), and thing(). but none of em will work. i understan dwhy, but is there a way to call it?


btw, these aren't real proc names and the such in my game. just a demo.

The problem here is that the compiler is not certain that the proc thing() will always be available for a given usr.

There are two ways to approach this. You can make sure there is a thing() proc defined directly under mob/ (just define it with no instructions in it), and override the definition under mob/player. The other approach is to use usr:thing() [note the colon in place of a period]. This way the compiler only checks to see if any type of usr has that proc, and allows it, leaving potential errors for misuse in the programmer's hands.
In response to Skysaw
On 7/18/01 2:31 pm Skysaw wrote:
On 7/18/01 2:26 pm XgavinX wrote:
okay, i have this:

mob/player/proc
thing()
world << "thing"


how do i call thing from within a global proc? i tried, usr.thing(), src.thing(), and thing(). but none of em will work. i understan dwhy, but is there a way to call it?


btw, these aren't real proc names and the such in my game. just a demo.

The problem here is that the compiler is not certain that the proc thing() will always be available for a given usr.

There are two ways to approach this. You can make sure there is a thing() proc defined directly under mob/ (just define it with no instructions in it), and override the definition under mob/player. The other approach is to use usr:thing() [note the colon in place of a period]. This way the compiler only checks to see if any type of usr has that proc, and allows it, leaving potential errors for misuse in the programmer's hands.


okay, thanks. i'll see what i can do.
On 7/18/01 2:26 pm XgavinX wrote:
mob/player/proc
thing()
world << "thing"


how do i call thing from within a global proc? i tried, usr.thing(), src.thing(), and thing(). but none of em will work. i understan dwhy, but is there a way to call it?

You have to have a mob/player variable, or instance, to call it on. For example:

proc/Dummy()
var/mob/player/myplayer = new
myplayer.thing()

Probably not what you wanted to do, though, because that creates a whole new player mob. But the principle is the same. You probably can't call usr.thing() because by default, usr is of type /mob, which doesn't have a proc/thing() defined. There are two ways to get around this (looks like Skysaw already beat me to it on this point, heh):
  • define an empty proc/thing() under mob. Then by definition, usr will always have thing() defined and usr.thing() will be valid. If usr happens to be a /mob/player, then it will call /mob/player/proc/thing() which is what you wanted.
  • or type it as usr:thing() and hope for the best. This tells the compiler not to check if proc/thing() really exists for usr (a /mob by default), so it will accept it. However, the colon notation can get you into trouble at runtime. If you have another mob class, say /mob/goodplayer, which doesn't have proc/thing() defined, you'll get an error at runtime when you try to call it.

Additionally, if you want to keep track of a certain player globally, and that player happens to be a /mob/player, you can call thing() on that variable. For example:

var/mob/player/kewlplayer

proc/KewlThing()
if (kewlplayer)
kewlplayer.thing()

mob/player/verb/getkewl()
src << "You are the kewlplayer!"
kewlplayer = src

And so on. I hope that made a little sense!
In response to Air Mapster
On 7/18/01 2:40 pm Air Mapster wrote:
On 7/18/01 2:26 pm XgavinX wrote:
mob/player/proc
thing()
world << "thing"


how do i call thing from within a global proc? i tried, usr.thing(), src.thing(), and thing(). but none of em will work. i understan dwhy, but is there a way to call it?

You have to have a mob/player variable, or instance, to call it on. For example:

proc/Dummy()
var/mob/player/myplayer = new
myplayer.thing()

Probably not what you wanted to do, though, because that creates a whole new player mob. But the principle is the same. You probably can't call usr.thing() because by default, usr is of type /mob, which doesn't have a proc/thing() defined. There are two ways to get around this (looks like Skysaw already beat me to it on this point, heh):
  • define an empty proc/thing() under mob. Then by definition, usr will always have thing() defined and usr.thing() will be valid. If usr happens to be a /mob/player, then it will call /mob/player/proc/thing() which is what you wanted.
  • or type it as usr:thing() and hope for the best. This tells the compiler not to check if proc/thing() really exists for usr (a /mob by default), so it will accept it. However, the colon notation can get you into trouble at runtime. If you have another mob class, say /mob/goodplayer, which doesn't have proc/thing() defined, you'll get an error at runtime when you try to call it.

Additionally, if you want to keep track of a certain player globally, and that player happens to be a /mob/player, you can call thing() on that variable. For example:

var/mob/player/kewlplayer

proc/KewlThing()
if (kewlplayer)
kewlplayer.thing()

mob/player/verb/getkewl()
src << "You are the kewlplayer!"
kewlplayer = src

And so on. I hope that made a little sense!


usr:thing() seems to be working alright for me. thanks.
In response to XgavinX
On 7/18/01 2:44 pm XgavinX wrote:
usr:thing() seems to be working alright for me. thanks.

usr and the colon operator in one statement...Air, you have done a bad thing here!
In response to Deadron
On 7/18/01 5:37 pm Deadron wrote:
On 7/18/01 2:44 pm XgavinX wrote:
usr:thing() seems to be working alright for me. thanks.

usr and the colon operator in one statement...Air, you have done a bad thing here!

Yes, perhaps I should have emphasized the danger a little more (but I did mention it). Bad Me.

By the way, is there a FAQ or tutorial on such perils? I see a section on usr in the FAQ which discusses alternatives and avoidance, but nothing on colon. Perhaps that's my cue to write something up in my copious (cough) amounts of free time... It would be nice to have something to point to, though.
In response to Air Mapster
On 7/18/01 5:51 pm Air Mapster wrote:
By the way, is there a FAQ or tutorial on such perils? I see a section on usr in the FAQ which discusses alternatives and avoidance, but nothing on colon. Perhaps that's my cue to write something up in my copious (cough) amounts of free time... It would be nice to have something to point to, though.

Go for it! If there is a discussion of colon operators that isn't filled with swear-words, I must not have written it.
In response to Air Mapster
On 7/18/01 5:51 pm Air Mapster wrote:

By the way, is there a FAQ or tutorial on such perils? I see a section on usr in the FAQ which discusses alternatives and avoidance, but nothing on colon. Perhaps that's my cue to write something up in my copious (cough) amounts of free time... It would be nice to have something to point to, though.

Ooh, I believe at one point I wrote a little expose on the dangers of using the colon. Perhaps I should email it to Deadron so he can stick it in the FAQ.