Replies: 4 (Who?), Viewed: 480 times.
Test Subject
Original Poster
#1 Old 29th May 2020 at 4:35 PM Last edited by AnaBelem : 30th May 2020 at 2:52 PM.
Default Python (traits, skills and motives)
Hello,

I'm making a mod and I'm looking on how to do some things in Python. I can access a Sim and get basic properties, but I'm unable to do the following:

  • Test if a Sim has a particular trait.
  • Assign/Remove a particular trait.
  • Get the value of a particular skill.
  • Change the value of a particular skill.
  • Increase/Decrease a particular motive.

Can anyone provide examples or show me where to look? I'm a Python programmer, but I'm having difficulty with the lack of documentation.
Advertisement
Test Subject
Original Poster
#2 Old 30th May 2020 at 2:30 PM Last edited by AnaBelem : 30th May 2020 at 2:52 PM.
Ok, I was able to solve the points related to the traits.

Code:
from sims4.resources import Types, get_resource_key
import services

# First, we get the Sim info we are going to work with.
# In this case, I'm getting the active sim.
client = services.client_manager().get_first_client()
sim_info = client.active_sim.sim_info

# For the trait, first we need to get the manager.
trait_manager = services.get_instance_manager(Types.TRAIT)

# Then we get the trait itself, using the string ID from the XML file.
my_trait = trait_manager.get(get_resource_key("my_string_id", Types.TRAIT))

# This is how we test for the trait.
if sim_info.has_trait(my_trait):
    # This is how we remove the trait from the sim.
    sim_info.remove_trait(my_trait)
else:
    # This is how we add the trait to the sim.
    sim_info.add_trait(my_trait)

I'm still struggling with the rest. I would appreciate any help.
Test Subject
#3 Old 30th May 2020 at 8:28 PM
This thread should help with the motives.

These are the basics, from reading dcrole's tutorial:
Code:
from sims4.resources import Types
from server_commands.argument_helpers import get_tunable_instance

# Get the sim.
sim = services.active_sim_info()

# Get instance and tracker. Use the proper motive name:
#     motive_hunger, motive_social, motive_fun, motive_energy, motive_bladder, motive_hygiene
stat = get_tunable_instance(Types.STATISTIC, 'motive_name', exact_match=True)
tracker = sim.get_tracker(stat)

# Check if you got the tracker.
if tracker is not None:

    # Set to maximum.
    tracker.set_value(stat, tracker.max_value)

    # Set to minimum.
    tracker.set_value(stat, tracker.min_value)

    # Set to absolute value.
    tracker.set_value(stat, value)

    # Set to relative value.
    tracker.set_value(stat, tracker.get_value() + value)
Test Subject
#4 Old 1st Jun 2020 at 5:53 AM
I just noticed an error in my code. It should end with:
Code:
    # Set to maximum.
    tracker.set_value(stat, stat.max_value)
 
    # Set to minimum.
    tracker.set_value(stat, stat.min_value)
 
    # Set to absolute value.
    tracker.set_value(stat, value)
 
    # Set to relative value.
    tracker.set_value(stat, tracker.get_value(stat) + value)


You can get the skills in the same manner you got the traits. This will get its raw value, not the skill level. All ranked statistics have a curve you should apply to get the proper level. I didn't look into applying the curve, but you can use a hack to convert:

Code:
from sims4.resources import Types, get_resource_key
import services

# Get the manager.
stat_manager = services.get_instance_manager(Types.STATISTIC)

# We are going for Fitness, I got the ID from the tuning file.
fitness_instance = stat_manager.get(get_resource_key("16659", Types.STATISTIC))
stat_fitness = sim_info.get_statistic(fitness_instance)

# This is the hack.
fitness_levels = [0, 100, 1540, 3700, 7300, 12580, 19780, 29920, 43360, 60460, 81580]

if stat_fitness is not None:

    # Getting the raw value.
    fitness = stat_fitness.get_value()
    fitness_skill = 1

    # Converting by hand.
    if fitness >= fitness_levels[10]:
        fitness_skill = 10
    elif fitness >= fitness_levels[9]:
        fitness_skill = 9
    elif fitness >= fitness_levels[8]:
        fitness_skill = 8
    elif fitness >= fitness_levels[7]:
        fitness_skill = 7
    elif fitness >= fitness_levels[6]:
        fitness_skill = 6
    elif fitness >= fitness_levels[5]:
        fitness_skill = 5
    elif fitness >= fitness_levels[4]:
        fitness_skill = 4
    elif fitness >= fitness_levels[3]:
        fitness_skill = 3
    elif fitness >= fitness_levels[2]:
        fitness_skill = 2

# Example of setting the value, using raw numbers.
example_num = 2356
stat_fitness.set_value(example_num)
Test Subject
Original Poster
#5 Old 1st Jun 2020 at 9:11 AM
Thanks a lot, it worked perfectly!

About the skill ranks, I did the same hack with the Fame ranks. I couldn't figure out how to get them otherwise. I will look for those curves now, thank you for the tip.
Back to top