Help with your scripting

Random drop chance

Random drop chance

Postby epsilonion » 29 Jul 2015, 01:02

So, I have been thinking about a random drop chance when you kill an enemy.
This is what I have come up with so far.

Let me know what you think... not tried it yet.. :D


Code: Select all

item                           = {}        -- make a table to hold the meshfiles
dropchance                     = 15;       -- drop chance is 15
dropnumber                     = 0;        -- Initialize rest of the variables
itemtospawn                    = 0;
x                              = 0;
y                              = 0;
z                              = 0;
               
 -- ** drop on enemy death, this is to go inside the KO function of your AI script **
   item[0] = "ammo.mesh";                                                       
   item[1] = "firstaid02.mesh";                                                 
   x,y,z = entity.getPosition( obj );                                           
   dropnumber = math.random(1,100)                                       
   
             if dropnumber < dropchance then                                         
                     itemtospawn = math.random(0,1)                                 
                     entity.spawn(item[itemtospawn],x,y,z,0.01,0.01,0.01);   
             end                                                                         
               
   entity.delete( obj );     


The last one entity.delete is to delete the dead enemy from the scene so you can see the drop better.
User avatar
epsilonion
Skyline Lead Moderator
Skyline Lead Moderator
 
Posts: 874
Joined: 26 Feb 2015, 11:51
Location: Hull, East Yorkshire, England
Skill: Business Manager
Skill: Great creative

Re: Random drop chance

Postby Shando » 29 Jul 2015, 02:39

Hey buddy,

looks ok to me ;)

One thing I would mention is that the Lua "random" function isn't great :(

To improve it slightly, you can insert the following in the onInit function (os.time only gets updated once a second, so you can't reseed every frame!):

Code: Select all
math.randomseed ( os.time ( ) );


and then 'pop' a few random numbers before using your first one.

HTH

Shando
Ryzen 7 4800H 16GB GTX1650 Win 11 64
Love, Hope, Strength http://www.lovehopestrength.co.uk
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Random drop chance

Postby SolarPortal » 30 Jul 2015, 09:28

nice to see you scripting now liam :) It shows you must be moving on to the next level with skyline now :)

As shando mentioned, the random can be improved very easily :)
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer


Return to Lua Scripting

Who is online

Users browsing this forum: No registered users and 2 guests

cron