Raised This Month: $51 Target: $400
 12% 

(REQ) Get random entity


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Nax0ne
Senior Member
Join Date: Jul 2011
Location: Chile
Old 11-15-2017 , 11:56   (REQ) Get random entity
Reply With Quote #1

Hello everyone! How can I get an "entity" at random, as if it were a player, but in reality it is an NPC. For example, a random NPC is selected as a "hero" but I have done it in different ways and it does not work for me.


Help mee :'(

PHP Code:
make_npc_hero( )
{    
    new Array:
entities ArrayCreate(132);    
    new 
ent = -1iHero;
    
    while( ( 
ent find_ent_by_classent"npc" ) ) ) 
    {
         
ArrayPushCellentitiesent ); 
        new 
iNpcTotal ArraySizeentities );    
        
        if ( 
randomiNpcTotal ) )
            
g_iNPCIsHeroent ] = 1;
    }
    
    
server_print"CURRENT HERO %i"ent );

__________________

Last edited by Nax0ne; 11-15-2017 at 11:59.
Nax0ne is offline
DjSoftero
Veteran Member
Join Date: Nov 2014
Location: Lithuania
Old 11-15-2017 , 13:40   Re: (REQ) Get random entity
Reply With Quote #2

get all the npc ids, put them into an array, then choose a random number from 0 to the amount of npcs you have.
and then choose that element of an array you randomly generated.

edit...
I looked at the code after i wrote this, it seems you were trying to do what i just told you to.

i guess this would be more like it:
PHP Code:
public make_npc_hero( )
{    
    new Array:
entities ArrayCreate(1)  
    new 
ent = -1iHero
    
    
while((ent find_ent_by_class(ent"npc")))
         
ArrayPushCell(entitiesent)

    new 
iNpcTotal ArraySize(entities)
    new 
random_hero_id ArrayGetCell(entitiesrandom(iNpcTotal 1))
    
server_print("CURRENT HERO %i"random_hero_id)

__________________
retired chump

Last edited by DjSoftero; 11-15-2017 at 13:54.
DjSoftero is offline
Nax0ne
Senior Member
Join Date: Jul 2011
Location: Chile
Old 11-15-2017 , 14:10   Re: (REQ) Get random entity
Reply With Quote #3

Quote:
Originally Posted by DjSoftero View Post
get all the npc ids, put them into an array, then choose a random number from 0 to the amount of npcs you have.
and then choose that element of an array you randomly generated.

edit...
I looked at the code after i wrote this, it seems you were trying to do what i just told you to.

i guess this would be more like it:
PHP Code:
public make_npc_hero( )
{    
    new Array:
entities ArrayCreate(1)  
    new 
ent = -1iHero
    
    
while((ent find_ent_by_class(ent"npc")))
         
ArrayPushCell(entitiesent)

    new 
iNpcTotal ArraySize(entities)
    new 
random_hero_id ArrayGetCell(entitiesrandom(iNpcTotal 1))
    
server_print("CURRENT HERO %i"random_hero_id)

Mmm i tested and it didn't work. But i changed this line:

PHP Code:
ArrayGetCell(entitiesrandom(iNpcTotal 1)) 
for this one:
PHP Code:
ArrayGetCell(entitiesrandom_num(0iNpcTotal 1)) 
And it works ! Thank you man for your help.
__________________
Nax0ne is offline
DjSoftero
Veteran Member
Join Date: Nov 2014
Location: Lithuania
Old 11-16-2017 , 07:46   Re: (REQ) Get random entity
Reply With Quote #4

that can`t possibly be the reason for this not to work. those functions you showed are made the same way, exscept random_num() isn`t limited to 0 as a first number.
__________________
retired chump

Last edited by DjSoftero; 11-16-2017 at 07:46. Reason: misstype
DjSoftero is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 11-16-2017 , 19:24   Re: (REQ) Get random entity
Reply With Quote #5

random_num( 0, x )

and

random( x )

are the same.

They both will return a random number from 0 to x.
__________________

Last edited by edon1337; 11-16-2017 at 19:24.
edon1337 is offline
Nax0ne
Senior Member
Join Date: Jul 2011
Location: Chile
Old 11-16-2017 , 22:00   Re: (REQ) Get random entity
Reply With Quote #6

Quote:
Originally Posted by DjSoftero View Post
that can`t possibly be the reason for this not to work. those functions you showed are made the same way, exscept random_num() isn`t limited to 0 as a first number.
EDIT: Dont worry, i have solved the problem of saving the origin.

Mmm how i said, works for me the second line. I dont know why the first line doesn't work. or i did something unexpected haha...

But the function is perfect. However, when I want to detect the origin of that "random" entity (hero), it is not saved... how i can do to save the origin entity, in this case the "hero", to use for others functions (for example to calculate the distance between the hero and the player) ???


PHP Code:
public make_npc_hero( )
{    
    new Array:
entities ArrayCreate);  
    new 
ent = -1Float:flOrigin];
    
    while ( (
ent find_ent_by_classent"npc" ) ) )
        
ArrayPushCellentitiesent );

    new 
iNpcTotal ArraySizeentities );
    new 
random_hero_id ArrayGetCellentitiesrandomiNpcTotal ) );
        
    
server_print"CURRENT HERO %i"random_hero_id );
    
pevrandom_hero_idpev_originflOrigin );
        
    
// How to save that origin??

__________________

Last edited by Nax0ne; 11-16-2017 at 23:38.
Nax0ne is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 11-17-2017 , 08:06   Re: (REQ) Get random entity
Reply With Quote #7

Random(0) will generate a random number....

Check if the iNpcTotal is above 0
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 11-17-2017 at 08:16.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
Reply



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 -4. The time now is 01:16.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode