Python

For questions about using Classic.

Post » Sat Oct 09, 2010 9:09 pm

I'm pretty new to python(completely) and I was wondering how to make an object at the mouse position and if this small script would work. I apologize in advance for all the errors in it. Like I said, I have no experience with python. :D
[code:k2idk6u4]
if System.global('action') == "Build":
System.global('menunum') = 2
if System.global('action') == "Gather":
System.global('menunum') = 3
if System.global('action') == "House":
// how do you create and object at mouse position?
[/code:k2idk6u4]
B
2
G
2
Posts: 48
Reputation: 746

Post » Sat Oct 09, 2010 11:07 pm

Here's how with correct syntax:
[code:1howd1bs]if System.globalvar('action') == "Build":
System.SetGlobalVar('menunum') = 2
if System.globalvar('action') == "Gather":
System.SetGlobalVar('menunum') = 3
if System.globalvar('action') == "House":
System.Create("Sprite", 0, System.mousex, System.mousey)[/code:1howd1bs]
B
79
S
24
G
52
Posts: 4,725
Reputation: 39,713

Post » Sat Oct 09, 2010 11:43 pm

Thanks! There's not a lot of posts about python in the forums which can make it difficult to learn it. Using this should make my list of 40 or more things take less time to make. :D
B
2
G
2
Posts: 48
Reputation: 746

Post » Sun Oct 10, 2010 12:21 am

Just in-case you didn't know, "#" is how you make a comment in Python, not "//". I'm not too familiar with Python, but you might be able to save sometime by using a dictionary to store your global variables.
B
3
G
3
Posts: 23
Reputation: 946

Post » Sun Oct 10, 2010 8:52 pm

For some reason this way of setting global variables wasn't working.
[code:aylto7xh]System.SetGlobalVar('menunum') = 2[/code:aylto7xh]
Instead I had to use this.
[code:aylto7xh]System.SetGlobalVar('menunum',2)[/code:aylto7xh]
[quote:aylto7xh]#" is how you make a comment in Python, not "//"[/quote:aylto7xh]
ya, I know, I get the comment sign mixed up with other programming languages. I believe "//" is java, right?
[quote:aylto7xh]you might be able to save sometime by using a dictionary[/quote:aylto7xh]
That's probably a good idea. Could this be used to substitute for an array? I wouldn't be using it to save the game, but it would store values with 3 keys (ex. (x,y,z) or (1,2,1)).

This brings me to my final two questions:
How do you get the oid of an object using python?
Is there a place where I could have found this information at?

Edit: Solved oid issue. Instead of object.OID, I used Object.OID.
B
2
G
2
Posts: 48
Reputation: 746

Post » Mon Oct 11, 2010 8:29 am

[quote="RomanCenturion":k14e8iq8]ya, I know, I get the comment sign mixed up with other programming languages. I believe "//" is java, right?[/quote:k14e8iq8]

Yes and many other languages too.


[quote="RomanCenturion":k14e8iq8]That's probably a good idea. Could this be used to substitute for an array? I wouldn't be using it to save the game, but it would store values with 3 keys (ex. (x,y,z) or (1,2,1)).[/quote:k14e8iq8]

I'm not entirely sure, but I'd presume so.
B
3
G
3
Posts: 23
Reputation: 946

Post » Mon Oct 11, 2010 11:51 am

[quote="RomanCenturion":1ie4m1jp]
[quote:1ie4m1jp]you might be able to save sometime by using a dictionary[/quote:1ie4m1jp]
That's probably a good idea. Could this be used to substitute for an array? I wouldn't be using it to save the game, but it would store values with 3 keys (ex. (x,y,z) or (1,2,1)).[/quote:1ie4m1jp]
I'm not exactly sure how you want to create, change and use the values.

A dictionary stores key/value pairs, much like Construct's HashTable. The closest substitute for an array would be a list. If the values you are using are set once and never change, then you could use tuples. Last but not least you can always create your own data structure using "class"

[code:1ie4m1jp]# creating a tuple
position_t = (1, 2, 1)
# alternative way
# positon_t = 1, 2, 1

# creating a list
position_l = [1, 2, 1]

# creating a dict
position_d = {'x': 1, 'y': 2, 'z': 1}

# creating your own data structure
class position:
def __init__(self, x=1, y=2, z=1):
self.x = x
self.y = y
self.z = z

# creating an instance with default values 1, 2, 1
p1 = position()

# creating an instance with values 4, 3, 7
p2 = position(4, 3, 7)[/code:1ie4m1jp]
You may also mix those sequence types/classes to get the structure you need.

[code:1ie4m1jp]# two fixed coords build the positions dict
headquarter = (1, 2, 1)
goldmine = (3, 7, 2)
positions_d = {'head': headquarter, 'gold': goldmine}

# the same with a list
positions_l = [headquarter, goldmine][/code:1ie4m1jp]

It all depends on what exactly you are trying to do with your values.
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Mon Oct 11, 2010 4:25 pm

[quote:2qbexcfc]I'm not exactly sure how you want to create, change and use the values.[/quote:2qbexcfc]
I don't know if this will clear things up, but I'll try to.
[code:2qbexcfc]
at 1,2,1 value = something
at 1,2,2 value = something else
at 2,1,1 value = something else
etc...
if object clicked: #what would this be?
x = 1
avar = 3
if object clicked:
x = 2
avar = 3
etc...
#y and z would be determined by other factors
if avar == 3:
text.SetText(value at x,y,z)
avar = 2
[/code:2qbexcfc]
In my game, I use the OID of sprites a lot. However, Construct has for, some reason, given two sprites the same OID. Is this normal? Also, Construct gives the sprites a different OID each time. Is there a way to influence Construct so that OIDs will be at least different from each other each time? I know a way I can work around this, but I'm just wondering why it did this? On a side note, can OIDs be made so that they're always greater than 0? I've tried Sprite.OID + 1, but it seems to cause bugs.
B
2
G
2
Posts: 48
Reputation: 746

Post » Mon Oct 11, 2010 6:33 pm

[quote:15zk0ssg]In my game, I use the OID of sprites a lot. However, Construct has for, some reason, given two sprites the same OID. Is this normal? [/quote:15zk0ssg]
It is normal, OID is unique for each object type, so two instances of the same type will have the same OID.


[quote:15zk0ssg]Is there a way to influence Construct so that OIDs will be at least different from each other each time?[/quote:15zk0ssg]
You want to use UID, it is unique for each object, so no two objects will ever have the same UID.
B
79
S
24
G
52
Posts: 4,725
Reputation: 39,713

Post » Mon Oct 11, 2010 6:56 pm

[quote:lhqhdp1s][quote:lhqhdp1s]
In my game, I use the OID of sprites a lot. However, Construct has for, some reason, given two sprites the same OID. Is this normal?[/quote:lhqhdp1s]
It is normal, OID is unique for each object type, so two instances of the same type will have the same OID.[/quote:lhqhdp1s]
Sorry, I didn't clarify. They were two different objects, not different instances of the same type. I don't think it could have been a mistake since it worked before, but I'll never know now since I got rid of the OID part of my project and converted it in private variables.
[quote:lhqhdp1s]You want to use UID, it is unique for each object, so no two objects will ever have the same UID.[/quote:lhqhdp1s]
Thanks, I think this will help with an issue I'm having of choosing which instance of the object.

Edit: Is there a limitation on how many lines of script you can have?
I got up to 65 and nothing after line 65 gets executed.
B
2
G
2
Posts: 48
Reputation: 746

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 5 guests