Skip to content

SetGender feature in Toolset

So changing gender via script doesn't seem possible, could we perhaps at some time in the future have a SetGender(0) and (1)? Would open up some interesting stuff for onspawn randomizing whether a random guard or commoner should be male or female.

Comments

  • ProlericProleric Member Posts: 1,316
    For now, this can be done by selecting a random template at create / spawn time.
  • NeverwinterWightsNeverwinterWights Member Posts: 339
    Proleric wrote: »
    For now, this can be done by selecting a random template at create / spawn time.

    Sadly this is still a problem when using spawn systems that rely on specific templates/resrefs. An issue I've run into myself.
  • BaireswolfBaireswolf Member Posts: 22
    I think the new JSON function might give us a workaround for this as long as we use a custom spawn system.

    Or I might be totally wrong.

    B.
  • ProlericProleric Member Posts: 1,316
    edited September 2021
    On paper, this should work - there are functions for

    Template to JSON
    Change a value by key in the JSON
    JSON to object at the desired location

    Haven't tried it yet, but in theory any field in an object can be changed without having a specific script function (a bit like having access to a GFF editor from a script), providing you don't mind making a new object before deleting the old one.
  • BaireswolfBaireswolf Member Posts: 22
    That's exactly what I was thinking. If it works as is, it will do for NESS changing the spawning function cause you pass de ResRef... Don't know how to do it with the vainilla spawn system through.
  • ProlericProleric Member Posts: 1,316
    edited September 2021
    Here is a working script:
    ///* JSON TEST
    
    #include "nw_inc_gff"
    
    void main()
    {
    json   jNPC = TemplateToJson("x3_hummount001", RESTYPE_UTC);
    object oNPC_Final;
    
    jNPC       = GffReplaceByte(jNPC, "Gender", GENDER_FEMALE);
    jNPC       = GffReplaceWord(jNPC, "FactionID", STANDARD_FACTION_COMMONER);
    oNPC_Final = JsonToObject (jNPC, GetLocation(GetFirstPC()));
    ChangeToStandardFaction(oNPC_Final, STANDARD_FACTION_COMMONER);
    }
    //*/
    
    I chose Human Rider as the template so that anyone can reproduce this without custom content.

    The GFF function and keyword to be used can be determined by opening the UTC with GFF Editor.

    The only thing I don't understand is that although the change to FactionID seems to work (as reported by JsonDump), the NPC still spawns as hostile - hence ChangeToStandardFaction.
    Post edited by Proleric on
  • ProlericProleric Member Posts: 1,316
    This seems to have huge potential for spawning random NPCs.

    In the past, the process was very buggy in my experience - changing body parts, colour, phenotype, clothing etc with sundry ad hoc function calls needed a lot of tweaking to avoid invisible or misaligned parts.

    In principle, what we can do now is make all those changes in the JSON prior to spawning, which, in theory, ought to be rendered by the engine exactly like a fixed template.

    Time will tell.
  • KamirynKamiryn Member Posts: 74
    Proleric wrote: »
    The only thing I don't understand is that although the change to FactionID seems to work (as reported by JsonDump), the NPC still spawns as hostile - hence ChangeToStandardFaction.

    If you compare the jsons before and after ChangeToStandardFaction() you’ll see that FactionID has changed from 1 to 2.

    Setting FactionID to 2 right away would create a non-hostile NPC.

  • BaireswolfBaireswolf Member Posts: 22
    Perhaps I'm missing it badly, but where can I find a list of GFF related function.

    I'm thinking of reading appereance & gender. If app is one of the playeable type and gender is set to both apply a gender change. Doing so I can set bandits to apply a gender change based on a random check, but bypass that for example in a Drow priestess or a non playeable race that lacks female models.
  • ProlericProleric Member Posts: 1,316
    edited September 2021
    @Baireswolf
    #include "nw_inc_gff"
    
    I updated the example to show that explicitly.

    The functions are not in the Lexicon yet, but they are referenced in Lexicon Json function examples.

    Once included, you can see a list in the toolset by filtering on GFF.

    Usage is pretty intuitive if you're used to working with GFF files.
  • ProlericProleric Member Posts: 1,316
    edited September 2021
    Regarding faction ID, Daz provided an explanation in the Vault forum:
    The actual engine constants for the standard factions are 1 higher than the nwscript ones.

    Engine:
    STANDARD_FACTION_HOSTILE = 1;
    STANDARD_FACTION_COMMONER = 2;
    STANDARD_FACTION_MERCHANT = 3;
    STANDARD_FACTION_DEFENDER = 4;

    Nwscript:
    STANDARD_FACTION_HOSTILE = 0;
    STANDARD_FACTION_COMMONER = 1;
    STANDARD_FACTION_MERCHANT = 2;
    STANDARD_FACTION_DEFENDER = 3;

    You’d use the engine constants for any gff related editing.

    Hopefully, this is a unusual(?) case - in my experience, GFF fields normally have the same values as NWScript.
  • BaireswolfBaireswolf Member Posts: 22
    Proleric wrote: »
    @Baireswolf
    #include "nw_inc_gff"
    
    I updated the example to show that explicitly.

    The functions are not in the Lexicon yet, but they are referenced in Lexicon Json function examples.

    Once included, you can see a list in the toolset by filtering on GFF.

    Usage is pretty intuitive if you're used to working with GFF files.

    Also I realized I could use the JSONpointer fnc to get the "Gender" value once I got it jsonized. The same goes for the other check I mentioned.
  • ProlericProleric Member Posts: 1,316
    Proleric wrote: »
    This seems to have huge potential for spawning random NPCs.

    Verified - see example.

  • WinterniteWinternite Member Posts: 11
    Thanks for the replies and assistance.

    Gonna have to try that script out, I have no knowledge of json and how it would even work in NWN, but sufficient to say adding this:
    json   jNPC = TemplateToJson("x3_hummount001", RESTYPE_UTC);
    object oNPC_Final;
    
    jNPC       = GffReplaceByte(jNPC, "Gender", GENDER_FEMALE);
    jNPC       = GffReplaceWord(jNPC, "FactionID", STANDARD_FACTION_COMMONER);
    oNPC_Final = JsonToObject (jNPC, GetLocation(GetFirstPC()));
    ChangeToStandardFaction(oNPC_Final, STANDARD_FACTION_COMMONER);
    

    To onspawn of a creature should determine its gender? Looking for a 50% chance to be either one or the other.
  • ProlericProleric Member Posts: 1,316
    That works - see example linked in my last post for a more complete example. The gender setting is trivial, of course:
    int nGender     = GENDER_MALE;
    if (Random(2)) nGender = GENDER_FEMALE;
    jNPC       = GffReplaceByte(jNPC, "Gender", nGender);
    
    Earlier in the thread I quoted an explanation by Daz about "FactionID". If you set it to (STANDARD_FACTION_COMMONER + 1) in the JSON, you don't need ChangeToStandardFaction() afterwards.
  • TarotRedhandTarotRedhand Member Posts: 1,481
    edited January 2022
    @Proleric Alternatively you could replace the first 2 lines of your snippet with -
    int nGender = (Random(2) ? GENDER_FEMALE : GENDER_MALE);
    

    TR
Sign In or Register to comment.