Function return object chaining?

Previous topic - Next topic

bigtunacan

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

Kitty Hello

post an example where it yields an error and I try to fix it.

MrTAToad

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

Kitty Hello

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.

bigtunacan

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