Jump to content
The Dark Mod Forums

self-referencing an entity in script - possible ?


motorsep

Recommended Posts

I am wondering if there is a way to reference an entity on which script is running without doing any voodoo with the code.

 

For example, if I run scrip on an entity (via "scriptobject" spawnarg) and I need to check for gui_parmX on the same entity, is there something like $self.getGuiParm(); or this.getGuiParm(); ? (talking about stock Doom 3/idTech4)

 

Thanks.

Link to comment
Share on other sites

That's how an object references itself when running a script, regardless of how the script starts. There are various ways of making this happen, scriptobject being one.

 

What's the other way?

 

I know that running script from worldspawn with "call" wouldn't allow such thing - I have to literally either hardcode $entity_name or find an entity, and get it's name.

Link to comment
Share on other sites

If the code you are writing is sitting inside the scriptobject, you don't need to name the entity.

Other ways of calling code on a script object...

entity myEnt = $door1.getTarget(0);
myEnt.callFunction("doSomething");

The most flexible way is to declare a variable of your script object type, because then you can access all its functions and data members. Example for turning off the player's lantern. This uses TDM's inventory features to get a reference to the lantern (but you can use whatever method in stock Doom3), then it checks whether the lantern's light pointer is null to see if the lantern is on, then calls the "InventoryUse" function on the script object to turn it off:

// If the lantern is on, turn it off.
$player1.setCurInvItem( "#str_02395" );             // lantern
if ( $player1.getCurInvItemName() == "#str_02395" ) // lantern
{
    playertools_lantern lntn = $player1.getCurInvItemEntity();  // playertools_lantern is the type of the script object
    if ( lntn.lanternLight != $null_entity ) // lantern is on
    {
        lntn.inventoryUse( $player1, $null_entity, 0 ); // turn if off properly
    }
}
Link to comment
Share on other sites

You can use:

 

 


self.someFunction();

 

to reference the entities' script object. (The script object and the entity it runs on are the same "thing").

 

Edit: "self." is optional. "someFunction()" does the same. EoE.

 

However, you need to make sure that the script engine knows what type of entity "self" is. It will allow only calls to methods that exist on that specific type of entity. You can "cast" the entity beforehand with:

 

 


your_entity_classtype variablename = self;

variablename.methodName();

 

where methodName() is only available on "your_entity_classtype" but not f.i. "entity".

 

See the Swift script code for some examples – I (ab)used the script code quite heavily in it :)

Edited by Tels

"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." -- George Bernard Shaw (1856 - 1950)

 

"Remember: If the game lets you do it, it's not cheating." -- Xarax

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recent Status Updates

    • Petike the Taffer

      I've finally managed to log in to The Dark Mod Wiki. I'm back in the saddle and before the holidays start in full, I'll be adding a few new FM articles and doing other updates. Written in Stone is already done.
      · 1 reply
    • nbohr1more

      TDM 15th Anniversary Contest is now active! Please declare your participation: https://forums.thedarkmod.com/index.php?/topic/22413-the-dark-mod-15th-anniversary-contest-entry-thread/
       
      · 0 replies
    • JackFarmer

      @TheUnbeholden
      You cannot receive PMs. Could you please be so kind and check your mailbox if it is full (or maybe you switched off the function)?
      · 1 reply
    • OrbWeaver

      I like the new frob highlight but it would nice if it was less "flickery" while moving over objects (especially barred metal doors).
      · 4 replies
    • nbohr1more

      Please vote in the 15th Anniversary Contest Theme Poll
       
      · 0 replies
×
×
  • Create New...