View Single Post
Old 09 June 2016, 11:56   #7
Daedalus
Registered User

Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 3,406
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 12:09.
Daedalus is online now  
 
Page generated in 0.04022 seconds with 11 queries