[SOLVED] Assign text to an instance (SOL Python)

For questions about using Classic.

Post » Wed Sep 11, 2013 12:30 pm

Hi!

Right now I'm working on some dynamic menus for a miniRPG project. I wanted to create a menu with Python by using a function with a parameters, like this:
[CODE]display("Title","Content",["OK,"CANCEL"])[/CODE]
and then
[CODE]def display(title,content,options):
    ...
    # CREATION OF NEW OBJECTS
          for items in options:
              System.CreateByName("POPUP_OPTION_BG","POPUP",Window.Width-128-option_offset,POPUP_TITLE_BG.Y+POPUP_BG.Height+32)
              System.CreateByName("txt_option","POPUP",Window.Width-128-option_offset,POPUP_TITLE_BG.Y+POPUP_BG.Height+32)
              #System.Create("txt_option","POPUP",Window.Width-128-option_offset,POPUP_TITLE_BG.Y+POPUP_BG.Height+32)
              option_offset += 144[/CODE]

The trouble starts when I try to assign Text values to those objects. I want to iterate through them and set every each instance with it's own option text. I've tried setting the text on creation and this method (found on SourceForge net page), just to set every instance with this text, for debug reasons:

[CODE]for t in txt_option:
     t.Text= "something"[/CODE]

but it didn't work as expected. This sets the new text for only one instance, the one existing in the layout before the creation. Is there a way to do this at all? I really would prefer dynamic creation of objects rather than hiding / showing trick for objects already on the layout...

Any help appreciated!
bitworks2013-09-12 11:28:38
B
4
S
1
Posts: 28
Reputation: 435

Post » Wed Sep 11, 2013 9:07 pm

You can access the last created instance with:
SOL.txt_option[0]
But this won't help if you create multiple instances at once.

You could make a function like this to return a reference to a newly created object, then you could store the references in a list or something.
[code]def my_create(name, layer, x, y):
    System.Create(name,layer,x,y)
    return eval("SOL."+name+"[0]")[/code]
B
92
S
32
G
107
Posts: 5,274
Reputation: 69,959

Post » Wed Sep 11, 2013 11:38 pm

Well, this seems to be what I'm looking for, thank you! I'm creating instances in a loop, one per loop, so accessing that instance in that loop should do just fine, I suppose. Also, storing a dictionary or a list of SOL items created that way in a Hash Table sounds very promising! Great idea.
B
4
S
1
Posts: 28
Reputation: 435


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 0 guests