Dynamic imagebuttons set

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
Master Set
Newbie
Posts: 15
Joined: Thu Nov 21, 2013 4:40 am
Contact:

Dynamic imagebuttons set

#1 Post by Master Set »

Hi!

In my battle system you can choose which enemy to attack by pushing an imagebutton with a portrait of that enemy.
It can be done with a screen like:

Code: Select all

screen choose_enemy:
    hbox xalign 0.5 yalign 0.5:
        imagebutton auto "demon_%s.png" action Jump('demon')
        imagebutton auto "dragon_%s.png" action Jump('dreagon')  
But enemies are different each time. And in different numbers. If one enemy is dead already i dont want to show his button. Wat if the new enemy appears in reinforsments?

How can i make my set of imagebuttons to change according to situation?

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Dynamic imagebuttons set

#2 Post by xela »

I don't know your code patterns, but think along the lines of:

Code: Select all

enemies = []

enemies.append("demon")
enemies.append("dreagon")

screen choose_enemy:
    hbox xalign 0.5 yalign 0.5:
        for enemy in enemies:
            imagebutton auto "".join(["%s"%enemy, "_%s.png"]) action Jump(enemy)

enemies.remove("demon") # would get rid of a demon...
Like what we're doing? Support us at:
Image

User avatar
i1abnrk
Regular
Posts: 38
Joined: Wed Nov 20, 2013 1:50 pm
Projects: Critter Corral, Haremu Kikkemu Mo
IRC Nick: i1abnrk
Deviantart: i1abnrk
Github: i1abnrk
Location: Wisconsin
Contact:

Re: Dynamic imagebuttons set

#3 Post by i1abnrk »

I'd use a little atl and a bit vector. It requires there be a maximum number of enemies on the screen at a time but we can also have a buffer of enemies waiting to appear. This we call enemy_formation or something. Each enemy_formation has a list of positions and a bitvector (a list of booleans) to tell us the next open position in the formation. An enemy_remove function will clear the dead guy from the screen and check if the buffer has another enemy to replace it. The list of positions is a list of screen positions as (x,y) tuples. Each battle screen can get its own formation by having a global function to call.
I don't have time to write code for you atm but I just finished something similar so I'll come back in 12 hours or so to finish this. Maybe the psuedocode above can get the wheels turning in the meantime.

User avatar
i1abnrk
Regular
Posts: 38
Joined: Wed Nov 20, 2013 1:50 pm
Projects: Critter Corral, Haremu Kikkemu Mo
IRC Nick: i1abnrk
Deviantart: i1abnrk
Github: i1abnrk
Location: Wisconsin
Contact:

Re: Dynamic imagebuttons set

#4 Post by i1abnrk »

To the next step I take is to develop an algorithm that fills variables into the battle scene script.
Nouns describe the data.
Nouns: atl, bitvector, list, booleans, maximum-number, enemies, screen, time, buffer of enemies waiting, enemy_formation, list of positions, next open position, enemy_position, enemy_remove, dead guy, function, screen positions, tuples, global function
Verbs: use, require there be, can also have, call, has, tell, remove, clear, check, replace, is a list of, get its own.
I am at a friends house but It's time to scidaddle. More in a couple hours.

User avatar
i1abnrk
Regular
Posts: 38
Joined: Wed Nov 20, 2013 1:50 pm
Projects: Critter Corral, Haremu Kikkemu Mo
IRC Nick: i1abnrk
Deviantart: i1abnrk
Github: i1abnrk
Location: Wisconsin
Contact:

Re: Dynamic imagebuttons set

#5 Post by i1abnrk »

So I am picturing what a battle formation might look like. For my example I'll have a row of 3 enemies in the back (top of the screen). I like to use pixels rather than relative positions. I assume the default resolution of 1028 by 800px. Enemies will be 240 by 360px.
I start with a class Battle_Formation and put that in an init -1 python block, where user classes should be.

Code: Select all

init -1 python:
    class Battle_Formation_Factory:
        #the top left corners of the plots
        positions=[]
        #which positions are empty
        empty=[]
        __init__(positions=[(0,0)], empty=[True]):
            self.positions=positions
            self.empty=empty
        #make a function like this for each formation
        def default_formation():
            return self([(34,80), (394,80), (754,80)], [True, True, True])
Then I need an enemy buffer and the current formation in a global in the init python phase.

Code: Select all

init:
    python:
        current_formation = None
        current_plot=(0,0)
        enemy_buffer = []
Next I make a transform to put baddies in position.

Code: Select all

    transform battleground:
        xpos current_plot[0]
        ypos current_plot[1]
Lastly, a scene which I'll call battle_screen. But the contents are unimportant to the script.
And a label where it all comes together in the script.

Code: Select all

label draw_enemies:
    #Use the default formation if it hasn't been set already
    if not current_formation:
        current_formation = Battle_Formation_Factory.default_formation()
    #keeps count of the plots in a formation
    $plot_index=0
    #look at each plot in the formation to check if it is empty
    for flag in current_formation.empty:
        #if this plot has no enemy in it
        if flag:
            #somewhere else we are appending and removing enemies to the list
            #if there are enemies that haven't been fought
            $if len(enemy_buffer):
                #store the top left corner of the plot (x,y)
                current_plot = (current_formation.positions[plot_index][0], current_formation.positions[plot_index][1])
                #see renpy documents function index for renpy.show
                $renpy.show('enemy healthy', [battleground], 'master', 0, None, 'plot_'+str(plot_index)+'_enemy')
        #prepare the next plot
        $plot_index=plot_index+1
Last edited by i1abnrk on Tue Jan 21, 2014 1:27 am, edited 3 times in total.

Master Set
Newbie
Posts: 15
Joined: Thu Nov 21, 2013 4:40 am
Contact:

Re: Dynamic imagebuttons set

#6 Post by Master Set »

I belive i need more experience with python to understand this. Too complicated for me right now. (

Master Set
Newbie
Posts: 15
Joined: Thu Nov 21, 2013 4:40 am
Contact:

Re: Dynamic imagebuttons set

#7 Post by Master Set »

Can someone tell me how this sting works exactly?

Code: Select all

imagebutton auto "".join(["%s"%enemy, "_%s.png"]) action Jump(enemy)

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Dynamic imagebuttons set

#8 Post by xela »

Master Set wrote:Can someone tell me how this sting works exactly?

Code: Select all

imagebutton auto "".join(["%s"%enemy, "_%s.png"]) action Jump(enemy)
join is a string method: http://docs.python.org/2/library/stdtypes.html#str.join
Like what we're doing? Support us at:
Image

Post Reply

Who is online

Users browsing this forum: Dark79, Google [Bot]