English Amiga Board


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

 
 
Thread Tools
Old 18 March 2013, 22:42   #1
leathered
Registered User
 
leathered's Avatar
 
Join Date: Oct 2011
Location: UK
Age: 47
Posts: 304
return newtype item no. in Blitz

I'm trying to write a piece of blitz basic 2 code that will allow me to return the number of the currently used list item.
Say if I have a newtype variable path "bobs()\num" in a while/wend loop and I want to return the number of the current item in the list on a certain condition to enable passing it to other variables and act on it etc.

I've somehow not been able to do this although it's never been a problem with normal arrays. I can access the individual items of the list using a subscript "bobs(var)\num" as a normal array, however I cannot find the means in blitz to return the (var) above which is currently being processed in a list.

I've taken to changing all newtype variables into normal arrays - presenting it's own problems! I'm stuck with the above problem, any ideas?

EDIT: To help clarify this I'll better explain the problem.
In my main loop is a section dealing with a particular action enforced by the player onto other objects. The objects are part of a list, and the particular action is represented by another variable. I simply want to pass the number of the listed object in question to this variable.
I have an idea that using a 'pointer' type variable could work, but it seems strange that it should be necessary given that the lists have pointers of their own already, and I can access them directly using the subscript number.
code example:
Code:
resetlist thislist()
while nextitem thislist()
if thislist()\condition =1 then thisvariable= thislist()
wend

; same example using normal array
for N=0 to ENDLIST
if thisitem(N)=1 then thisvariable=N
next N
Whereupon afterward I can act upon fields of the affected item using thislist(thisvariable)/etc.

I'm not sure that I'm meant to do this with Blitz2 as it warns in the manual about using usual array indexing, for the most part however, it works - albeit missing the last part of the puzzle.

Last edited by leathered; 19 March 2013 at 14:05.
leathered is offline  
Old 19 March 2013, 19:40   #2
Leffmann
 
Join Date: Jul 2008
Location: Sweden
Posts: 2,269
Maybe I'm misunderstanding you, but can't you just keep count manually, or are you looking for a more elegant solution?

Code:
n = 0
ResetList list()
While NextItem(list())
 If list()\cond = 1 Then index = n
 n+1
Wend
Leffmann is offline  
Old 20 March 2013, 02:06   #3
leathered
Registered User
 
leathered's Avatar
 
Join Date: Oct 2011
Location: UK
Age: 47
Posts: 304
Quote:
Originally Posted by Leffmann View Post
Maybe I'm misunderstanding you, but can't you just keep count manually, or are you looking for a more elegant solution?

Code:
n = 0
ResetList list()
While NextItem(list())
 If list()\cond = 1 Then index = n
 n+1
Wend
Thanks for replying, you understood perfectly well and I've been able to find a solution.

The problem appeared to be happening when items are removed from the list and then added again. The listed objects are enemy bobs for a game, and are currently removed from the list when 'killed' and added again when needed. Blitz does this quite elegantly, and for the most part works fine.
However I've still found no way of accurately returning the number of the listed item. If 'index' above is a variable then the code you've suggested would work first time around but fall apart once the item has been killed and added again - the listed item number apparently does not match the number being counted. I think that under some circumstances it would, but in my case it doesn't; I'm not quite able to explain why... I think that in many circumstances it simply wouldn't matter as long as the fields were always being accessed in the same way, whereas I'm using a few methods depending on the situation.

While typing this I remembered there was another section of code where I was doing something very similar to your suggestion. In the end the solution was simply to move the problem area outside of the 'loopy list' and count the items manually with another loop.
(typed properly this time )

Code:
;GR counter
for GRNUM=0 to nn
if nast(GRNUM)\gr=>1 then bobs()\gr=nast(GRNUM)\gr:gbd=GRNUM
next GRNUM

;initialise
if INI=1
resetlist nast()
while nextitem (nast())
nast()\etc1=etc
;and so on...
Some point earlier I was trying to minimise the amount of looping going on. The code worked fine until I started 'recycling' the objects.

Thankyou Leffmann and credit to you sir . Your kind inspiration has saved me at least a weeks work

ps. I was very curious of your use of the word 'index'. I'd assumed you meant a variable. Assumption being what it is I tried it and found the 'index list' command. So I may have misunderstood you and perhaps there's a way of returning the 'currently used item' after all - I mean, there must be
leathered is offline  
Old 20 March 2013, 03:52   #4
Leffmann
 
Join Date: Jul 2008
Location: Sweden
Posts: 2,269
Yes I meant for "index" to just be an integer variable, but once you remove f.ex the 5th item from the list then the following item will be the new 5th and so on, so the index may be no good anymore.

PushItem and PopItem works for keeping track of an item directly, but it's a last-in-first-out stack and can be a bit inconvenient, but may work for what you're doing.
Leffmann is offline  
Old 20 March 2013, 11:59   #5
leathered
Registered User
 
leathered's Avatar
 
Join Date: Oct 2011
Location: UK
Age: 47
Posts: 304
I'd thought that I was re-initialising the 'fake' index numbers with every 'new item' - but you're right and that would explain the difficulty. I've encountered trouble with blitz and the stack before, but was wondering if PushItem and PopItem would do the job as you've confirmed.
So there's a couple of new ways I can get the job done when it comes to optimising again.
TBH I think I've been staring at a computer screen for too many late nights for too long. I've got a holiday coming up and thought I'd be de-bugging for the whole trip, but as it is I'll be doing some chilling (literally, in Scotland )
Thanks again Leffmann; your help is most appreciated
leathered 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
What is this Amiga Item? Peter support.Hardware 9 29 July 2010 18:05
Possible collectors item? CRAZG Amiga scene 1 28 May 2007 00:14
M&M3 item evaluator Amigamancer Retrogaming General Discussion 4 04 April 2005 23:31
another ebay item that just cracked me up sarek2k MarketPlace 17 15 March 2005 08:04
Here's a rare item! th4t1guy MarketPlace 7 15 June 2004 11:03

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:11.

Top

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