[SOLVED] Help with code please

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
Andwiiger
Newbie
Posts: 14
Joined: Thu Oct 26, 2017 12:35 pm
Contact:

[SOLVED] Help with code please

#1 Post by Andwiiger »

I'm trying to make a salary system for a Visual Novel. The way I want it to work is that if the player works 1 hour then they get a set amount of money, but if they work more than 1 hour then they get a bonus. I've gotten it to work by entering it manually each time, but I would like to get a class to do it instead so it's handled automatically. The code gives me syntax errors on line 6 ( $ Bonus = (0.2*hourpay) ) and on line 22 ($ hours = 1). Could someone please give me an indication as to what's wrong and explain how I should write it? Thanks in advance.

This is the code I have:

init python:
class Salary:
def __init__(self, hours = 0, hourpay = 10):
self.hours = hours
self.hourpay = hourpay
$ Bonus = (0.2*hourpay)

def add(self):
if (hours == 1):
$ Salary = hourpay
else:
$ Salary = hourpay + (Bonus*(hours-1))


label start:

$ salary = 0
$ hours = 0
$ hourpay = 10

python:
$ hours = 1
money = Salary()

money.add()

if (hours == 1):
"You have %(salary)d after working %(hours)d hour"
else:
"You have %(salary)d after working %(hours)d hours"
Last edited by Andwiiger on Sat May 26, 2018 5:42 pm, edited 1 time in total.

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Help with code please

#2 Post by kivik »

I think you've misunderstood what the $ is for, it's used for single line of python in your renpy code, not to set global variables.

That said, there're other problems in your code, so I've made a different version:

Code: Select all

init python:
    class Salary:
        def __init__(self, money, hourpay = 10, bonus = 0.2):
            self.money = money
            self.hourpay = hourpay
            self.bonus = 1 + bonus

        def pay(self, hours):
            pay = self.hourpay * hours if hours <= 1 else self.hourpay + self.bonus * self.hourpay * (hours - 1)
            self.money += pay
            return pay

default salary = Salary(0)

label start:
    call pay(1)
    call pay(3)
    return

label pay(hours):
    $ pay = salary.pay(hours)
    "You worked [hours] hour(s) and received $[pay], giving you a total of $[salary.money]."
There're lots of weird things you've done that's hard to get into, including variable scopes, object variables, variable types etc. Let me know if my code makes sense to you.

P.S. use the [code] tags to wrap your code so it gets formatted on the forum :)

Andwiiger
Newbie
Posts: 14
Joined: Thu Oct 26, 2017 12:35 pm
Contact:

Re: Help with code please

#3 Post by Andwiiger »

Kivik: Your code makes alot of sense. I understand it and I've managed to use it the way I want it. Two little things I want to ask.
Firstly I would like to change it so that the player get an increased bonus for each hour. Meaning that if they work 3 hours they get something like 10$ (1st hour) + 12$ (second hour 10$ + 20% bonus) and then 14$ (third hour 10$ + 40% bonus) giving a total of 36$.

Secondly could you please explain how I can overwrite the variables later, so that another job could have and hourpay of like 50 or 100?

Thanks in advance

sculpteur
Veteran
Posts: 312
Joined: Fri Nov 17, 2017 6:40 pm
Completed: Apocalypse Lovers
Projects: Apocalypse Lovers
Organization: Awake_Production
Location: France
Discord: https://discord.gg/apocalypse-lovers
Contact:

Re: Help with code please

#4 Post by sculpteur »

For your second question, if you want to change the hourpay for another job, you will have to modify this line :
def __init__(self, money, hourpay = 10, bonus = 0.2):

and replace hourpay = 10 by hourpay = an_other_variable

and you will just have to modify your variable when your job will change.
Image

“He asked me to calm down, close my eyes and be quiet. He explained to me that if I was afraid, the shadow that ran barefoot in the street would feel it. I got scared seeing Jumanji on TV, so let me tell you, we didn't stay hidden for long and had to start running again.”
Jessica's Diary.

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Help with code please

#5 Post by kivik »

Ok if the bonus increase is accumulative: i.e. 0.2 * (hour - 1) * wage for each hour, then the easiest thing is to create a loop and increment both the total money and the accumulative percentage:

Code: Select all

    class Salary:
        def __init__(self, money, hourpay = 10, bonus = 0.2):
            self.money = money
            self.hourpay = hourpay
            self.bonus = bonus # changed this so it's no longer 1 + bonus

        def pay(self, hours):
            pay = 0
            for i in range(0, hours):
                pay += self.hourpay + self.bonus * i * self.hourpay
            self.money += pay
            return pay
Just to explain how it works a little: for i in range(0, hours) will loop through as many times as the hours you've worked, with i carrying values 0, 1, 2, 3... to (hours - 1). This works perfectly in our scenario because the first i value is 0 - making it 0 bonus. Then we multiply the bonus (0.2) with i (which is always looped hour - 1) giving us 0, 0.2, 0.4, 0.6 etc. for each hour - and multiply that by the hourly wage. Add it to the temp variable pay, and we have our total pay.

There's probably some clever maths function that can do the same thing in a single line, but why do that when we have loops?

If you have some other rules, then you need to clarify - as the accumulation gets a bit crazy after few hours!


Second question we need to expand on sculpteur's answer a bit, as there're two things that needs to happen:
1 - we need to move the player money outside the Salary class and store it as a global variable, or a player attribute
2 - we need to create different jobs with different hourly wages

Code: Select all

init python:
    class Job: #changed class name to be more suitable
        def __init__(self, name, hourpay, bonus = 0.2):
            self.name = name # maybe give the job a name?
            self.hourpay = hourpay # you now HAVE to supply the hourpay as we're expecting different jobs
            self.bonus = bonus

        def pay(self, hours):
            pay = 0
            for i in range(0, hours):
                pay += self.hourpay + self.bonus * i * self.hourpay
            return pay

default money = 0
default jobs = {
    "cleaning": Job("Cleaning", 10),
    "office": Job("Office Work", 15)
}

label start:
    call pay(jobs["cleaning"], 1)
    call pay(jobs["cleaning"], 3)
    call pay(jobs["office"], 1)
    call pay(jobs["office"], 3)
    return

label pay(job, hours):
    $ pay = job.pay(hours)
    $ money += pay
    "You worked [hours] hour(s) and received $[pay], giving you a total of $[money]."
This is the beauty of object orientation - you can have as many jobs you want with different attributes and work out the pay accordingly.

I've used a python dictionary to store all the jobs, it's an easy way to reference a job by name later. If you want to add another job later on, you just need to do:

Code: Select all

$ jobs["job_name"] = Job("Job Title", 30, 0.1)
Let me know if that makes sense and if you have any more questions :)

Andwiiger
Newbie
Posts: 14
Joined: Thu Oct 26, 2017 12:35 pm
Contact:

Re: Help with code please

#6 Post by Andwiiger »

Sculpteur and Kivik: Thank you both for your answers.

Kivik: It makes perfect sense. Thanks alot for explaining how it works, I can follow along and understand what the code does. Your code was easy to understand, so I could make a few changes without causing problems.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Help with code please

#7 Post by Remix »

Just for info:

pay = sum( [ hourpay * (1.0 + (n * bonus)) for n in range( hours ) ] )
Frameworks & Scriptlets:

Post Reply

Who is online

Users browsing this forum: Google [Bot]