Python

This forum is currently in read-only mode.
0 favourites
  • 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.

    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]
  • Here's how with correct syntax:

    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]
  • 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.

  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • For some reason this way of setting global variables wasn't working.

    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 "//"
    
    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
    
    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.
  • ya, I know, I get the comment sign mixed up with other programming languages. I believe "//" is java, right?

    Yes and many other languages too.

    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)).

    I'm not entirely sure, but I'd presume so.

  • [quote:1ie4m1jp]you might be able to save sometime by using a dictionary

    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)).

    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"

    # 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.
  • [quote:2qbexcfc]I'm not exactly sure how you want to create, change and use the values.

    I don't know if this will clear things up, but I'll try to.

    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.
  • [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?

    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?

    You want to use UID, it is unique for each object, so no two objects will ever have the same UID.

  • [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?

    It is normal, OID is unique for each object type, so two instances of the same type will have the same OID.

    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.

    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.

  • There shouldn't be any limit to python scripts. I was able to run a 100 line script without any issues.

  • [quote:32womvdf]you might be able to save sometime by using a dictionary

    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)).

    Though it's a bit irregular, a dictionary using tuples as keys can be used as a sort of multi-dimensional array. Also, a dict's get() and setdefault() methods can provide easy handling of cases where keys are not there yet. Here's a simple example:

    http://www.daniweb.com/code/snippet275835.html

    With that pointed out, I'd just use lists in most cases.

    [quote:32womvdf]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.

    I've got a few scripts well longer than that, with no problems.

  • [quote:2h1rx0he]There shouldn't be any limit to python scripts. I was able to run a 100 line script without any issues.

    [quote:2h1rx0he]I've got a few scripts well longer than that, with no problems.

    I don't know why, but it's just ignoring the lines of script. Your'e probably right about it not being limited though, I switched a few lines around with the same result.

    Thanks for the link Silent Cacophony, it looks helpful.

    Edit:Could someone show a simple way of making and getting information from a dictionary in construct? I'm not doing something right and it's frustrating not being able to figure it out.

    I found out how to do it

    d1{}
    d1[1,1,1] = "Hello"
    d1[1,1,2] = "Bob"
    ...
    Text.SetText(d1[1,1,1] + " " + d1[1,1,2])
    #Sets text of Text to "Hello Bob" without the quotes
    [/code:2h1rx0he]
  • I was wondering how to use the MouseKeyboard on object clicked event in python. I was able to get this far:

    MouseKeyboard.OnClickObject(left,single,Object)[/code:20m0o9c6]However, "single" isn't the correct syntax. Any idea on what to use instead? Thanks.
  • For object parameters use the object name in quotes.

    For combo parameters use a number (0 for the first choice, 1 for the 2nd and so on).

    MouseKeyboard.OnClickObject(0,0,'Object')[/code:2s8jsalz]
    
    Be warned though, "On object clicked" is a triggered condition.  Triggered conditions only work in events, they will not work in a python script.
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)