• Please note that we've updated the Mount & Blade II: Bannerlord save file system which requires you to take certain steps in order for your save files to be compatible with e1.7.1 and any later updates. You can find the instructions here.

WB Coding python code doesn't work

Users who are viewing this thread


the store random number generator in mount and blade has too many duplicate 'random' numbers
here is my code defined in module constants using python
def randgennodups():
    import random
    output = random.sample(range(10), 1)
    output1 = output[0]
    return output1

("init_town_walkers_bugged", ##too many duplicate "randoms" MB has a ****ty random generator
(try_for_range, ":walker_no", 0, num_town_walkers),
(store_add, ":troop_slot", slot_center_walker_0_troop, ":walker_no"),
(party_get_slot, ":walker_troop_id", "$current_town", ":troop_slot"),
(gt, ":walker_troop_id", 0),
(store_add, ":entry_no", town_walker_entries_start, ":walker_no"),
(store_random_in_range, ":walkers", walker_templates_begin, walker_templates_end),
(set_visitor, ":entry_no", ":walkers"),
(try_for_range, ":walker", walker_templates_begin, walker_templates_end),
(assign, reg1, randgennodups()),(store_random_in_range, reg2, 0, 24),(store_random_in_range, reg3, 5, 15),(store_random_in_range, reg4, 0, 7),
(val_mod, reg1, 10),
(store_random_in_range, reg5, 0, 7),(store_random_in_range, reg6, 0, 7),(store_random_in_range, reg7, 0, 7),
(store_random_in_range, reg8, 0, 7),(store_random_in_range, reg9, 0, 7),
(str_store_troop_face_keys, s1, "trp_player"),
(face_keys_set_hair, s1, reg1),(face_keys_set_beard, s1, reg2),(face_keys_set_age, s1, 0),(face_keys_set_face_texture, s1, reg3),
(face_keys_set_morph_key, s1, 1, reg4),
(face_keys_set_morph_key, s1, 2, reg5),
(face_keys_set_morph_key, s1, 3, reg6),
(face_keys_set_morph_key, s1, 4, reg7),
(face_keys_set_morph_key, s1, 5, reg:cool:,
(face_keys_set_morph_key, s1, 6, reg9),
(face_keys_set_morph_key, s1, 7, reg4),
(face_keys_set_morph_key, s1, 8, reg4),
(troop_set_face_keys, ":walker", s1, 0),
(store_random_in_range, ":clothing", unisex_clothing_start, unisex_clothing_end),
(store_random_in_range, ":clothingf", female_clothing_start, female_clothing_end),
(troop_get_type, ":gender", ":walker"),
(eq, ":gender", 1),
(troop_add_item, ":walker", ":clothingf"),
(eq, ":gender", 0),
(troop_add_item, ":walker", ":clothing"),

however in game i see the same hair everytime


You cannot use python syntax in MBScript.
You can try doing that at compiling-level, i can't help you with that.
Upvote 0
M&B engine's RNG apparently is odd, but produces even values. It is strange.

Random = Random * 0x343fd + 0x269ec3;
listIndex = (RandomGlobal >> 0x10 & 0x7fff) % listLen);
Upvote 0


Sergeant at Arms
This is how I fixed psevdorandom equipment of agents. When mission starts the random generator aparently uses mission timer as seed for random generator. Each time you enter to the scene random will generate the same sequence. It always uses 0 as time for starting random sequence. So I have just shifted the sequence of random numbers from (0 to 99) wich gives more random results in equipment. I think this also can help for face randomisation. Well at the beginning this code I used for random reequipment and sudenly it changes all randomness.

1) Add this trigger at the begining of each mission.
(ti_on_agent_spawn, 0, ti_once,[],[
    # Procedure to fix psevdorandom of troops equipment
    (try_for_range, ":i", 0, "$random_seed"), 
      (store_random_in_range, reg10, 0, 2),

2) Add this code for each mission end possibilty. This can take some time but it is worth it.
(store_mission_timer_a_msec, "$random_seed"),
(val_mod, "$random_seed", 100),

In the global map I don't think that random can duplicate sequences because random uses time as seed.
Well the first spawned agent will not be randomised. Maybe reequip him.
Last edited:
Upvote 0
Top Bottom