Jump to content

Zombie Peds.


therenex

Recommended Posts

HALLO =D

I'm making a nice zombie peds script like a multiplayer survival horror. But I have some problems. I always have some problems.

I spawn 5000 peds all over San Andreas. Then, to make them follow the nearest player, I use onClientRender, so every client checks if there are any near peds and make them follow the local player. To do this, I create a colshape (circle) around the player and get the ped elements in it, but it fucking lags.

Any ideas on how to do this?

Link to comment

Thanks. I tried using a timer, but it's kinda the same, it freezes two times per second lol.

I'm searching for a more useful method of getting the peds around me.

I currently use (all this is clientside) getElementsByType and then isElementStreamedIn.

I tried to create a colcircle around the player's position, that gets updated (or created again) everytime my function is called, but the circle simply won't create.

I don't know what to do. Please help me.

Link to comment

If you are using the night build it's probably better to use onClientPreRender to perform calculations. One way or the other you must find a way to limit your calculations to nearby zombies. Creating a massive loop every frame is not very smart. Your best option is to use onClientElementStreamIn and onClientElementStreamOut. Push ped elements onto a stack in onClientElementStreamIn and remove them when they stream out again. Iterate through the stack instead of getting all zombie peds every frame. This should at least bring back the workload significantly unless there is a high density of zombies. And you don't have to use collision shapes.

Link to comment

Thanks for the help.

@Talidan: after getting all the peds I use that function, but getting all the peds is really what lags the client, they're over 5000.

@Ace_Gambit: thanks for that, sounds like a great idea. However, I just have a minimal idea of what a stack is... could you give me an example, please?

Link to comment
Yep, the first thing i did was to put some output, it doesn't get triggered. It worked once and what it made my player attack himself because I wasn't checking if streamed objects were peds or not, but after I started checking, never worked again.

Check for spelling errors, it doesn't check if the event exists.

Also, make the event trigger on getRootElement() or add this handler for every ped on the server. Not something like getLocalPlayer(). (if this is the case)

Link to comment

Thank you, Gamesnert. That was my mistake. I used getLocalPlayer. But now that I use getRootElement, the script just pushes peds, and doesn't pop them.

This is the code that executes the push n' pop:

function onStreamIn ( ) 
    local tipo = getElementType ( source ) 
    if tipo == "ped" then 
        table.insert ( zombies, source ) 
        outputConsole ( "STACK PUSH!" ) 
    end 
end 
  
function onStreamOut ( ) 
    local tipo = getElementType ( source ) 
    if tipo == "ped" then 
        table.remove ( zombies, source ) 
        outputConsole ( "STACK POP!" ) 
    end 
end 
  
addEventHandler( "onClientElementStreamIn", getRootElement(), onStreamIn ) 
addEventHandler( "onClientElementStreamOut", getRootElement(), onStreamOut ) 

Can't see anything wrong with it.

Link to comment

I think I can. These lines are from the LUA Wiki, and represent the parameters for insert and remove...

table.insert(table, [pos,] value)
table.remove(table [, pos])

So actually, you're doing...

table.insert ( zombies
, #zombies+1[number, table value], source[ped, table value] )

table.remove ( zombies
, source[ped, table index] )

As you can see, the ped is first used as a value, since position is automatically set to a number. And then you try to remove it, with the ped as a position. But the position is a number, so it can't find the entry.

Knowing this, it's easier to find out what the prob is. Because you don't have a zombies[source].

I think it would be better to just do...

function onStreamIn ( ) 
    local tipo = getElementType ( source ) 
    if tipo == "ped" then 
        zombies[source]=true --Setting source as index, it would be pointless to also set it as value 
        outputConsole ( "STACK PUSH!" ) 
    end 
end 
  
function onStreamOut ( ) 
    local tipo = getElementType ( source ) 
    if tipo == "ped" then 
        zombies[source]=nil 
        outputConsole ( "STACK POP!" ) 
    end 
end 
  
addEventHandler( "onClientElementStreamIn", getRootElement(), onStreamIn ) 
addEventHandler( "onClientElementStreamOut", getRootElement(), onStreamOut ) 

And then when retrieving...

for k,v in pairs(zombies) do 
    whateveryouwannadowithemisuptoyouandtotallynotmydicision() 
end 

Keep in mind here that you should use k to return the ped, v will only return true! :P Also, it doesn't have numbered indexes, so that's why I used pairs instead of ipairs.

Hope this helps, ;)

Gamesnert.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...