SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 29/11/2019, 04:04 PM   #1
KayJ
Gangsta
 
KayJ's Avatar
 
Join Date: Apr 2014
Posts: 678
Reputation: 64
Default Random Objects and random spawn

How do I create random objects(say like 500 objects) from a list of selected item IDs(like gun/bottle/etc) into random places using MapAndreas?
KayJ is offline   Reply With Quote
Old 29/11/2019, 04:35 PM   #2
NoteND
Huge Clucker
 
Join Date: May 2016
Posts: 252
Reputation: 5
Default Re: Random Objects and random spawn

Code:
stock randomEx(min, max)
{      
    new rand = random(max-min)+min;    
    return rand;
} 

new Objects[600] = {1, 2, 3, 4, 5, 6...}

SpawnObjects()
{
	for(new i = 0; i <= 500; i++)
	{
		new Float:X, Float:Y, Float:Z;
		
		new rand = Objects[random(600)];
		
		X = randomEx(-3000, 3000);
		Y = randomEx(-3000, 3000);
		
		MapAndreas_FindZ_For2DCoord(X, Y, Z);
		
		CreateObject(rand, X, Y, Z, 0.0, 0.0, 0.0, 200.0);
	}
	return 1;
}
Use "SpawnObjects();" under command or where ever you want to use.

Not tested.

Last edited by NoteND; 29/11/2019 at 05:27 PM.
NoteND is offline   Reply With Quote
Old 30/11/2019, 02:52 AM   #3
KayJ
Gangsta
 
KayJ's Avatar
 
Join Date: Apr 2014
Posts: 678
Reputation: 64
Default Re: Random Objects and random spawn

Quote:
Originally Posted by NoteND View Post
Code:
stock randomEx(min, max)
{      
    new rand = random(max-min)+min;    
    return rand;
} 

new Objects[600] = {1, 2, 3, 4, 5, 6...}

SpawnObjects()
{
	for(new i = 0; i <= 500; i++)
	{
		new Float:X, Float:Y, Float:Z;
		
		new rand = Objects[random(600)];
		
		X = randomEx(-3000, 3000);
		Y = randomEx(-3000, 3000);
		
		MapAndreas_FindZ_For2DCoord(X, Y, Z);
		
		CreateObject(rand, X, Y, Z, 0.0, 0.0, 0.0, 200.0);
	}
	return 1;
}
Use "SpawnObjects();" under command or where ever you want to use.

Not tested.
It randomly creates my medkit id but the problem is it randomly creates model ID 0 too..
Also how do I add labels to them like I've object M4, a burger and pizza ?
KayJ is offline   Reply With Quote
Old 30/11/2019, 11:36 AM   #4
NoteND
Huge Clucker
 
Join Date: May 2016
Posts: 252
Reputation: 5
Default Re: Random Objects and random spawn

Change this line

Code:
new rand = Objects[random(600)];
to

Code:
new rand = random(sizeof(Objects));
and for labels.. you need to create new array for example

Code:
ObjectsNames[600][32] = {"M4", "Aidkit", "AK-47"...}
but names need to be on same number as object ids.
NoteND is offline   Reply With Quote
Old 30/11/2019, 12:08 PM   #5
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,713
Reputation: 1671
Default Re: Random Objects and random spawn

Quote:
Originally Posted by KayJ View Post
It randomly creates my medkit id but the problem is it randomly creates model ID 0 too..
Also how do I add labels to them like I've object M4, a burger and pizza ?
random(599) + 1

Random 600 returns a random integer (0 up to 599). Random 599+1 returns 1 up to 599
__________________
When the opportunity presents itself to flip-a da table, uh, you flip-a da table.

Discord: Kwarde#1966
Kwarde is offline   Reply With Quote
Old 30/11/2019, 12:29 PM   #6
KayJ
Gangsta
 
KayJ's Avatar
 
Join Date: Apr 2014
Posts: 678
Reputation: 64
Default Re: Random Objects and random spawn

Quote:
Originally Posted by NoteND View Post
Change this line

Code:
new rand = Objects[random(600)];
to

Code:
new rand = random(sizeof(Objects));
and for labels.. you need to create new array for example

Code:
ObjectsNames[600][32] = {"M4", "Aidkit", "AK-47"...}
but names need to be on same number as object ids.
Code:
D:\GAMES\SA-MP\gamemodes\gamemode.pwn(1271) : warning 203: symbol is never used: "Objects"
Pawn compiler 3.2.3664	 	 	Copyright (c) 1997-2006, ITB CompuPhase


1 Warning.
PHP Code:
new Objects [600] = {117361582};

SpawnObjects()
{
    for(new 
0<= 500i++)
    {
        new 
Float:XFloat:YFloat:Z;

          new 
rand random(sizeof(Objects));

        
randomEx(-30003000);
        
randomEx(-30003000);

        
MapAndreas_FindZ_For2DCoord(XYZ);

        if(
>= 5.0 && 30.0)
        {
            
CreateObject(randXYZ0.00.00.0200.0);
        }
    }
    return 
1;

Now it crashes my game with opcode warnings
KayJ is offline   Reply With Quote
Old 30/11/2019, 01:02 PM   #7
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,713
Reputation: 1671
Default Re: Random Objects and random spawn

Your array has a size of 600 cells but you only initialised two object IDs. So index 0 and 1 have this object ID, all other indexes have '0'. It is creating objects with ID 0 most likely crashing you.
Don't define an array size if you have initial data, or make sure that all unused cells are holding a valid object ID, eg. :
Code:
new Objects[600] = {1550, 1998, ...};
This will set index 0 of Objects to 1550 and all other cells to 1998. This is a waste of memory though. I don't recommend that. Stick to:
Code:
new Objects[] = {1550, 1998};
__________________
When the opportunity presents itself to flip-a da table, uh, you flip-a da table.

Discord: Kwarde#1966
Kwarde is offline   Reply With Quote
Old 30/11/2019, 03:45 PM   #8
KayJ
Gangsta
 
KayJ's Avatar
 
Join Date: Apr 2014
Posts: 678
Reputation: 64
Default Re: Random Objects and random spawn

Quote:
Originally Posted by Kwarde View Post
Your array has a size of 600 cells but you only initialised two object IDs. So index 0 and 1 have this object ID, all other indexes have '0'. It is creating objects with ID 0 most likely crashing you.
Don't define an array size if you have initial data, or make sure that all unused cells are holding a valid object ID, eg. :
Code:
new Objects[600] = {1550, 1998, ...};
This will set index 0 of Objects to 1550 and all other cells to 1998. This is a waste of memory though. I don't recommend that. Stick to:
Code:
new Objects[] = {1550, 1998};
PHP Code:
new Objects [] = {117361582};

SpawnObjects()
{
    for(new 
0<= 500i++)
    {
        new 
Float:XFloat:YFloat:Z;

          new 
rand random(sizeof(Objects));

        
randomEx(-30003000);
        
randomEx(-30003000);

        
MapAndreas_FindZ_For2DCoord(XYZ);

        if(
>= 5.0 && 30.0)
        {
            
CreateObject(randXYZ0.00.00.0200.0);
        }
    }
    return 
1;

I tried this but got error .
Code:
D:\GAMES\SA-MP\gamemodes\gamemode.pwn(1271) : warning 203: symbol is never used: "Objects"
Pawn compiler 3.2.3664	 	 	Copyright (c) 1997-2006, ITB CompuPhase


1 Warning.
PHP Code:
new Objects [] = {117361582};

SpawnObjects()
{
    for(new 
0<= 500i++)
    {
        new 
Float:XFloat:YFloat:Z;

          new 
rand Objects[random(599) + 1];

        
randomEx(-30003000);
        
randomEx(-30003000);

        
MapAndreas_FindZ_For2DCoord(XYZ);

        if(
>= 5.0 && 30.0)
        {
            
CreateObject(randXYZ0.00.00.0200.0);
        }
    }
    return 
1;

There's no object spawning.

PHP Code:
new Objects [] = {117361582};

SpawnObjects()
{
    for(new 
0<= 500i++)
    {
        new 
Float:XFloat:YFloat:Z;

          new 
rand Objects[600];

        
randomEx(-30003000);
        
randomEx(-30003000);

        
MapAndreas_FindZ_For2DCoord(XYZ);

        if(
>= 5.0 && 30.0)
        {
            
CreateObject(randXYZ0.00.00.0200.0);
        }
    }
    return 
1;

Used this code but still not working as it should do. Same model id 0 thing
I also need like more than 1000 objects all-around map so help me with dynamic object
KayJ is offline   Reply With Quote
Old 30/11/2019, 04:52 PM   #9
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,713
Reputation: 1671
Default Re: Random Objects and random spawn

Provided code will return runtime errors as you may try to access non existing array indexes.

Code:
new rand = random(sizeof(Objects));
Size of Objects is 2 at this point so 'rand' can be 0 or 1.

Code:
CreateObject(Objects[rand], ...);
Create object with modelid from Objects at index 'rand', which is 0 or 1.
Your code tries to create object 'rand' which is random 599 + 1: It will try to create object ID 1-599 (depending.on what rand is).

You should find an tutorial about how arrays work
__________________
When the opportunity presents itself to flip-a da table, uh, you flip-a da table.

Discord: Kwarde#1966
Kwarde is offline   Reply With Quote
Old 01/12/2019, 02:57 AM   #10
KayJ
Gangsta
 
KayJ's Avatar
 
Join Date: Apr 2014
Posts: 678
Reputation: 64
Default Re: Random Objects and random spawn

PHP Code:
new ObjectNames[][32] =
{
    
"First Aid Kit"//11736
    
"Pizza"//1582
    
"Bean Can"//1666
    
"Alice Backpack" //3026
};

new 
Objects [] ={1173615821666 ,3026};

SpawnObjects()
{
    for(new 
0<= 5000i++)
    {
        new 
Float:XFloat:YFloat:Z;

          new 
rand random(sizeof(Objects));
          new 
rand1 random(sizeof(ObjectNames));

        
randomEx(-30003000);
        
randomEx(-30003000);

        
MapAndreas_FindZ_For2DCoord(XYZ);

        if(
>= 5.0 && 30.0)
        {
            
CreateObject(Objects[rand], XYZ0.00.00.0200.0);
            
Create3DTextLabel(ObjectNames[rand1], 0x33AA33FF2.43XYZ00);
        }
    }
    return 
1;

The labels are not showing now.

Last edited by KayJ; 01/12/2019 at 03:32 AM.
KayJ 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
[Tutorial] Random Aspects - Using numargs(), getarg(...) and random(...) to make random systems dynamic and easier! SickAttack Tutorials 7 10/05/2015 10:39 AM
Objects on random position. HY Scripting Help 6 03/03/2015 06:29 PM
[Ajuda] random de spawn MustangGT PortuguÍs/Portuguese 7 05/03/2013 04:22 PM
Random Fire Objects [HELP] Sting. Scripting Help 3 17/11/2012 12:10 PM
Players spawn with random objects HondaCBR Scripting Help 3 30/12/2011 02:06 PM


All times are GMT. The time now is 09:01 AM.


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