Jump to content


Photo

Script error: Element exists but not assigned

xedit script

Best Answer zilav , 14 December 2017 - 02:08 AM

Assigned() returns a boolean value. Scripts try to convert all values to strings when you print them, however you really should work with it as bool

if Assigned(_aidt) then
  AddMessage('Element exists');

Also RNAM is not a name, it is a signature. If you are accessing elements by signature then use ElementBySignature. Or just always use ElementByPath which works with both.

Go to the full post


  • Please log in to reply
3 replies to this topic

#1 FWDekker

FWDekker

    Prisoner

  • Members
  • 2 posts

Posted 14 December 2017 - 01:23 AM

I'm trying to write a script that will export particular data from an NPC to a file for further processing. However, the program exhibits strange behaviour when I try to access some of the NPC's elements. Getting simple fields and printing them as strings works fine, but it goes wrong as soon as I try to get non-string elements.

 

In particular, I have the following script:

{
    Export list of records
    ------------------------
    Hotkey: Ctrl+Alt+Shift+E
}
unit UserScript;

const sRecordsToSkip = 'REFR,PGRD,PHZD,ACHR,NAVM,NAVI,LAND';


function Initialize: integer;
begin
    // Do nothing
end;


function Process(e: IInterface): integer;
var
    _aidt     : IwbElement;
    _assigned : string;
begin
    if Pos(Signature(e), sRecordsToSkip) <> 0 then begin
        Exit;
    end;

    if not ElementExists(e, 'AIDT') then begin
        Exit;
    end;

    _aidt := ElementByName(e, 'RNAM');
    _assigned := Assigned(_aidt);
    AddMessage('Is assigned? ' + _assigned);
end;


function Finalize: integer;
begin
    // Do nothing
end;


end.

Whenever I run this script, the message "Is assigned? False" is printed. I do not understand how this is possible: I checked whether the element existed, but after getting it, it is Nil.

 

What's going wrong here? Any help is appreciated.


Edited by FWDekker, 14 December 2017 - 01:24 AM.

  • 0

#2 zilav

zilav

    Commander

  • Mod Authors
  • PipPipPipPip
  • 333 posts

Posted 14 December 2017 - 02:08 AM   Best Answer

Assigned() returns a boolean value. Scripts try to convert all values to strings when you print them, however you really should work with it as bool

if Assigned(_aidt) then
  AddMessage('Element exists');

Also RNAM is not a name, it is a signature. If you are accessing elements by signature then use ElementBySignature. Or just always use ElementByPath which works with both.


  • 1

#3 FWDekker

FWDekker

    Prisoner

  • Members
  • 2 posts

Posted 14 December 2017 - 12:39 PM

you really should work with it as bool

I know, I just had some problems with type conversion and decided to temporarily go for this hacky approach.

 

Also RNAM is not a name, it is a signature.

In which case I think the documentation is wrong in saying that ElementExists "[r]eturns true if aeContainer [h]as a child element whose name is asName" (emphasis mine), since it returned true when I asked about the signature RNAM.

 

 

Regardless, using ElementBySignature or ElementByPath seems to do what I want. Thanks!


Edited by FWDekker, 14 December 2017 - 12:40 PM.

  • 0

#4 zilav

zilav

    Commander

  • Mod Authors
  • PipPipPipPip
  • 333 posts

Posted 15 December 2017 - 02:07 AM

I'm not the author of that documentation.

Also Name is that you see in xEdit in the leftmost column, for example "RNAM - Race" is a name, simply "RNAM" is a signature. Some elements don't have signatures like "Leveled List Entries" and can be accessed by name or path only.


  • 1



Also tagged with one or more of these keywords: xedit, script

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users