PDA

View Full Version : Control-NPC


upsilon
07-17-2006, 08:07 AM
I've noticed some weird behavior from the Control-NPC script. I was playing around, testing out what is and isnt saved to the npc database ,by setting up some objects in Control-NPC's onCreated event. heres the code:

in the Control-NPC script...


function onCreated() {
sendtorc("Control-NPC has been reset");
this.testobj = new TGraalVar();
this.testobj.join("test");
}



in the 'test' class' script...


function onCreated() {
sendtorc("new test object created...");
}


Whats happening, is on the first reset, the message in Control-NPC's onCreated event is printed, then when test class is joined, the message in its onCreated event is printed aswell.. which makes sense.

Now...

On subsequent resets, the message in Control-NPC's onCreated event is still printed, but the one in the test class is not. If i add a new variable to Control-NPC, with a different name than before, joining the test class to it triggers the test classes onCreated event again, but like in the first case, it isnt triggered on subsequent resets.

Why? How does this make any sense? I was thinking it would be a good place to put recovery code, for when the npc server is restarted (in Control-NPC's onInitialized() event), or for setting things into motion that dont logically make any sense being tied to when a level is loaded, a player connects, or a player enters a level. But i dont see how this is possible.

I understand that Control-NPC is usually used to detect when a projectile lands, or when a player connects to the server. But what is its purpose in general? and how does it benifit from this behavior?

Someone told me NEVER EVER to reset Control-NPC, but i don't understand how i could do that, when its reset everytime its edited, and there are legitimate reasons for editing it.

Omini
07-17-2006, 12:29 PM
I think it's because the other test class script, because it isn't being re-created like the Control-NPC, it doesn't go through those strings again.

Novo
07-17-2006, 12:52 PM
I learnt that when you update a script, the objects within aren't destroyed. That is... The Database NPC will still point to the right object.

This problem could be when you restart NC that the classes themselves are restarted... And the trigger isn't reached by the classes by the time it is created. ( the classes are loading, and aren't in the cache yet).

upsilon
07-17-2006, 04:28 PM
Novo, i dont think it is though. On the first restart, the the trigger is always reached. its only subsequent restarts. I do understand this behavior somewhat, the only problem is, that there is no way that i can think of to actually clear the Control-NPC's local variables, or even just disassociate the object from Control-NPC and re-use the variable for a new instance of the same class. Its weird how it works. If you later change the class you are joining to the object in the variable,the new classes onCreated is run. I suspect its actually just joining the old object in that variable to the new class, which doesnt seem like a good default. As the code that joined it to the first class, no longer exists.

I guess what im really confused about is object lifetime. When do they start, and when do they stop? Its not really clear to me from just following the control flow of the script

Novo
07-17-2006, 04:33 PM
They start when they are declared.

They stop when no variables point to them.

I have found the reason why it didn't work.


this.testobj = new TGraalVar();


Umm... Try new TStaticVar();

TGraalVar is a number, string, array... Therefore, when you join a class, and assign the variable to another of TGraalVar, it doesn't destroy the variable. TGraalVar's attitude when assigned to another TGraalVar is that it copies the value of the other TGraalVar, rather than assigning the the variable to the new object.

This means that when you join the class a second time, the class is already there. The object didn't change, and so the object is still in the class. Because no additional memory was allocated for that specific object, there is no Created trigger.

upsilon
07-17-2006, 05:23 PM
Oh, ok. so in reality... Control-NPC retains the variables, but if its reset, the onCreated event will still be triggered, and the variables will likely be overwritten. I was just using the wrong base class. :whatever:

Got it. So i would have to check
if (!this.whatever.initialized) {
this.whatever = something;
}
or possibly just remove/comment out the initialization code for Control-NPC local variables, to keep editing or resetting the script from overwritting them. This makes sense.

thanks allot Novo. ^^