Function variable pointer

About Monkey 2 Forums Monkey 2 Programming Help Function variable pointer

This topic contains 11 replies, has 6 voices, and was last updated by  Mark Sibly 2 years, 1 month ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #7257

    Hezkore
    Participant

    How do I pass a variable to a function as a pointer?
    If I pass an image to my function and load an image into the image variable, it’s not applied outside of that function.

    If I define the function with image:Image Ptr as the perameter and pass Varptr myImage to it, it seems to work.
    But accessing the image via either just image=Image.Load or image[0]=Image.Load causes a compile error.

    #7266

    abakobo
    Participant

    Images are reference based so there is no need to use pointer… (or you re doing some specific external stuffs) It is supposed to passed by reference and any modifications should be kept.

    Did you Flushed the drawing commands: MyCanvas.Flush()? Note that flushing might slow down you process.

    It’s easier to help with the code..

    here is an example of a function drawing to an image with modification by reference.

    [/crayon]
    #7270

    Hezkore
    Participant

    Your example doesn’t show

    .”…and load an image…”

    Which is the problem. 🙁

    Here’s an example showing that it just doesn’t work.
    https://dl.dropboxusercontent.com/u/2842751/image_test.zip

    #7271

    Danilo
    Participant

    > Here’s an example showing that it just doesn’t work.
    > https://dl.dropboxusercontent.com/u/2842751/image_test.zip

    Mark should take a look at your example.

    Here an ugly test using casting to ‘Void Ptr’:

    Works, but casting to void ptr is not typesafe.

    #7272

    AdamStrange
    Participant

    @danilo Line 6 #import “assets/”

    I’ve found that it is best to reference each asset individually and not import using a wildcard.

    Although it work. I found that on windows it would copy assets into the assets folder and also the contents folder. osx worked fine

    When referencing individual files, the asset all appeared correctly in the assets folder on compile

    #7274

    abakobo
    Participant

    My example finished with a crash after a few time… Problem(?) with canvas that I reported.

    The Image objects are extending resource and resource are not GCed, and images can be created only after “New Appinstance”.
    The error message shows that it’s confused with some GCed object and some non GCed object. It converted the image to some GCed object but should not have done that.
    And your simple Function should just work. I tried with a method but it’s still the same problem!

    In my opinion you found 2 bugs! and I found one with my un-usefull example! Nice shot!

    EDIT:reported all 3 supposed bugs on github.

    #7350

    abakobo
    Participant

    Mark has answered the issue and said function params are not actually passed by reference even if classes are reference based. (the problem of image pointers is something else):

    This is expected behaviour – functions parameters are not passed ‘by reference’ so when you assign to ‘img’ inside ‘MyImageLoading’ you are only assigning to a local variable which disappears when the function returns.

    I still don’t understand the exact behaviour of my above example though, and why it works..
    But this example shows a bit of reference based classes behavior in mx2:
    And if you use a global foo class for pointers you’ll end with the same kind of errors we have with image pointers.

    [/crayon]
    #7353

    Hezkore
    Participant

    Well that’s kinda how I expected images to work.
    But I figured that there might be some way to change that, either by passing it as Ptr and accessing it via [0] or something, but that just throws an error.
    I ended up passing the loaded image from inside the function as a return value and via some lambda functions I managed to get it working.

    #7354

    Mark Sibly
    Keymaster

    Mx2 doesn’t have ‘var’ params so you can’t pass a ‘variable’ to a function.

    An array can be used here though – this is preferred to Varptr as it will prevent the object potentially being GC prematurely:

    Something like ‘Out’ or Var’ params will likely happen at some point, not sure when.

    #7355

    GW
    Participant

    Mx2 doesn’t have ‘var’ params so you can’t pass a ‘variable’ to a function.

    What’s the logic behind this decision?

    #7356

    Danilo
    Participant

    > Something like ‘Out’ or Var’ params will likely happen at some point, not sure when.

    +1 for ‘Out’ params.

    #7383

    Mark Sibly
    Keymaster

    > What’s the logic behind this decision?

    Becasue it’s hard to do and I haven’t found a need for it myself or had requests for it yet!

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.