07 June 2016, 10:37 | #1 |
Registered User
Join Date: Dec 2013
Location: Auckland
Posts: 3,541
|
Share NewType to Statement/Function
I can't work out how to do this? The manual states that you can't pass NewTypes directly, but Pointers can be passed? Is there an example of this?
Thanks in advance, Erik |
07 June 2016, 20:27 | #2 |
Registered User
Join Date: Oct 2014
Location: Europe
Posts: 470
|
I've expanded the example from a while ago:
Code:
NEWTYPE.foo a.w b.w c.w End NEWTYPE DEFTYPE.foo tsrc ;create a variable of the new type, means actually malloc() tsrc\a=10,20,30 *tdst.foo = tsrc ;grab the address of the memory where our nt variable is If *tdst ;is this a valid address? NPrint Hex$(*tdst) NPrint *tdst\a NPrint *tdst\b NPrint *tdst\c ; ;*tdst=0 ;make sure we are not trying to free that later EndIf Statement zorg {ntaddr.l} If ntaddr>0 ;better check all times *dummy.foo=ntaddr NPrint Hex$(*dummy) NPrint *dummy\a NPrint *dummy\b NPrint *dummy\c *dummy=0 EndIf End Statement zorg {tsrc.foo} zorg {*tdst} MouseWait End Hope this helps. *Edit: With a little bit more information about your goals i might have other ideas. The above example is (as it is now) useless, as it could be done by simply declaring the ntype as SHARED inside the Function. Last edited by Cylon; 10 June 2016 at 01:14. Reason: added src comments |
08 June 2016, 00:53 | #3 |
Registered User
Join Date: Dec 2013
Location: Auckland
Posts: 3,541
|
Thanks mate, I think I follow that.
I did eventually use a shared, eg: Code:
DefType .Projectile *ThisProjectile Statement ProjectileUpdate{} Shared *ThisProjectile.Projectile (misc code) End Statement |
08 June 2016, 08:10 | #4 |
Registered User
Join Date: Sep 2007
Location: Stockholm
Posts: 4,332
|
Is the asterisk really necessary in your case, Earok?
|
08 June 2016, 20:54 | #5 |
Registered User
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,335
|
Yep, you can share a Newtype with statements and functions without using a pointer (asterisk). That's only necessary to pass it as an argument. Unless you have another area of code where you have to deal with it as a pointer, I would suggest leaving it as a plain Newtype.
|
09 June 2016, 03:55 | #6 |
Registered User
Join Date: Dec 2013
Location: Auckland
Posts: 3,541
|
Pointer is the only way we can set what the variable, uh, points to, right?
I'm using it along these lines Code:
for i = 0 to 1 *ThisProjectile = projectiles(i) ProjectileUpdate{} next Statement ProjectileUpdate{} Shared *ThisProjectile.Projectile (misc code) End Statement |
09 June 2016, 10:56 | #7 |
Registered User
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,335
|
Well, yes, if you need to pass the pointer as an argument. But if you're sharing them, you don't need to use pointers:
Code:
NEWTYPE .mytype a.w b.w c.s End NEWTYPE Deftype .mytype TestVar TestVar\a = 1 TestVar\b = 2 TestVar\c = "Hello World!" ; Various code Statement myStatement{} Shared TestVar NPrint TestVar\c End Statement Edit: I've taken a proper look at your example code, and it's slightly different to what I had in mind but would be a good example of pointer use if the *ThisProjectile was passed as an argument instead of shared. If you wanted to do it the sharing, you could still do it without using pointers: Code:
for i = 0 to 1 ProjectileUpdate{} next Statement ProjectileUpdate{} Shared projectiles(), i (misc code) End Statement P.S. In Blitz, the statement code needs to be before the calling code in the source, so that example won't work in practice... Last edited by Daedalus; 09 June 2016 at 11:09. |
09 June 2016, 11:02 | #8 |
Registered User
Join Date: Dec 2013
Location: Auckland
Posts: 3,541
|
Except, you can't do this (or the equivalent of) without pointers though? What I'm really after is being able to pass any instance of the newtype to the statement, not just one.
Code:
Deftype .mytype TestVar Deftype .mytype Var1 Deftype .mytype Var2 TestVar = Var2 myStatement{} ; Various code Statement myStatement{} Shared TestVar NPrint TestVar\c End Statement |
09 June 2016, 11:30 | #9 |
Registered User
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,335
|
Yep, that's where pointers are a better choice. In your first example it was an array, which can easily be shared along with its index (i) to enable the statement to use any instance within that array. If you're talking about different variables entirely and not just different instances within an array, you either need to share them all or use pointers. That example you just posted would still work, but copying newtypes can be slow if they're big, and the statement works with a copy (TestVar), not the original data (Var1 or Var2).
Like I said, if sharing pointers works for you, by all means go for it. It's just more "correct" to pass it as an argument to the statement as in Cylon's example. |
12 June 2016, 02:35 | #10 |
Registered User
Join Date: Oct 2014
Location: Europe
Posts: 470
|
Once you start messing with list nodes and stuff you will need to go the * way, anyway, always.
|
24 May 2018, 11:22 | #11 |
A1200 040 SAM440EP 667
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 873
|
Hi, sorry for hijack this thread: blasted
I am confused about how do I could export an entire NType array from inside a function so later I could use the array data in my main code: Code:
Function boardInit{} NEWTYPE .cells symbol.s row.w col.w End NEWTYPE Dim board.cells(20,20) For i=0 To 19 For j=0 To 19 board(i,j)\symbol="~" board(i,j)\row=i board(i,j)\col=j Next j Next i Function Return board.cells() End Function ... main code ... For i=0 To 19 For j=0 To 19 NPrint("symbol = "+ board(i,j)\symbol) NPrint("row = " + Str$(board(i,j)\row)) NPrint("col = " + Str$(board(i,j)\col)) VWait 10 Next j Next i Thanks in advance |
24 May 2018, 15:00 | #12 |
A1200 040 SAM440EP 667
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 873
|
Even the code copied fromhttps://www.amigacoding.com/index.ph...:Good_Practice
Code:
Statement resetgame{} SHARED board() For i = 1 To 10 For j = 1 To 10 board(i, j) = 0 Next j Next i End Statement |
24 May 2018, 15:28 | #13 |
Registered User
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,335
|
Hmmm, well as you're dealing with an array, things are a little more complicated, and I don't know how to pass pointers to arrays in the same way as a single Newtype. The only way I can see to do this is to first declare the Newtype and array outside the functions, and then share them for use within the functions where access is required (in your example, the boardInit{} function). This approach would look something like this:
Code:
NEWTYPE .cells symbol.s row.w col.w End NEWTYPE Dim board.cells(20,20) Function boardInit{} SHARED board() For i=0 To 19 For j=0 To 19 board(i,j)\symbol="~" board(i,j)\row=i board(i,j)\col=j Next j Next i End Function ... main code ... boardInit{} For i=0 To 19 For j=0 To 19 NPrint "symbol = "+ board(i,j)\symbol NPrint "row = " + Str$(board(i,j)\row) NPrint "col = " + Str$(board(i,j)\col) VWait 10 Next j Next i Individual Newtypes can be passed to and from a function as pointers, so if you really need that to happen (i.e., you need a new instance of the array every time the Init function is called), you need to put the array inside the Newtype. This might look something along these lines: Code:
NEWTYPE .cell symbol.s row.w col.w End NEWTYPE NEWTYPE .boardrow cells.cell[20] End NEWTYPE NEWTYPE .board rows.boardrow[20] End NEWTYPE Function boardInit{} DEFTYPE .board board For i=0 To 19 For j=0 To 19 board\rows[i]\cells[j]\symbol="~" board\rows[i]\cells[j]\row=i board\rows[i]\cells[j]\col=j Next j Next i Function Return &board ; Address of board Newtype we created End Function ... main code ... *myBoard = boardInit{} For i=0 To 19 For j=0 To 19 NPrint "symbol = "+ *myBoard\rows[i]\cells[j]\symbol NPrint "row = " + Str$(*myBoard\rows[i]\cells[j]\row) NPrint "col = " + Str$(*myBoard\rows[i]\cells[j]\col) VWait 10 Next j Next i Also, bear in mind that NPrint isn't a function, so doesn't use brackets. NPrint can also accept multiple arguments which it joins together in the same way as "adding" the strings, which means you don't need the Str$() functions: Code:
NPrint "col = ", *myBoard\rows[i]\cells[j]\col Edit: Just saw your second post now. That example from AmigaCoding.com requires the board() array to already be Dim'd outside the function, which is how it works in my first example above. Hope that makes sense |
24 May 2018, 16:37 | #14 | ||
A1200 040 SAM440EP 667
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 873
|
Hi Daedalus, thanks for your help
Quote:
I have not more code for now because I just starting whith the board but cannot find how to pass the matrix coordinates outside the function/statement and spend the spare time googling about it I will try your way with the NType outside statement Quote:
Anyway I will try your examples and see what I get. Thanks again friend |
||
24 May 2018, 16:52 | #15 | ||||
Registered User
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,335
|
Quote:
Quote:
Quote:
Quote:
|
||||
24 May 2018, 21:25 | #16 |
Registered User
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,335
|
I've just tried this exact code myself, and it compiles and runs fine in both Blitz 2.1 and AmiBlitz 3:
Code:
Dim board(10,10) Statement resetgame{} SHARED board() For i = 1 To 10 For j = 1 To 10 board(i, j) = 0 Next j Next i End Statement resetgame{} End |
25 May 2018, 00:45 | #17 |
Banana
Join Date: Jul 2016
Location: Darmstadt
Posts: 1,214
|
I don't know whether you're still trying pointers but I had some success in passing a pointer to a newtype by putting '&' in front of the variable name when calling the statement. As one would in c. The statement needs to take a pointer as its argument.
|
25 May 2018, 18:52 | #18 | |
A1200 040 SAM440EP 667
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 873
|
Quote:
My mistake on this one.. You're right, that code was copy & pasted from AmigaCoding.com that originated the error that was "garbage at the end of a line" but between the tries I was having errors of syntax that originated the errors confusion. Sorry about that |
|
25 May 2018, 19:09 | #19 |
A1200 040 SAM440EP 667
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 873
|
I have not tried pointers yet as I had no more spare time but I will considere that option when I could, thanks for the tip
|
25 May 2018, 22:13 | #20 | ||
Registered User
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,335
|
Quote:
Quote:
|
||
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
for..each statement in CLI | pieter1976 | support.Other | 10 | 09 January 2020 22:15 |
AmigaOS 3.1 source code leak - official statement | Cyborg | News | 71 | 09 January 2016 09:08 |
SECTION statement and the CHIP/FAST attribute | Apollo | Coders. Asm / Hardware | 3 | 15 June 2013 18:18 |
return newtype item no. in Blitz | leathered | Coders. Blitz Basic | 4 | 20 March 2013 11:59 |
Hyperion Statement on litigation with Amiga Inc. | Ultron | News | 2 | 02 May 2007 11:06 |
|
|