• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

How can I create random objects?

Status
Not open for further replies.
Level 10
Joined
Nov 5, 2008
Messages
536
I want to put a "random generator" in my map. When the game starts, there is 33% chance that the generator spawns a Zergling, 33% to spawn a marine and 33% to spawn something else.. Then the generator vanishes..



How can this be done?
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
I want to put a "random generator" in my map. When the game starts, there is 33% chance that the generator spawns a Zergling, 33% to spawn a marine and 33% to spawn something else..
The scripted approach is to use conditional flow control statements to branch to specific procedures depending on the result of a random integer (returned from a call to a random generator native). If Galaxy supports the case statement then I advise using one.

There is also a data editor approach where you use a "set" effect to randomly select an effect chain. These effect chains spawn the appropiate units.

Which is better depends on scaleability. The scripted approach is probably more scaleable and flexible (large numbers of dynamic choices). The data editor approach is more hard coded but more efficent if you only have a couple predetermined random selections.

Then the generator vanishes..
I have no clue what this "generator" object is. Please elaborate.
 
That's a pretty lame generator as far as procedural generators you could make go. I created a script for you though which will do what you asked (a bit elaborate, but I want you to understand it):

JASS:
void gf_RandomUnitSpawnExample () {
    // ------------------------- Interface -------------------------
    const int       lv_totalUnitTypes   = 3;
    const string    lv_unitType1        = "Marine";
    const string    lv_unitType2        = "Zergling";
    const string    lv_unitType3        = "YourOtherUnitIDGoesHere";
    // Go ahead and add more const strings for other unit types
    // when modifying the code. Make sure to increase the total too.
    // -------------------------------------------------------------
    fixed           lv_random           = RandomFixed(0, 100);
    fixed           lv_chance           = 100/lv_totalUnitTypes;
    int             lv_p                = 1;
    point           lv_pt               = Point(32, 32);
    unit            lv_u;
    
    // When the game starts, there is 33% chance that the generator spawns a Zergling,
    // 33% to spawn a marine, and 33% to spawn something else...
    if (lv_random <= lv_chance) {
        UnitCreate(1, lv_unitType1, c_unitCreateIgnorePlacement, lv_p, lv_pt, RandomInt(0, 360));
    } else if (lv_random <= lv_chance*2) {
        UnitCreate(1, lv_unitType2, c_unitCreateIgnorePlacement, lv_p, lv_pt, RandomInt(0, 360));
    } else {
        UnitCreate(1, lv_unitType3, c_unitCreateIgnorePlacement, lv_p, lv_pt, RandomInt(0, 360));
    }
    lv_u = UnitLastCreated();
        
}


(ctrl + alt + t to make a new custom script in your trigger editor btw)

Make sure to call your function in the initialization function part.
 
Last edited:
Level 1
Joined
Jan 31, 2012
Messages
5
If you're not familiar with Jass, like me, this may be helpful. I believe this should accomplish your goals. I didn't give the variable a specific name or create a location for the units in the following trigger to be placed at, but those things are simple enough to do.

  • random generator
    • Events
      • Game - Map initialization
    • Local Variables
      • Untitled Variable 001 = 0 <Integer>
    • Conditions
    • Actions
      • Variable - Set Untitled Variable 001 = (Random integer between 1 and 3)
      • General - If (Conditions) then do (Actions) else do (Actions)
        • If
          • Untitled Variable 001 == 1
        • Then
          • Unit - Create 1 Zealot for player 1 at Point facing 270.0 degrees (No Options)
        • Else
          • General - If (Conditions) then do (Actions) else do (Actions)
            • If
              • Untitled Variable 001 == 2
            • Then
              • Unit - Create 1 Zergling for player 1 at Point facing 270.0 degrees (No Options)
            • Else
              • General - If (Conditions) then do (Actions) else do (Actions)
                • If
                  • Untitled Variable 001 == 3
                • Then
                  • Unit - Create 1 Marine for player 1 at Point facing 270.0 degrees (No Options)
                • Else
                  • General - Return False
I'm not terribly familiar with the SC2 editor. There may be a more streamlined way of implementing multiple permutations of "if then else".
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
I'm not terribly familiar with the SC2 editor. There may be a more streamlined way of implementing multiple permutations of "if then else".
Case statements should be more efficient but that depends if the galaxy scripting language supports them.

Additionally you could map the unit types using an array and use the random integer as an array index. This is the most efficient as it eliminates procedural coupling but restricts the complexity of the spawns. Currently the way you have it coded it creates a separate state for each spawn where the mapped types way would have a simple state machine which gets fed in the array.
 
Case statements should be more efficient but that depends if the galaxy scripting language supports them.

Additionally you could map the unit types using an array and use the random integer as an array index. This is the most efficient as it eliminates procedural coupling but restricts the complexity of the spawns. Currently the way you have it coded it creates a separate state for each spawn where the mapped types way would have a simple state machine which gets fed in the array.

I assumed he would want to do something a bit more complex so I scripted my code in a way that would allow him to add in other actions for each unit type.
 
Status
Not open for further replies.
Top