Behaviors & Python

This forum is currently in read-only mode.
  • Hey guys, can someone tell me how to access behaviors event with python please ?

    I mean, for example : Sprite.Behavior.RTS.MoveTo etc... ?

    Thanks in advance !

  • Here's the gist of accessing objects and picked objects in python:

    Behaviors are accessed like so. If the object is called Sprite and the behavior is Platform then the behavior is SpritePlatform from python. Be aware python is case sensitive.

    To access the behavior paired with the object you do it like so depending on the way you access Sprite.

    Sprite

    SpritePlatform

    Sprite

    SpritePlatform

    SOL.Sprite

    SOL.SpritePlatform

    SOL.Sprite

    SOL.SpritePlatform

    You can also see a list of them from python by setting some text to the dictionary with a snippet like this:

    Python("str(dir())")

    Also you can look at a list of methods available in a similar way:

    Python("str(dir(SpritePlatform))")

  • Wow ! Many thanks for this quick answer

    Also, thanks for the link, I did not know about the SOL.Sprite[X] : I always thought that picking an object was replacing the last one !

    Many thanks again

    player.elite

  • Is there a way to create new private variables using python ? Or do I have to create a private variable called for example "parameters" and then split the content to create something like private variables ? (exemple : parameters -> "var1_value:var2_value" etc... and then to get value1, parameters.split(":") -> parameters[0]) ?

  • You can't create new private variables in python. You can create more python variables and add them to objects but unfortunately Sprite[0] is a Different python object than SOL,Sprite[0] so that can lead to errors.

  • Hey, well I created a class for my objects but this I don't know how to add it to Construct :/

    I tried adding it in start of layout -> Python Script -> my class etc..

    With : execfile(path to my py file)

    but it doesn't work, so, how can I add a class to a Construct project :s ?

    Thanks in advance for your help !

    player.elite

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Hey !

    I found a way myself, I don't know if it's the best one but it's working !

    First, to import the class, do :

    Start of layout, python script :

    import sys, os, random
    
    class GNPC():
    	
       def __init__(self):
          # npcList
          self.npcList = []
          self.npcListMov = []
       def addNpc(self, npcObject, npcObjectRTS):
          self.npcList.append(npcObject)
          self.npcListMov.append(npcObjectRTS)
    
    npcManager = GNPC()
    [/code:22wogsv7]
    
    Then, when you spawn NPCs (for each npcs etc..) do : 
    [code:22wogsv7]
    npcManager.addNpc(SOL.entity, SOL.entityRTS)
    [/code:22wogsv7]
    
    Finally, to call event from your object : (for example : every X seconds -> for each object -> do this code)
    [code:22wogsv7]
    npcManager.npcList[0].SetValue("test", 500) # This is to call events from the object
    npcManager.npcListMov[0].MoveTo(random.randint(1,7000), random.randint(1,4000)) # This is to call events from the RTS Behavior (of the object)
    [/code:22wogsv7]
    
    [b]EDIT[/b] Btw, I don't know how to add python variables to objects in a list :/
  • Here's the gist of accessing objects and picked objects in python:

    documentation-for-span-class-posthilit-python-span-scripting_p622892?#p622892

    Behaviors are accessed like so. If the object is called Sprite and the behavior is Platform then the behavior is SpritePlatform from python. Be aware python is case sensitive.

    To access the behavior paired with the object you do it like so depending on the way you access Sprite.

    Sprite

    SpritePlatform

    Sprite

    SpritePlatform

    SOL.Sprite

    SOL.SpritePlatform

    SOL.Sprite

    SOL.SpritePlatform

    You can also see a list of them from python by setting some text to the dictionary with a snippet like this:

    Python("str(dir())")

    Also you can look at a list of methods available in a similar way:

    Python("str(dir(SpritePlatform))")

    Hey,

    Is Sprite[x] supposed to pick the "Sprite x" ?

    Because when I create for example 10 Sprite, if I do Sprite[0].SetX(1) and then Sprite[4].SetX(25) -> it's always the same Sprite object that moves :/

    EDIT : It looks like that if I create those sprite in the Construct Layout Editor (copy paste 5 time the sprite), the Sprite[x] thing is working BUT if I create those using python :

    System.CreateByName("Sprite", 1, 100, 100)
    [/code:2d8t12t0]
    
    then the Sprite[x] thing is giving me this error :
    [quote:2d8t12t0]
    ---------------------------
    Error
    ---------------------------
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\A\F\G\SRC\main.py", line 30, in <module>
        execfile("src/load_game.py")
      File "src/load_game.py", line 5, in <module>
        Sprite[1].SetX(10)
      File "<string>", line 911, in __getitem__
    IndexError
    
    ---------------------------
    OK   
    ---------------------------
    
    
    
    my python code is 
    [code:2d8t12t0]
    System.CreateByName("Sprite", 1, 500, 200)
    Sprite[1].SetX(10)
    [/code:2d8t12t0]
    
    There is 1 sprite instance created with the layout editor (Sprite[0]) and one with python (supposed to be Sprite[1])
    
    [h2][b]EDIT 2 :[/b][/h2] Sprite[x] seems to works only when ALL the code of "load_game.py" was executed, is there a way to access Sprite[x] before all the code was executed (except SOL.Sprite) ?
  • It causes that error because of how picking works. The correct way to reference newly created sprites is:

    System.CreateByName("Sprite", 1, 500, 200)
    SOL.Sprite[0].SetX(10)[/code:1wskp8k4]
    
    The relevant bit of the error is IndexError which basically means it can't access anything at that index.
    The reason? CC doesn't add the newly created objects to the object list until the next toplevel event.  C2's picking of new objects is much the same.
    
    If you want to go the python route try:
  • Thanks !

    Now I can call any object without losing it

    I'm creating something like a "game engine" (a customizable game would be a better word), I'm done with objects (picking, attributes) etc.. and now i'm working on events :

    is there any way to access Construct Events with python ? Or should I do like pygame :

    In construct  : On key "Q" pressed : run python : pyorbit.events.append("KeyPressed:Q")
    
    and then in my "loop.py" file :
    
    for event in pyorbit.events:
         do stuff
    
     [/code:1octdss7]
    
    Thanks a lot for all the help you provided to me !
  • Yeah that or you could call a certain python function directly.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)