Behaviors & Python

For questions about using Classic.

Post » Sun Nov 09, 2014 5:27 pm

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 !
B
7
S
1
Posts: 58
Reputation: 683

Post » Mon Nov 10, 2014 2:16 am

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[i]
SpritePlatform[i]

SOL.Sprite
SOL.SpritePlatform

SOL.Sprite[i]
SOL.SpritePlatform[i]


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))")
B
94
S
33
G
117
Posts: 5,388
Reputation: 75,337

Post » Tue Nov 11, 2014 2:39 pm

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 :D

player.elite
B
7
S
1
Posts: 58
Reputation: 683

Post » Tue Nov 11, 2014 3:49 pm

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]) ?
B
7
S
1
Posts: 58
Reputation: 683

Post » Tue Nov 11, 2014 6:02 pm

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.
B
94
S
33
G
117
Posts: 5,388
Reputation: 75,337

Post » Wed Nov 12, 2014 8:26 pm

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
B
7
S
1
Posts: 58
Reputation: 683

Post » Thu Nov 13, 2014 9:43 pm

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 :
Code: Select all
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()



Then, when you spawn NPCs (for each npcs etc..) do :
Code: Select all
npcManager.addNpc(SOL.entity, SOL.entityRTS)


Finally, to call event from your object : (for example : every X seconds -> for each object -> do this code)
Code: Select all
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)


EDIT Btw, I don't know how to add python variables to objects in a list :/
B
7
S
1
Posts: 58
Reputation: 683

Post » Sun Nov 23, 2014 12:48 pm

R0J0hound wrote: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[i]
SpritePlatform[i]

SOL.Sprite
SOL.SpritePlatform

SOL.Sprite[i]
SOL.SpritePlatform[i]


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 :
Code: Select all
System.CreateByName("Sprite", 1, 100, 100)


then the Sprite[x] thing is giving me this error :
---------------------------
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: Select all
System.CreateByName("Sprite", 1, 500, 200)
Sprite[1].SetX(10)


There is 1 sprite instance created with the layout editor (Sprite[0]) and one with python (supposed to be Sprite[1])

EDIT 2 : 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) ?
B
7
S
1
Posts: 58
Reputation: 683

Post » Mon Nov 24, 2014 2:29 am

It causes that error because of how picking works. The correct way to reference newly created sprites is:
Code: Select all
System.CreateByName("Sprite", 1, 500, 200)
SOL.Sprite[0].SetX(10)


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:
span-class-posthilit-python-span-references-want-to-get-what-span-c_p627869?#p627869
B
94
S
33
G
117
Posts: 5,388
Reputation: 75,337

Post » Sat Nov 29, 2014 6:14 pm

Thanks !
Now I can call any object without losing it :D

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 :
Code: Select all
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

 


Thanks a lot for all the help you provided to me ! :)
B
7
S
1
Posts: 58
Reputation: 683

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 1 guest