BudeRoy Posted January 5, 2016 Share Posted January 5, 2016 Heya, i need some help with my LUA code here. I'am pretty newbie with the LUA, like "write some code, test it out, cry & retry until it works." I have created new profession 'Soldier' and i wanted to randomly generate army title for the character.local function soldier_start(player, square) local profession = player:getDescriptor():getProfession(); if profession == 'soldier' then getPlayer():getDescriptor():setForename("Sgt.");Code above works perfectly! When i enter into game with my Soldier profession, it'll show my character name as Sgt. Porter for example. But.. I want to add more titles to be randomly picked up when entering game, like Sgt. Pvt. & Cpt. so you can be example Sgt. Porter or Pvt.Porter etc. so you'll never know what your "military rank" will be, even it doesn't matter anyhow. What's the function here to make it pick one from many title options? I'am officially out of ideas here after 4 hours of struggling, any help would be greatly appreciated! Link to comment Share on other sites More sharing options...
ethanwdp Posted January 6, 2016 Share Posted January 6, 2016 I've tackled with something like this, where I needed to have randomization. Since tables are able to hold information as numbered entries, I just had a random number generator pull out an entry based on the number generated. Example: Table has 5entries, 1,2,3,4, and 5.. RNG generates 3, so I use the third entry. This should do the trick:titleTable = {"Pvt. ","Sgt. ","Cpt. "}local function GRN(argTable) local randNumber = ZombRandBetween(1, #argTable) -- sets the maximum generated number to the size of the given table. Example: fooTable has 2 entries, so the maximum number is 2. return argTable[randNumber]endlocal function soldier_start(player, square) local profession = player:getDescriptor():getProfession(); if profession == 'soldier' then getPlayer():getDescriptor():setForename( GRN(titleTable) ); endendsoldier_start(getPlayer()) BudeRoy 1 Link to comment Share on other sites More sharing options...
BudeRoy Posted January 6, 2016 Author Share Posted January 6, 2016 I tested couple of times to create new character and hallelujah it works! Many thanks for pointing that out and helping me! You saved my day and now i can continue my project ethanwdp 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now