English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. Blitz Basic

 
 
Thread Tools
Old 07 June 2016, 10:37   #1
earok
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
earok is offline  
Old 07 June 2016, 20:27   #2
Cylon
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
Please mind this way you are using the variable (type) itself, normally a new one would have been created (copied) inside the Function. This is not the case here, we are passing the address only.
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
Cylon is offline  
Old 08 June 2016, 00:53   #3
earok
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
So I just set *ThisProjectile before calling the statement.
earok is offline  
Old 08 June 2016, 08:10   #4
idrougge
Registered User
 
Join Date: Sep 2007
Location: Stockholm
Posts: 4,332
Is the asterisk really necessary in your case, Earok?
idrougge is offline  
Old 08 June 2016, 20:54   #5
Daedalus
Registered User
 
Daedalus's Avatar
 
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.
Daedalus is offline  
Old 09 June 2016, 03:55   #6
earok
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
earok is offline  
Old 09 June 2016, 10:56   #7
Daedalus
Registered User
 
Daedalus's Avatar
 
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
Should work fine and doesn't use pointers. The alternative is using pointers and passing the address as in Cylon's example above, and this doesn't use shared variables. Generally you wouldn't need to do both, just one or the other. Personally I would favour the shared method I showed, but passing pointers could be considered better programming technique.

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
and then access your projectiles array as normal inside the statement. Again, it might not be considered the best coding practice, but at least it avoids some of the pitfalls of using pointers and avoids having to assign them before calling and inside the statement. Your code probably works, but it's sort of taking the less good aspects of each technique and combining them. If you're happy with that, then great

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.
Daedalus is offline  
Old 09 June 2016, 11:02   #8
earok
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
earok is offline  
Old 09 June 2016, 11:30   #9
Daedalus
Registered User
 
Daedalus's Avatar
 
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.
Daedalus is offline  
Old 12 June 2016, 02:35   #10
Cylon
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.
Cylon is offline  
Old 24 May 2018, 11:22   #11
AlfaRomeo
A1200 040 SAM440EP 667
 
AlfaRomeo's Avatar
 
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
Can someone give me a tip on this?
Thanks in advance
AlfaRomeo is offline  
Old 24 May 2018, 15:00   #12
AlfaRomeo
A1200 040 SAM440EP 667
 
AlfaRomeo's Avatar
 
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
give me a syntax error
AlfaRomeo is offline  
Old 24 May 2018, 15:28   #13
Daedalus
Registered User
 
Daedalus's Avatar
 
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
It's not really clear however from your example as to what the function of returning the array would be. If you only ever intend to have one array, and use the Init function to set that same array up each time, the above approach should be fine.

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
I presume the \row and \col values will be updated at some point? Because if not they're pretty redundant Again, it's hard to tell the intention without more code, but just thought I'd mention it in case things are simply getting over-complicated.

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
Finally, I should point out that I haven't tested this code...

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
Daedalus is offline  
Old 24 May 2018, 16:37   #14
AlfaRomeo
A1200 040 SAM440EP 667
 
AlfaRomeo's Avatar
 
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 873
Hi Daedalus, thanks for your help

Quote:
Originally Posted by Daedalus View Post
It's not really clear however from your example as to what the function of returning the array would be. If you only ever intend to have one array, and use the Init function to set that same array up each time, the above approach should be fine.

...

I presume the \row and \col values will be updated at some point? Because if not they're pretty redundant Again, it's hard to tell the intention without more code, but just thought I'd mention it in case things are simply getting over-complicated.
I will need \row, \col and \symbol as coordinates and characters of a board so I could positioning some ascii characters and know where they are when moving.
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:
Originally Posted by Daedalus View Post
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
I tried the AmigaCoding.com example with "Dim board(10,10)" already created outside statement but it give me a syntax error

Anyway I will try your examples and see what I get.
Thanks again friend
AlfaRomeo is offline  
Old 24 May 2018, 16:52   #15
Daedalus
Registered User
 
Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,335
Quote:
Originally Posted by AlfaRomeo View Post
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
Quote:
I will try your way with the NType outside statement
Cool, well if you're only looking to pass a single set of coordinates, and not the entire array, the earlier examples of passing a single Newtype as a pointer might be what you need. Hopefully you get something working anyway.

Quote:
I tried the AmigaCoding.com example with "Dim board(10,10)" already created outside statement but it give me a syntax error
That is kinda strange, on what line does it show the error? I can't see what's happening there but I'll check it out later on (I wrote that section on AmigaCoding.com so I really hope there isn't something obvious I'm missing ). Just a thought: How did you get the code into Blitz? I wonder if there's something strange like mismatched line endings that Blitz is choking on, for example if it was saved on PC first and then loaded in Blitz...

Quote:
Anyway I will try your examples and see what I get.
Thanks again friend
No problem, let us know how it goes!
Daedalus is offline  
Old 24 May 2018, 21:25   #16
Daedalus
Registered User
 
Daedalus's Avatar
 
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
So I'm really not sure what's causing the syntax errors you're seeing I'm afraid.
Daedalus is offline  
Old 25 May 2018, 00:45   #17
E-Penguin
Banana
 
E-Penguin's Avatar
 
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.
E-Penguin is offline  
Old 25 May 2018, 18:52   #18
AlfaRomeo
A1200 040 SAM440EP 667
 
AlfaRomeo's Avatar
 
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 873
Quote:
Originally Posted by Daedalus View Post
Cool, well if you're only looking to pass a single set of coordinates, and not the entire array, the earlier examples of passing a single Newtype as a pointer might be what you need. Hopefully you get something working anyway.
The idea is when I need to reinitiate de game I will call that function and at some points I will get some coordinates too.

Quote:
Originally Posted by Daedalus View Post
Just a thought: How did you get the code into Blitz? I wonder if there's something strange like mismatched line endings that Blitz is choking on, for example if it was saved on PC first and then loaded in Blitz...
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
AlfaRomeo is offline  
Old 25 May 2018, 19:09   #19
AlfaRomeo
A1200 040 SAM440EP 667
 
AlfaRomeo's Avatar
 
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 873
Quote:
Originally Posted by E-Penguin View Post
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.
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
AlfaRomeo is offline  
Old 25 May 2018, 22:13   #20
Daedalus
Registered User
 
Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,335
Quote:
Originally Posted by AlfaRomeo View Post
The idea is when I need to reinitiate de game I will call that function and at some points I will get some coordinates too.
That make sense. For that to work, all you'll need to do is Dim the array outside the function and use the SHARED command to make the array available inside the function, then you won't have to worry about passing pointers or using nested Newtypes. Simple is good

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
No worries, it's one of those things that's very easy to forget. If you're passing text files, opening them and saving them again in another editor might be a way of working around the issue. Failing that, there are a number of ASCII line feed / carriage return converters on Aminet. You could even write your own
Daedalus is offline  
 


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 06:12.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.09676 seconds with 13 queries