SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 18/07/2020, 06:34 AM   #1
SkyFlare
Huge Clucker
 
SkyFlare's Avatar
 
Join Date: Apr 2015
Location: NZ
Posts: 221
Reputation: 43
Default New to Loops such as these ones, any advice?

This function is to get the Free Biz ID in my Database... (well actually it doesnt read the database)
Ex: I have BIZ ids 1,2,3,5: Output: Biz ID 4 is free.
This is what the loop looks like and I am just wondering if there's a smarter way to do this, it works right now though.

But I don't think it'll work if I change it in realtime, as the Array's ID's will change if I add ID 4 in, and delete ID 2, my result may not output 2....
PHP Code:
GetFreeBizID()
{
    new 
ResultLastID;
    for(new 
0MAX_BIZSi++)
    {
        if(
== 0)
        {
            
printf("Iteration [%i]"i);//Result 0
            
printf("DynamicBizs[i][ID]: [%i]"DynamicBizs[i][ID]);// Result 1
            
LastID DynamicBizs[i][ID];
            continue;
        }
        if(
LastID == DynamicBizs[i][ID]-&& DynamicBizs[i][ID] == LastID +1)
        {
            
LastID DynamicBizs[i][ID];
            
printf("Iteration [%i]"i);//Result 0
            
printf("DynamicBizs[i][ID]: [%i]"DynamicBizs[i][ID]);// Result 1
            
continue;
        }
        if(
LastID == DynamicBizs[i][ID]-2)
        {
            
Result i+1;
            break;
        }
    }
    
printf("GetFreeBizID Result: %i"Result);
    return 
Result;

__________________
Purchasable Scripting Services Available, Inbox me with your Requests.
My Project Progress Tracker
My Work
Link
Information
S-Store System V2 LinkIngame Store in Dialogs
SkyFlare is offline   Reply With Quote
Old 18/07/2020, 07:43 AM   #2
grymtn
Huge Clucker
 
Join Date: Jun 2013
Posts: 272
Reputation: 26
Default Re: New to Loops such as these ones, any advice?

I dont exactly understand what you are trying to do but if you are just trying to get one id, something like below should work. If you remove break you will see all ids.

Important edit: You should never match database id with in game object id that your script gives. Just create another variable called dbid in your enum and do your database jobs on that id. Its fool proof and less resource requiring since you are taking down your loop workload.

Code:
for(new i=0; i<maxbiz; i++)
{
    if(biz[i][taken]==free)
    {
        //The id you want is i. i is free. Do your action here.
        Break;
    }
}
__________________
grymtn is offline   Reply With Quote
Old 18/07/2020, 08:01 AM   #3
GameOvr
Huge Clucker
 
GameOvr's Avatar
 
Join Date: Jun 2018
Posts: 296
Reputation: 9
Default Re: New to Loops such as these ones, any advice?

This isn't the way to do it. You should consider array as the ID. do what grymton told. make a seperate token called taken and make it true when taken and false when its removed then loop through all and take the array index which taken is false using break;
__________________
GameOvr is offline   Reply With Quote
Old 18/07/2020, 08:11 AM   #4
SkyFlare
Huge Clucker
 
SkyFlare's Avatar
 
Join Date: Apr 2015
Location: NZ
Posts: 221
Reputation: 43
Default Re: New to Loops such as these ones, any advice?

Welp, doesn't matter I totally forgot about Y_Iterate
__________________
Purchasable Scripting Services Available, Inbox me with your Requests.
My Project Progress Tracker
My Work
Link
Information
S-Store System V2 LinkIngame Store in Dialogs
SkyFlare is offline   Reply With Quote
Old 18/07/2020, 08:48 AM   #5
xRadical3
Gangsta
 
Join Date: May 2017
Location: Iran
Posts: 694
Reputation: 63
Default Re: New to Loops such as these ones, any advice?

Quote:
Originally Posted by SkyFlare View Post
Welp, doesn't matter I totally forgot about Y_Iterate
Create a iter variable.
pawn Code:
new Iterator:Business<MAX_BUSINESS>;

When you load or create new business, do this for add to iter:
pawn Code:
Iter_Add(Business, id);

Now you can only get the free slot with this function:
pawn Code:
Iter_Free(Business);

Last edited by xRadical3; 19/07/2020 at 09:51 AM.
xRadical3 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need some advice babecka Scripting Help 0 25/06/2014 09:38 AM
[HELP] advice CNN Luca12 Scripting Help 4 09/05/2014 04:42 PM
Your advice? GuyYahood1 General 4 09/03/2014 08:24 PM
VPS Advice jonrb Server Support 8 28/09/2011 07:04 PM
Need a little advice. OldDirtyBastard Help Archive 6 04/11/2010 06:51 PM


All times are GMT. The time now is 07:31 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.