Urban Wars
April 19, 2024, 09:51:06 am
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Welcome, here you will find all the information on Urban Wars
 
  Home Help Search Staff List Login Register  

Randomly spawning a different sprite

Pages: [1] 2
  Print  
Author Topic: Randomly spawning a different sprite  (Read 920 times)
Mexicouger
Lieutenant
*
Posts: 111


View Profile
« on: April 23, 2010, 05:16:31 pm »

Is there any way to make it so instea dof having 1 setmodel, have multiple spawn at random times. Like say for an alien gun, plasma isnt always in 1 shape. So can you make like a gun shoot 3 different sprites at different times?
Report Spam   Logged

Share on Facebook Share on Twitter

DukeInstinct
Administrator
Lieutenant
*****
Posts: 120


The ArchDuke


View Profile WWW
« Reply #1 on: April 23, 2010, 05:53:48 pm »

At the top of the function declare a float.

Assign your float to a random number using the built in function random();

Then add a couple if statements to check what number your float is.



Code:
float Rand;

Rand=random(); // returns a number between 0.0 and 1.0

if (Rand<=0.33)
setmodel(Entity, Sprite Name);
else if (Rand<=0.66)
setmodel(Entity, Sprite2 Name);
else
setmodel(Entity, Sprite3 Name);
Report Spam   Logged



Urban Wars Coder/3d Modeler/Animator

Mexicouger
Lieutenant
*
Posts: 111


View Profile
« Reply #2 on: April 23, 2010, 09:34:47 pm »

THANKS duke. My code was fairly similar to that My float was r. r = random();  then I saw a Footsetp code that used the random function. it was like if (self.walkframe = 1|| self.walkframe = 4)  if (r == 0)  sound (self, CHAN_WEAPON, "boot\boot1.wav", 1, ATTN_NORM);   and it went all the way up to 3. I redid that same code for richochet sound effects and modified it  a bit. So I will try this for my debirs. Thanks Duke. Be expecting me to ask for alot of help. I am gettin alot better Also, why does the number have to be between 0 and 1?
Report Spam   Logged
DukeInstinct
Administrator
Lieutenant
*****
Posts: 120


The ArchDuke


View Profile WWW
« Reply #3 on: April 23, 2010, 09:57:39 pm »

The number you get back from the built in random function will always be between 0 and 1. However, if you wanted a number between 0 and 3 you could just multiply the number you get back from random() with 3.

So you could also do this if you wanted to:

Code:
float Rand;

Rand=random(); // returns a number between 0.0 and 1.0
Rand*=3;

if (Rand<=1)
setmodel(Entity, Sprite Name);
else if (Rand<=2)
setmodel(Entity, Sprite2 Name);
else
setmodel(Entity, Sprite3 Name);
Report Spam   Logged



Urban Wars Coder/3d Modeler/Animator

Mexicouger
Lieutenant
*
Posts: 111


View Profile
« Reply #4 on: April 23, 2010, 10:02:40 pm »

SO THIS is what I get from it. rand is less than or equel to 1. it plays the sound if th enumber is randomly smaller than 1. correct? and else is any othe rnumber that wasn`t executed? I`m more familiar with your first code. I`m gonna put it in play with debris. Thanks. please answer my other question with how to spawn multiple sprite without having to execut ethe function multiple times.
Report Spam   Logged
Mexicouger
Lieutenant
*
Posts: 111


View Profile
« Reply #5 on: April 23, 2010, 10:06:33 pm »

EXCEPTIONAL DOUBle post: Since my other topic got deleted, my new quetsion is: How do I randomly spawn a number of sprites without having to call upon the function 6 times. Like gib, it tosses several gib, but it doesnt say spawnmeatspray(); spawnmeatspray(); etc. ^ also want to  randomly spawn a different number of sprites. Say, between 1 an d10 sprites?
Report Spam   Logged
DukeInstinct
Administrator
Lieutenant
*****
Posts: 120


The ArchDuke


View Profile WWW
« Reply #6 on: April 23, 2010, 10:29:21 pm »

You can use a while loop for that.

Code:
float Rand;
float i;

Rand=random()*10;
i=0;

while (i<Rand)
{
SpawnDebris();
i+=1;
}
Report Spam   Logged



Urban Wars Coder/3d Modeler/Animator

Mexicouger
Lieutenant
*
Posts: 111


View Profile
« Reply #7 on: April 23, 2010, 11:21:05 pm »

I Got an error. It said no free edicts. whats that supposed to mean? I put in the while function and adde dthe random sprite model function and it said that.
Report Spam   Logged
Mexicouger
Lieutenant
*
Posts: 111


View Profile
« Reply #8 on: April 23, 2010, 11:36:12 pm »

Both codes didn`t work. It still only shows 1 sprite and the loop errored out.
Report Spam   Logged
DukeInstinct
Administrator
Lieutenant
*****
Posts: 120


The ArchDuke


View Profile WWW
« Reply #9 on: April 23, 2010, 11:46:42 pm »

You're creating too many entities in Quake if you're getting the error "no free edicts". Make sure you have the i float being incremented every loop. Also make sure you aren't creating all the sprites in the same spot or otherwise you won't be able to tell there is more than one.
Report Spam   Logged



Urban Wars Coder/3d Modeler/Animator

Mexicouger
Lieutenant
*
Posts: 111


View Profile
« Reply #10 on: April 23, 2010, 11:56:56 pm »

So my code was the debris code. a basic  spawn an dthink function with a bouncing func. then in effects, create explosion, I added while local float i, r;  (i < r) { debris(self.origin) i + 1 = 1 }    that was the code. I`m on my cellphone so it`  abit jumbled. I got the code right in meaning that before, the debris randomly shot out of an explosion. It all worked well but I wanted the code to be more efficient rather than hackish. So am I supposed to ad donto that  loop like add while (i<r) { debris(self.origin); i + 1 = 2; } ? or will the func just loop like that. And the sprites do come out right. I just wonder why It won`t properly work.
Report Spam   Logged
Mexicouger
Lieutenant
*
Posts: 111


View Profile
« Reply #11 on: April 24, 2010, 12:02:16 am »

EDALLOC: no free edicts.   Am I supposed to put the while loop in my debris function or in the create explosion function for spawning?
Report Spam   Logged
Mexicouger
Lieutenant
*
Posts: 111


View Profile
« Reply #12 on: April 24, 2010, 12:10:17 am »

1 more thing: the random code worked, but I couldnt get the random number of debris spawning in. I hope you help me with that still
Report Spam   Logged
DukeInstinct
Administrator
Lieutenant
*****
Posts: 120


The ArchDuke


View Profile WWW
« Reply #13 on: April 24, 2010, 12:18:02 am »

If you're putting a while loop in the think function for the debris then that would easily overflow the game with entities. The while loop should only be in the create explosion function which I assume is the initial spawning function of the debris.

Report Spam   Logged



Urban Wars Coder/3d Modeler/Animator

Mexicouger
Lieutenant
*
Posts: 111


View Profile
« Reply #14 on: April 24, 2010, 12:28:19 am »

THE create explosion is an effect for general granades and rockets. the sprite occurs when the rocket explodes and I made it so that it calls upon the debris function 7 times. I would like to use  aloop to spawn things like that instead because it would save memory
Report Spam   Logged
Pages: [1] 2
  Print  
 
Jump to:  


Get your own Chat Box! Go Large!
Bookmark this site! | Upgrade This Forum
SMF For Free - Create your own Forum

Powered by SMF | SMF © 2016, Simple Machines
Privacy Policy