Hi there! You are currently browsing as a guest. Why not create an account? Then you get less ads, can thank creators, post feedback, keep a list of your favourites, and more!
Test Subject
Original Poster
#1 Old 24th Nov 2017 at 3:57 AM
Default Adding and removing traits in script
I'm trying to learn how to modify Sims through Python scripts. Some of Scumbumbo's mods are giving me information I want, but I can't for the life of me figure out how to add or remove traits (not creating NEW ones, adding or removing EXISTING ones). I'm sure it's really obvious and I'll kick myself when it's revealed, but nothing I've tried has worked so far (doesn't help when searches I attempt for adding and removing traits, no matter what keywords I use, get bombarded with a list of in-game cheat codes instead of scripting lines.)

In fact, is there an easy way to see everything in a sim_info object? You'd think this would be documented somewhere, but I'm having trouble finding it.
Advertisement
Deceased
#2 Old 25th Nov 2017 at 10:04 AM
Quote: Originally posted by HeroicJay
In fact, is there an easy way to see everything in a sim_info object? You'd think this would be documented somewhere, but I'm having trouble finding it.

There used to be a way to do that, but it was no longer working except in my game where I'd fixed it earlier this month. Since I think it fits what you want to do like a glove, I've uploaded a newer version, including a couple of sample "probes" - the first two demonstrating some of the things you'll want to look at.

http://modthesims.info/showthread.php?t=549155
Test Subject
Original Poster
#3 Old 26th Nov 2017 at 6:59 AM Last edited by HeroicJay : 26th Nov 2017 at 7:29 AM.
Promising, but all it seems to accomplish when I try to load a game with it installed is a crash. When I look at the exception file generated, it makes reference to a missing module named "ScumLog".

EDIT: Wait, I missed that the Enable Debug Cheats mod was out-of-date. I'll try fixing that. FURTHER EDIT: Makes the game stop crashing. The exceptions regarding the lack of ScumLog still exist, and the data dumping options do not appear. STILL FURTHER EDIT: As "ScumLog" does not appear to actually be used anywhere, I commented it out... only for "injector" now to be throwing the same exception. It IS used.
Deceased
#4 Old 26th Nov 2017 at 10:07 AM Last edited by scumbumbo : 26th Nov 2017 at 10:18 AM.
Doh! Yes, just remove the line that references ScumLog - that's a module on my own computer I use for logging things but is not used by the tool itself it was just used to test some things I played around with while fixing it up.

I've updated the download to clean up the imports and added the injector module as well in case you don't have a copy.
Test Subject
Original Poster
#5 Old 26th Nov 2017 at 10:42 AM
Okay, now this is giving me the info I need, at least after activating the sim_info probe. Thanks!

That "Cosmic Gender Ray" mod by hugs proved to be surprisingly useful to my original question as well, as it modifies traits.
Deceased
#6 Old 26th Nov 2017 at 10:55 AM
Quote: Originally posted by HeroicJay
Okay, now this is giving me the info I need, at least after activating the sim_info probe. Thanks!

That "Cosmic Gender Ray" mod by hugs proved to be surprisingly useful to my original question as well, as it modifies traits.

Awesome! Good luck with your project. Yeah, traits shouldn't be at all difficult to add. I've added buffs in code and it should be fairly similar.
Deceased
#7 Old 27th Nov 2017 at 1:33 PM
In order to update the change gender script for my Change Sim Name or Gender mod I found that I had to swap some traits around in order to modify the gender preferences. So if you haven't yet had time to figure out how to add/remove traits yet check out the gender change module from that mod to see. Pretty simple, just get the instance for the trait tuning and call the sim_info's remove_trait or add_trait method.
Test Subject
#8 Old 30th Nov 2017 at 8:06 PM
Quote: Originally posted by scumbumbo
In order to update the change gender script for my Change Sim Name or Gender mod I found that I had to swap some traits around in order to modify the gender preferences. So if you haven't yet had time to figure out how to add/remove traits yet check out the gender change module from that mod to see. Pretty simple, just get the instance for the trait tuning and call the sim_info's remove_trait or add_trait method.


It sounds like what you are saying is that adding and removing traits can be done through tuning? If that's correct could you please be so kind as to give an example of that. it would be most helpful to a project of my own.
Deceased
#9 Old 1st Dec 2017 at 9:48 AM
Quote: Originally posted by FrkTrunte
It sounds like what you are saying is that adding and removing traits can be done through tuning? If that's correct could you please be so kind as to give an example of that. it would be most helpful to a project of my own.

I assume you mean that I'm saying traits can be added/removed from a script, as that is what we're discussing. Adding/removing a trait from the XML tuning via a loot action is the documented method for doing so.

So here's a simple but fun example script to demonstrate adding/removing a trait via the Python scripting. This is NOT a full mod and could cause issues with your game (although I don't think it would unless you used it to add the trait to a sim that already had a conflicting trait). Still, I wouldn't suggest saving your game after using it without first researching whether it could cause issues. This is just for a fun example!

The first command, ghost adds the trait_ghost_Anger trait to the active Sim. The second, unghost removes it.
Code:
from sims4.resources import Types, get_resource_key
import sims4.commands
import services

TRAIT_GHOST_ANGER = 101679

@sims4.commands.Command('ghost', command_type=sims4.commands.CommandType.Live)
def add_trait(_connection=None):
    # Get the console output
    output = sims4.commands.CheatOutput(_connection)

    # Get the sim_info for the active sim
    sim_info = services.client_manager().get(_connection).active_sim.sim_info

    # Get the tuned trait instance from the tuning instance manager
    instance_manager = services.get_instance_manager(Types.TRAIT)
    trait_ghost_anger = instance_manager.get(get_resource_key(TRAIT_GHOST_ANGER, Types.TRAIT))

    # Check if our sim already has that trait
    if sim_info.has_trait(trait_ghost_anger):
        output('Sim is already a Death By Anger ghost')
    else:
        # The sim doesn't have that trait, so add it
        sim_info.add_trait(trait_ghost_anger)
        output('Sim is now a ghost')

@sims4.commands.Command('unghost', command_type=sims4.commands.CommandType.Live)
def remove_trait(_connection=None):
    output = sims4.commands.CheatOutput(_connection)
    sim_info = services.client_manager().get(_connection).active_sim.sim_info
    instance_manager = services.get_instance_manager(Types.TRAIT)
    trait_ghost_anger = instance_manager.get(get_resource_key(TRAIT_GHOST_ANGER, Types.TRAIT))
    if not sim_info.has_trait(trait_ghost_anger):
        output('Sim is not a Death By Anger ghost')
    else:
        sim_info.remove_trait(trait_ghost_anger)
        output('Sim is no longer a ghost')
Back to top