English Amiga Board


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

 
 
Thread Tools
Old 01 January 2018, 02:11   #1
gazj82
Registered User
 
gazj82's Avatar
 
Join Date: Jan 2014
Location: Cambs / UK
Posts: 356
Blitz Basic - NEWTYPES

Happy new year all!

I need some help with NEWTYPES in blitz basic.

Here is some of my code
Code:
.variables
DEFTYPE .w
NEWTYPE .sprite
x.w
y.w
oldx.w
oldy.w
frame.w
speed.w
direction.w
olddirection.w
End NEWTYPE
DEFTYPE .sprite player
DEFTYPE .sprite enemy1
DEFTYPE .sprite enemy2
Now player works fine, just as expected. The trouble is I want to have two (or more) of the same enemy following the same code using a for loop. When I used to use dim'd arrays for this I could do this easily. How can I reference the right enemyX in a for loop. I guess it would be something like this.

Code:
for ene=1 to 2
enemy(ene)\x + 1
next ene
Surely there must be a way of doing this and I just don't know the syntax?
gazj82 is offline  
Old 01 January 2018, 03:39   #2
earok
Registered User
 
Join Date: Dec 2013
Location: Auckland
Posts: 3,539
That looks just fine to me (except next ene is redundant though? just use next?). Do remember that arrays are zero indexed though. Here's an example (truncated) from an actual game project of mine.

NEWTYPE .Entity
ID.b
X.q
Y.q
End NEWTYPE

Dim Players.Entity(4)
for i = 0 to 3
Players(i)\ID=i
Players(i)\Y=300
next
earok is offline  
Old 01 January 2018, 18:27   #3
Daedalus
Registered User
 
Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,334
Arrays are zero indexed, but also count the index of the amount they're DIM'd to. For example, Players.Entity(4) has 5 elements, indexed 0, 1, 2, 3 and 4.

This is not the case for arrays *within* Newtypes (e.g. Players\skill[1] will only have a [0] index), but arrays of primitives and Newtypes have n+1 members.

As for another way of doing it, there isn't really. The data you want to access is within an array, so you must provide an index. However, there is a compiler shortcut you can use: the Usepath command. This tells the compiler the default Newtype root to use, and can include variables. So, to borrow earok's example:

Code:
Dim Players.Entity(4)

Usepath Players(i)

for i = 0 to 3
\ID=i
\Y=300
next
Finally, the variable with the Next keyword is optional, but I make a habit of including it anyway, since it helps avoid potential bugs when you have a number of nested loops. But that's just me - I also like to use the enforced type declarations of AmiBlitz3 too
Daedalus is offline  
Old 02 January 2018, 01:26   #4
gazj82
Registered User
 
gazj82's Avatar
 
Join Date: Jan 2014
Location: Cambs / UK
Posts: 356
Should have replied ages ago. Thanks Earok your post got me moving again. I did no dim'd arrays start at 0.

Thank you for your help to Deadalus
gazj82 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
[blitz basic] How much amiga-blitz friendly is this? saimon69 Coders. Blitz Basic 105 21 April 2022 19:45
Blitz Basic vs E vs Something Else naviward Coders. General 14 28 May 2016 21:38
Blitz Basic (1) Retro1234 Coders. Blitz Basic 9 18 February 2016 17:54
Blitz basic 2 Help Havie Coders. Blitz Basic 30 08 September 2013 09:15
blitz basic petza request.Apps 11 08 April 2007 01:49

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 03:53.

Top

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