- Nov 11, 2024
- 52
- 104
- 33
[edit :
Right after posting this, I had an idea and it solved my problem.
Sorry ^^]
Hi everyone,
A question for Python programmers.
I'm having trouble with some Python code for my Renpy game. In the game, the female character has outfits that are divided into tops, bottoms, and lingerie.
I'm trying to code a function that will modify the character's stats based on the outfit she's wearing.
The “wardrobe” system works very well. I can change and remove outfits without any problems. The stats vary correctly and it works in 90% of cases.
But I have one case (and so I imagine there could be others) where when I reset the outfits, there is a ghost +1 added to a stat.
This happens when I put on outfits that affect the same variable, one positively and one negatively.
In addition, it is important to note that the values that are modified go through another function that generates a visual effect when a stat is modified.
Furthermore, some stats have a clamp when they fall below 0.
I also tried to program the outfits (top and bottom) that are worn at the same time so that their stats are combined before being applied to the character.
Otherwise, if I applied one and then the other, the clamps could obviously create undesirable side effects. (For example, if the stat was 0, and I put on an outfit that removes 1 and then one that adds 2, I wanted it to be 0+(-1+2) and not 1-1+2, which would not give the same result with the clamp at 0).
And finally, the function is supposed to store the actual applied value (and not the theoretical value) to take the clamps into account.
Here is my code:
default top_current_effect = {}
default bottom_current_effect = {}
default top_effects = {
"blue_pull": {},
"red_pull": {"lust": -1, "slut": 1},
"green_pull": {"study": 1, "submission": -2},
"croptop_pink": {"lust": 2, "karma": -1},
"croptop_marine": {"lust": 2, "slut": -2},
"croptop_white": {"lust": 2, "study": -1},
"croptop_blue": {"lust": 1},
}
default bottom_effects = {
"pants_beige": {},
"pants_yellow": {"study": 1, "slut": -2},
"pants_white": {"submission": 1, "lust": -1},
"pants_blue": {"slut": -2, "karma": 1},
"short_blue": {"lust": 2},
"short_red": {"slut": 1},
"short_white": {"submission": 1},
}
init python:
def _apply_stat_change(stat, delta):
if delta == 0:
return 0
if stat == "study":
return update_study(delta)
elif stat == "lust":
return update_lust(delta)
elif stat == "submission":
return update_submission(delta)
elif stat == "karma":
return update_karma(delta)
else:
import renpy.store as S
cur = getattr(S, stat, 0)
setattr(S, stat, cur + delta)
return delta
# TOP + BOTTOM
def apply_top_bottom_effects(new_top, new_bottom):
global selected_top, selected_bottom
global top_current_effect, bottom_current_effect
changes = []
for stat, applied_value in top_current_effect.items():
if applied_value != 0:
removed = _apply_stat_change(stat, -applied_value)
changes.append(f"REMOVE TOP {stat}: {applied_value} (applied={removed})")
top_current_effect = {}
for stat, applied_value in bottom_current_effect.items():
if applied_value != 0:
removed = _apply_stat_change(stat, -applied_value)
changes.append(f"REMOVE BOTTOM {stat}: {applied_value} (applied={removed})")
bottom_current_effect = {}
eff_top = top_effects.get(new_top, {})
for stat, value in eff_top.items():
applied = _apply_stat_change(stat, value)
if applied != 0:
top_current_effect[stat] = applied
changes.append(f"APPLY TOP {stat}: {value} (applied={applied})")
eff_bottom = bottom_effects.get(new_bottom, {})
for stat, value in eff_bottom.items():
applied = _apply_stat_change(stat, value)
if applied != 0:
bottom_current_effect[stat] = applied
changes.append(f"APPLY BOTTOM {stat}: {value} (applied={applied})")
selected_top = new_top
selected_bottom = new_bottom
if changes:
renpy.notify("\n".join(changes))
#####Stats function
init python:
def update_karma(change):
global karma
before = karma
karma += change
applied = karma - before
renpy.hide("karmaup")
renpy.hide("karmadown")
if applied > 0:
renpy.show("karmaup", at_list=[move_up_and_disappear])
elif applied < 0:
renpy.show("karmadown", at_list=[move_down_and_disappear])
return applied
def update_submission(change):
global submission
old_val = submission
submission += change
if submission < 0:
submission = 0
applied = submission - old_val
renpy.hide("submissionup")
renpy.hide("submissiondown")
if applied > 0:
renpy.show("submissionup", at_list=[move_up_and_disappear])
elif applied < 0:
renpy.show("submissiondown", at_list=[move_down_and_disappear])
return applied
def update_lust(change):
global lust
before = lust
lust += change
if lust < 0:
lust = 0
applied = lust - before
renpy.hide("lustup")
renpy.hide("lustdown")
if applied > 0:
renpy.show("lustup", at_list=[move_up_and_disappear])
elif applied < 0:
renpy.show("lustdown", at_list=[move_down_and_disappear])
return applied
def update_study(change):
global study
before = study
study += change
if study < 0:
study = 0
applied = study - before
renpy.hide("studyup")
renpy.hide("studydown")
if applied > 0:
renpy.show("studyup", at_list=[move_up_and_disappear])
elif applied < 0:
renpy.show("studydown", at_list=[move_down_and_disappear])
return applied
-----------------------
The problem is when I do croptop_pink + pants_white (this works, I get the right values returned).
Then when I change the pants to pants_yellow for example but keep the top, the game creates a ghost +1 on the lust stat :/
I added notify to verify that the game is taking the correct values into account, and they are returning the correct values.
I'm far from being a Python expert (I'm a real noob). I rely heavily on AI for coding, but I have to admit that I can't find the problem here (and I've made quite a few versions of this function :/).
Anyway, if anyone can help me, that would be really cool.
Best regards and thank you for your time,
Paul
Right after posting this, I had an idea and it solved my problem.
Sorry ^^]
Hi everyone,
A question for Python programmers.
I'm having trouble with some Python code for my Renpy game. In the game, the female character has outfits that are divided into tops, bottoms, and lingerie.
I'm trying to code a function that will modify the character's stats based on the outfit she's wearing.
The “wardrobe” system works very well. I can change and remove outfits without any problems. The stats vary correctly and it works in 90% of cases.
But I have one case (and so I imagine there could be others) where when I reset the outfits, there is a ghost +1 added to a stat.
This happens when I put on outfits that affect the same variable, one positively and one negatively.
In addition, it is important to note that the values that are modified go through another function that generates a visual effect when a stat is modified.
Furthermore, some stats have a clamp when they fall below 0.
I also tried to program the outfits (top and bottom) that are worn at the same time so that their stats are combined before being applied to the character.
Otherwise, if I applied one and then the other, the clamps could obviously create undesirable side effects. (For example, if the stat was 0, and I put on an outfit that removes 1 and then one that adds 2, I wanted it to be 0+(-1+2) and not 1-1+2, which would not give the same result with the clamp at 0).
And finally, the function is supposed to store the actual applied value (and not the theoretical value) to take the clamps into account.
Here is my code:
default top_current_effect = {}
default bottom_current_effect = {}
default top_effects = {
"blue_pull": {},
"red_pull": {"lust": -1, "slut": 1},
"green_pull": {"study": 1, "submission": -2},
"croptop_pink": {"lust": 2, "karma": -1},
"croptop_marine": {"lust": 2, "slut": -2},
"croptop_white": {"lust": 2, "study": -1},
"croptop_blue": {"lust": 1},
}
default bottom_effects = {
"pants_beige": {},
"pants_yellow": {"study": 1, "slut": -2},
"pants_white": {"submission": 1, "lust": -1},
"pants_blue": {"slut": -2, "karma": 1},
"short_blue": {"lust": 2},
"short_red": {"slut": 1},
"short_white": {"submission": 1},
}
init python:
def _apply_stat_change(stat, delta):
if delta == 0:
return 0
if stat == "study":
return update_study(delta)
elif stat == "lust":
return update_lust(delta)
elif stat == "submission":
return update_submission(delta)
elif stat == "karma":
return update_karma(delta)
else:
import renpy.store as S
cur = getattr(S, stat, 0)
setattr(S, stat, cur + delta)
return delta
# TOP + BOTTOM
def apply_top_bottom_effects(new_top, new_bottom):
global selected_top, selected_bottom
global top_current_effect, bottom_current_effect
changes = []
for stat, applied_value in top_current_effect.items():
if applied_value != 0:
removed = _apply_stat_change(stat, -applied_value)
changes.append(f"REMOVE TOP {stat}: {applied_value} (applied={removed})")
top_current_effect = {}
for stat, applied_value in bottom_current_effect.items():
if applied_value != 0:
removed = _apply_stat_change(stat, -applied_value)
changes.append(f"REMOVE BOTTOM {stat}: {applied_value} (applied={removed})")
bottom_current_effect = {}
eff_top = top_effects.get(new_top, {})
for stat, value in eff_top.items():
applied = _apply_stat_change(stat, value)
if applied != 0:
top_current_effect[stat] = applied
changes.append(f"APPLY TOP {stat}: {value} (applied={applied})")
eff_bottom = bottom_effects.get(new_bottom, {})
for stat, value in eff_bottom.items():
applied = _apply_stat_change(stat, value)
if applied != 0:
bottom_current_effect[stat] = applied
changes.append(f"APPLY BOTTOM {stat}: {value} (applied={applied})")
selected_top = new_top
selected_bottom = new_bottom
if changes:
renpy.notify("\n".join(changes))
#####Stats function
init python:
def update_karma(change):
global karma
before = karma
karma += change
applied = karma - before
renpy.hide("karmaup")
renpy.hide("karmadown")
if applied > 0:
renpy.show("karmaup", at_list=[move_up_and_disappear])
elif applied < 0:
renpy.show("karmadown", at_list=[move_down_and_disappear])
return applied
def update_submission(change):
global submission
old_val = submission
submission += change
if submission < 0:
submission = 0
applied = submission - old_val
renpy.hide("submissionup")
renpy.hide("submissiondown")
if applied > 0:
renpy.show("submissionup", at_list=[move_up_and_disappear])
elif applied < 0:
renpy.show("submissiondown", at_list=[move_down_and_disappear])
return applied
def update_lust(change):
global lust
before = lust
lust += change
if lust < 0:
lust = 0
applied = lust - before
renpy.hide("lustup")
renpy.hide("lustdown")
if applied > 0:
renpy.show("lustup", at_list=[move_up_and_disappear])
elif applied < 0:
renpy.show("lustdown", at_list=[move_down_and_disappear])
return applied
def update_study(change):
global study
before = study
study += change
if study < 0:
study = 0
applied = study - before
renpy.hide("studyup")
renpy.hide("studydown")
if applied > 0:
renpy.show("studyup", at_list=[move_up_and_disappear])
elif applied < 0:
renpy.show("studydown", at_list=[move_down_and_disappear])
return applied
-----------------------
The problem is when I do croptop_pink + pants_white (this works, I get the right values returned).
Then when I change the pants to pants_yellow for example but keep the top, the game creates a ghost +1 on the lust stat :/
I added notify to verify that the game is taking the correct values into account, and they are returning the correct values.
I'm far from being a Python expert (I'm a real noob). I rely heavily on AI for coding, but I have to admit that I can't find the problem here (and I've made quite a few versions of this function :/).
Anyway, if anyone can help me, that would be really cool.
Best regards and thank you for your time,
Paul
Last edited: