Jump to content
  • 0

Script error: Element exists but not assigned


FWDekker

Question

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
Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

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 1
Link to comment
Share on other sites

  • 0

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
Link to comment
Share on other sites

  • 0

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 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Guidelines, Privacy Policy, and Terms of Use.