[Solved] Voting for someone based on a percentage?

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
Rekoija
Newbie
Posts: 15
Joined: Wed Nov 20, 2019 3:19 pm
Contact:

[Solved] Voting for someone based on a percentage?

#1 Post by Rekoija » Wed Nov 20, 2019 3:34 pm

okay so, i have 4 characters that can be voted for

let’s just call them A, B, C and D
the player must convince other people to vote for someone specific.
$ votea = 0
$ voteb = 0
$ votec = 0
$ voted = 0
player “You should vote for B”
$ voteb += 1
throughout the whole thing different characters will get or lose points, and lastly the player will choose someone to vote for which will (as an example, i don’t know the actual values yet)
$ votec += 5

so now let’s say the values are
votea: 1
voteb: 3
votec: 5
voted: 1
now i want renpy to “randomly” choose which one of these got voted for
so there’s a
10 % chance A gets voted,
30 % chance B gets voted,
50 % chance C gets voted and
10 % chance D gets voted

and whoever gets picked it will jump to their label. jump VotedA

i have no idea how to set up this thing, despite looking at a bunch of different posts about it.
i probably just didn’t understand what any of it meant… i found one where they said how to have the different options, but then it didn’t show what to do with those options, so i messed around with it for an hour before i had to give up, and that’s before i even changed the value from an actual number to the $ votea = 0 things...

any contribution would be much appreciated!
Last edited by Rekoija on Sat Jan 18, 2020 5:27 pm, edited 2 times in total.

User avatar
Per K Grok
Miko-Class Veteran
Posts: 743
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1 & 2
Projects: the Phantom Flier
itch: per-k-grok
Location: Sverige
Contact:

Re: Voting for someone based on a percentage?

#2 Post by Per K Grok » Wed Nov 20, 2019 6:15 pm

Rekoija wrote:
Wed Nov 20, 2019 3:34 pm
okay so, i have 4 characters that can be voted for

let’s just call them A, B, C and D
the player must convince other people to vote for someone specific.
$ votea = 0
$ voteb = 0
$ votec = 0
$ voted = 0
player “You should vote for B”
$ voteb += 1
throughout the whole thing different characters will get or lose points, and lastly the player will choose someone to vote for which will (as an example, i don’t know the actual values yet)
$ votec += 5

so now let’s say the values are
votea: 1
voteb: 3
votec: 5
voted: 1
now i want renpy to “randomly” choose which one of these got voted for
so there’s a
10 % chance A gets voted,
30 % chance B gets voted,
50 % chance C gets voted and
10 % chance D gets voted

and whoever gets picked it will jump to their label. jump VotedA

i have no idea how to set up this thing, despite looking at a bunch of different posts about it.
i probably just didn’t understand what any of it meant… i found one where they said how to have the different options, but then it didn’t show what to do with those options, so i messed around with it for an hour before i had to give up, and that’s before i even changed the value from an actual number to the $ votea = 0 things...

any contribution would be much appreciated!


A little something I quickly put together. Should work if you don't set negative vote value for any of the candidates.


Code: Select all

default A=0
default B=0
default C=0
default D=0

default E=0

default WINNER= "..."

screen voting():
    vbox:
        text "A:s points: " + str(A)
        text "B:s points: " + str(B)
        text "C:s points: " + str(C)
        text "D:s points: " + str(D)

    vbox:
        xpos 100
        textbutton "+A" action SetVariable("A", A+1)
        textbutton "-A" action SetVariable("A", A-1)
        textbutton "+B" action SetVariable("B", B+1)
        textbutton "-B" action SetVariable("B", B-1)
        textbutton "+C" action SetVariable("C", C+1)
        textbutton "-C" action SetVariable("C", C-1)
        textbutton "+D" action SetVariable("D", D+1)
        textbutton "-D" action SetVariable("D", D-1)

    vbox:
        xpos 150
        textbutton "Vote" action Jump("vote")
        text "The winner is " + WINNER
        text str(E)

label start:
    show screen voting

    $ renpy.pause(hard=True)


label vote:

    $ E=renpy.random.randint(1,  A+B+C+D )

    if E<=A and A>0:
        $ WINNER="A"
    elif E<=A+B and B>0:
        $ WINNER="B"
    elif E<=A+B+C  and C>0:
        $ WINNER="C"
    else:
        $ WINNER="D"

    jump start

Rekoija
Newbie
Posts: 15
Joined: Wed Nov 20, 2019 3:19 pm
Contact:

Re: Voting for someone based on a percentage?

#3 Post by Rekoija » Wed Nov 20, 2019 9:18 pm

Hey, I really appreciate this, and although it doesn't work at all, now I know what other information i should probably give to actually find the solution to my problem (my bad, really though, thank you for even trying at all :D)

okay so, you've set up all this stuff that shows us how many points people have gotten. the player isn't supposed to know that at all :o
it's a secret, you're pretty much just saying stuff and not knowing that people care what you've said

their vote points should be equal to the $ things i've made, so like
a = votea (votea being the $ votea = 0 that i put in my init)

i have a character announcing the winner like this;
name "And the winner is... (insert winnner name)", in a normal dialog box rather than the announcement text thing you made (honestly i went into this without knowing how much info i actually needed to give out, so that's entirely on me)

i don't know how to make it jump to my labels from finding the winner >.<''
if winner is A i'd like it to jump to label awon, ect.

again, thank you so much for your contribution! :D

GNVE
Regular
Posts: 37
Joined: Wed Sep 12, 2018 4:11 pm
Completed: ShSt - Bad Day
Projects: ShSt - Afterparty, Collings University
itch: https://gnve.itch.io
Contact:

Re: Voting for someone based on a percentage?

#4 Post by GNVE » Thu Nov 21, 2019 2:59 pm

There are a couple of ways to do this. The easiest one I can think of is:

Code: Select all

python early:
    votes = [] #a list of votes is recorded here

    def voting():
        global votes
        if votes == []: votes = ['a','b','c'] #to prevent an error if the list is empty.
        renpy.jump('won'+renpy.random.choice(votes))



label start:

menu:
    "You should vote for:"

    "Peter":
        $votes += 'a'
    "Mary":
        $votes += 'b'
    "Bob":
        $votes += 'c'

menu:
    "You should not vote for"

    "Peter":
        $if 'a' in votes: votes.remove('a')
    "Mary":
        $if 'b' in votes: votes.remove('b')
    "Bob":
        $if 'c' in votes: votes.remove('c')


label voting:

    $if len(votes) < 5: renpy.jump('start') #just for testing

    "Current possibilities are: [votes]" #just for testing

    $voting()

label wona:
    "Peter won"

    return

label wonb:
    "Mary won"

    return

label wonc:
    "Bob won"

    return
Only downside is that you can't go into negative points with this system.
You could make it a lot fancier but I'm kinda tired so.... I'm sorry the I didn't explain the code very well. I'll add that later if the code is to horrible to read :). Anyways I hope this helps.

edit: corrected a stupid mistake in the code.
Last edited by GNVE on Thu Nov 21, 2019 8:19 pm, edited 1 time in total.

Rekoija
Newbie
Posts: 15
Joined: Wed Nov 20, 2019 3:19 pm
Contact:

Re: Voting for someone based on a percentage?

#5 Post by Rekoija » Thu Nov 21, 2019 3:59 pm

def voting():
if votes == []:
votes = ['a','b','c'] #to prevent an error if the list is empty.
renpy.jump('won'+renpy.random.choice(votes))


this part causes this error:

file "game/scriptvote.rpy", line 7: Tab characters are not allowed in Ren'Py scripts.

line seven being "if votes == []:"


edit: also uhhh, idk where i'll be adding the points? can you show me where the ($ votea = 0) comes into the equation?

GNVE
Regular
Posts: 37
Joined: Wed Sep 12, 2018 4:11 pm
Completed: ShSt - Bad Day
Projects: ShSt - Afterparty, Collings University
itch: https://gnve.itch.io
Contact:

Re: Voting for someone based on a percentage?

#6 Post by GNVE » Thu Nov 21, 2019 8:17 pm

Rekoija wrote:
Thu Nov 21, 2019 3:59 pm
def voting():
if votes == []:
votes = ['a','b','c'] #to prevent an error if the list is empty.
renpy.jump('won'+renpy.random.choice(votes))


this part causes this error:

file "game/scriptvote.rpy", line 7: Tab characters are not allowed in Ren'Py scripts.

line seven being "if votes == []:"
Oh sorry as said a bit tired. I made the rest in Renpy but added the "votes = ['a','b','c'] #to prevent an error if the list is empty." line at the last moment in the browser.
This should work better:

Code: Select all

    def voting():
        global votes
        if votes == []: votes = ['a','b','c'] #to prevent an error if the list is empty.
        renpy.jump('won'+renpy.random.choice(votes))
edit: also uhhh, idk where i'll be adding the points? can you show me where the ($ votea = 0) comes into the equation?
This voting system woudln't count the votes in that way. the votes = [] is a list with every vote recorder e.g. ['a','a','b','c','a','c','a','b','b','b','a','c','c','a','c','c','b'] and picks one random vote to win from the list (so basically the percentage choice you wanted) as said the downside is that you can't go into the negative with this system because you can only remove items from the list.

If you really need the point system I'd use a code like this:

Code: Select all

python early:
    count = 0 #just for testing this code
    votea = 0 #count for number of votes cast for A, Peter in this example
    voteb = 0 #count for number of votes cast for B, Mary in this example
    votec = 0 # and the votes for c or Bob
    voted = 0 #not used in this example but makes it easier to drop into your code

    def voting():
        global votea
        global voteb
        global votec
        global voted

        if votea < 0: votea = 0 #unless you really need to know if your player went into negative points then I can work around that as well.
        if voteb < 0: voteb = 0
        if votec < 0: votec = 0
        if voted < 0: voted = 0 #again not used in the example
        if votea + voteb + votec + voted == 0: renpy.jump('won'+renpy.random.choice(['a','b','c','d'])) #makes sure that if all vote values are 0 or negative the game doesn't default to wona

        winnum = renpy.random.randint(1,votea+voteb+votec+voted)
        if winnum <= votea: renpy.jump('wona')
        elif winnum <= votea + voteb: renpy.jump('wonb')
        elif winnum <= votea + voteb + votec: renpy.jump('wonc')
        else: renpy.jump('wond')







label start:

menu:
    "You should vote for:"

    "Peter":
        $votea += 1
    "Mary":
        $voteb += 1
    "Bob":
        $votec += 1

menu:
    "You should not vote for"

    "Peter":
        $votea -= 1
    "Mary":
        $voteb -= 1
    "Bob":
        $votec -= 1


label voting:
    $count += 1 #just for testing this code
    $if count <5: renpy.jump('start') #just for testing this code

    $voting()

label wona:
    "Peter won"

    return

label wonb:
    "Mary won"

    return

label wonc:
    "Bob won"

    return

label wond:
    "D won"

    return

Rekoija
Newbie
Posts: 15
Joined: Wed Nov 20, 2019 3:19 pm
Contact:

Re: Voting for someone based on a percentage?

#7 Post by Rekoija » Fri Nov 22, 2019 5:35 am

wow, thank you so much!

edit: (for anyone else that may need this):
that count thing confused me a bit, so i kinda broke the code until i figured out i actually needed it to be there (when it's not there i guess it just always goes to a? anyway i set the count to +=5 instead of removing it, and everything works perfectly!)




so yeah, i really appreciate the help! :D

GNVE
Regular
Posts: 37
Joined: Wed Sep 12, 2018 4:11 pm
Completed: ShSt - Bad Day
Projects: ShSt - Afterparty, Collings University
itch: https://gnve.itch.io
Contact:

Re: Voting for someone based on a percentage?

#8 Post by GNVE » Fri Nov 22, 2019 7:39 am

Rekoija wrote:
Fri Nov 22, 2019 5:35 am
that count thing confused me a bit, so i kinda broke the code until i figured out i actually needed it to be there (when it's not there i guess it just always goes to a? anyway i set the count to +=5 instead of removing it, and everything works perfectly!)
Hey Rekoija,

Everything that I said was for testing only can be removed in your game. It basically substitutes all menu's you'll eventually implement in the game to add or subtract votes for the characters in your game.
(what it does is just repeat those two menu's 5 times so you see it works.)

Post Reply

Who is online

Users browsing this forum: Triority