GLBasic forum

Main forum => GLBasic - en => Topic started by: MikeHart on 2010-Feb-24

Title: Type problem
Post by: MikeHart on 2010-Feb-24
Hi folks,

I thought types as parameters of functions and return parameters are always BYREF. Byref means as a pointer.

So why is this code not showing random positions but it always shows the initial values of the type.

Code (glbasic) Select
TYPE myType
xpos
ypos
ENDTYPE

GLOBAL newType AS myType
GLOBAL myTypeList[] AS myType
GLOBAL currType AS myType

FOR i = 1 TO 10
newType = CreateNewType()
SetPos(newType,RND(480),RND(320))
NEXT
FOREACH currType IN myTypeList[]
j = j + 1
PRINT currType.xpos+":"+currType.ypos,10,j*10
NEXT
SHOWSCREEN
KEYWAIT

FUNCTION CreateNewType AS myType:
LOCAL newt AS myType
newt.xpos = 11
newt.ypos = 22
DIMPUSH myTypeList[], newt
RETURN newt
ENDFUNCTION

FUNCTION SetPos: t AS myType, x, y
t.xpos = x
t.ypos = y
RETURN TRUE
ENDFUNCTION
Title: Re: Type problem
Post by: Kitty Hello on 2010-Feb-24
the create new type returns a COPY of the element you pushed into the array.
Title: Re: Type problem
Post by: Moru on 2010-Feb-24
Push the item into the array after you have set the position, you are accessing a copy of the data.

Code (glbasic) Select
TYPE myType
xpos
ypos
ENDTYPE

GLOBAL newType AS myType
GLOBAL myTypeList[] AS myType
GLOBAL currType AS myType
LOCAL j

FOR i = 1 TO 10
newType = CreateNewType()
SetPos(newType,RND(480),RND(320))
DIMPUSH myTypeList[], newType
NEXT
FOREACH currType IN myTypeList[]
j = j + 1
PRINT currType.xpos+":"+currType.ypos,10,j*10
NEXT
SHOWSCREEN
KEYWAIT

FUNCTION CreateNewType AS myType:
LOCAL newt AS myType
newt.xpos = 11
newt.ypos = 22
RETURN newt
ENDFUNCTION

FUNCTION SetPos: t AS myType, x, y
t.xpos = x
t.ypos = y
RETURN TRUE
ENDFUNCTION
Title: Re: Type problem
Post by: MikeHart on 2010-Feb-25
Ok, I see. But in and out of functions , types are used by reference, right?

So if that is given, how can I return a reference of the type I pushed into the array? Is this impossible?

Title: Re: Type problem
Post by: Kitty Hello on 2010-Feb-25
You cannot "return" a handle. You can only pass a handle per reference. If you returned a handle, a new addition to the array would break the handle possibly because of reallocations. There is a small limitations because of that, but this way you have a lot less headache than with C or C++ programming languages. You can't return a reference in Java or so, either. It's for your own safety. Redesign the function to get it working as we showed above.
Title: Re: Type problem
Post by: MikeHart on 2010-Feb-25
QuoteYou can't return a reference in Java or so, either

Maybe in JAVA you can't, in other languages you can. But this is not about other languages, it's about GLBasic. And there I take it as a given that I can't do it the way like I would and have to redesign my approach. I was porting my sprite library to GLBasic so was looking for a way to reference types.

But thanks for the info and clarification.
Title: Re: Type problem
Post by: Kitty Hello on 2010-Feb-25
why not return the index in the array then? That's the way I handle all that kind of things in GLBasic wrappers.
Title: Re: Type problem
Post by: MikeHart on 2010-Feb-25
This is only practical when you have a fix number of objects. But in my sprite engine I want to be able to remove and add new objects at any time and within a frame. So the index of an array element can change at any time. Because my objects can have a parent child relationship, it would make this not easier/possible.
Title: Re: Type problem
Post by: Kitty Hello on 2010-Feb-26
if you remove one, just set the internal flag to zero. If you add one, try to find an empty index in the array. If not, append one. That's what I do with the entity system.
Title: Re: Type problem
Post by: Moru on 2010-Feb-26
This saves a lot of reordering and allocating new memory all the time so will make your code fast. In general when making arrays that will change size a lot, allocate a big array, when you run out of space, allocate a large chunk at once.