Skip to content

switch/case

I'm trying to make a special wandering monster table, and am uncertain how to proceed.
void WanderingMonsters()
    {
    object oPC;
    object oTarget;    object oSpawn;
    location lTarget;  oTarget = oPC;
    lTarget = GetLocation(oTarget);
    oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "resref", lTarget);
    }

void main()
{

   switch ()
{
case 0:
    
     break;
case 1:
     // statements ...
     break;
case 2:
     // statements ...
     break;
default:
     // statements ...
     break;
}
}

This is all I have. I just don't know how to structure the switch/case thingy or if a custom function is the right approach? I want each case to be a different critter???

Comments

  • ForSeriousForSerious Member Posts: 446
    You are on the right track.
    Change WanderingMonsters to accept a string:
    void WanderingMonsters(string sResRef)
    //...
    oSpawn = CreateObject(OBJECT_TYPE_CREATURE, sResRef, lTarget);
    
    Add Random to your switch case.
    // Random(4) will spit out an integer 0-3, unlike d4() that spits out 1-4
    switch(Random(4))
    
    In your cases, list all the ResRefs you want:
    case 1: WanderingMonsters("monsterresref"); break;
    

    You'll need to define oPC. Let me know if you need help with that. Depending on where this script is being called from, it will be different.
  • ProlericProleric Member Posts: 1,282
    The Lexicon is the go-to site for scripting info, e.g.
    https://nwnlexicon.com/index.php?title=Switch_case_Statement
  • ZephiriusZephirius Member Posts: 411
    edited February 2021
    Proleric wrote: »
    The Lexicon is the go-to site for scripting info, e.g.
    https://nwnlexicon.com/index.php?title=Switch_case_Statement

    Yeah, I went to that exact page before I posted and didn't fully understand what I was looking at.
  • ZephiriusZephirius Member Posts: 411
    Thanks for the help ForSerious and Proleric. ;) The script is firing on all cylinders...
  • ZephiriusZephirius Member Posts: 411
    I couldn't help it. I'm elated!
    I took what ForSerious taught me and made my own custom function. It works well I might add. :)
    I know it's not the shiniest script in the code-verse, but I'm proud of it.

    It's just for a goblin encampment - background noise sort of thing.
    object oPC = GetEnteringObject();
    
    void GoblinChatter(string sSound)
    {
       AssignCommand(oPC, PlaySound(sSound));
    }
    
    void main()
    {
    
    if (!GetIsPC(oPC)) return;
    
        switch(Random(8))
        {
        case 0: GoblinChatter("c_goblin_atk1"); break;
        case 1: GoblinChatter("c_goblin_atk2"); break;
        case 2: GoblinChatter("c_goblin_atk3"); break;
        case 3: GoblinChatter("c_goblin_bat1"); break;
        case 4: GoblinChatter("c_goblin_bat2"); break;
        case 5: GoblinChatter("c_goblinchf_bat1"); break;
        case 6: GoblinChatter("c_goblinwiz_bat1"); break;
        case 7: GoblinChatter("c_goblin_slct"); break;
       }
    }
    
    
    

    Thanks again guys. I am a slow learner, but I do learn. :)

  • ForSeriousForSerious Member Posts: 446
    You are most welcome! I'm glad you got it working. I don't know about you, but I love it when my scripts finally work like I wanted them to. It's a great feeling.
  • ZephiriusZephirius Member Posts: 411
    ForSerious wrote: »
    You are most welcome! I'm glad you got it working. I don't know about you, but I love it when my scripts finally work like I wanted them to. It's a great feeling.

    Indeed.
  • TarotRedhandTarotRedhand Member Posts: 1,481
    If you want to try a different tutorial on switch/case try my TR's Basics - Decisions. I think you may well find it easier to understand. If you look at the Related Projects directly below the download there are links to the other volumes in the series too.

    BTW there is a downloadable version of the Lexicon too but it doesn't cover the new functions introduced by the EE and is PC only. On the other hand it is so much easier and faster to seach.

    TR
  • ZephiriusZephirius Member Posts: 411
    Thanks. I'll give it a look-see.
Sign In or Register to comment.