GLBasic forum

Feature request => IDE/Syntax => Topic started by: bigtunacan on 2011-Oct-01

Title: Function return object chaining?
Post by: bigtunacan on 2011-Oct-01
Any chance we will be able to chain directly off of function returns at some point?

something like this...

Code: GLBasic [Select]
ltr.ent = init_entity(get_kind_of_sprite("ship").name$)
 

rather than requiring an intermediate step like this?

Code: GLBasic [Select]
LOCAL spr AS SPRITE = get_kind_of_sprite("ship")
ltr.ent = init_entity(spr.name$)
 
Title: Re: Function return object chaining?
Post by: Kitty Hello on 2011-Oct-04
post an example where it yields an error and I try to fix it.
Title: Re: Function return object chaining?
Post by: MrTAToad on 2011-Oct-05
I think he wants something like this :

Code: GLBasic [Select]
TYPE SPRITE
        name$
ENDTYPE

DEBUG init_entity(get_kind_of_sprite("ship").name$)

FUNCTION get_kind_of_sprite AS SPRITE:name$
LOCAL temp AS SPRITE

        temp.name$="moo"
        RETURN temp
ENDFUNCTION

FUNCTION init_entity%:name$
        DEBUG name$+"\n"
ENDFUNCTION

I can see it being very hard to implement...
Title: Re: Function return object chaining?
Post by: Kitty Hello on 2011-Oct-05
ah. The bad thing is, that a object is always returned by value, which means a full copy is done.
I think there's better ways to do this. But it's a bug, yes.
Title: Re: Function return object chaining?
Post by: bigtunacan on 2011-Oct-09
Sorry,  I haven't been on the forums in a few days.

But MrTaToad had the general idea.  In the particular example I ran into I was using the below

This is just showing a couple of custom TYPEs I am using
Code: GLBasic [Select]
TYPE LETTER
        ent AS ENTITY
        xoffset%

        FUNCTION move:
                self.ent.locy = self.ent.locy+1
                self.ent.locx = (150 * SIN(self.ent.locy)) + self.xoffset%
        ENDFUNCTION
ENDTYPE

TYPE ENTITY
        name$
        locx%
        locy%
        scale#
        rot#
        xmin%
        xmax%
        ymin%
        ymax%
        alive
        width%
        height%
ENDTYPE
 

Here is where I run into the error...

Code: GLBasic [Select]
GLOBAL letters_array[] AS LETTER
SUB init_letters:
        LOCAL ltr AS LETTER
        LOCAL spr AS SPRITE
        LOCAL yoffset%

        FOR i=0 TO 9
                spr=get_kind_of_sprite("ship") // <== This is what I ended up doing
                ltr.ent = init_entity(spr.name$)   // <== 2 lines instead

//              ltr.ent = init_entity(get_kind_of_sprite("ship").name$)    // <== Wanted to do this all in one line //                                                                                                               since it's cleaner IMHO

                yoffset% = RND(1000)
                DEBUG "yoff: " + yoffset% + "\n"
                ltr.ent.locy = -ltr.ent.height - yoffset%
                ltr.xoffset% = RND(screen_width%)
                DIMPUSH letters_array[], ltr
        NEXT
ENDSUB