[ vJASS ] About Spawn Unit

General Jass Discussion

13 years
How do I make:

Quote
-Spawn ( The Unit ID ) ( Value )

Spawn unit with entered unit ID, you can set how much the spawned unit
13 years
Do you want it as a chat command??
13 years
Of course - .. -
Oh I want it vJASS :3 :3 Because it's neat and cute :3 :3
13 years
Ok, working in a idea, but the part of using unitid to call the unit requires more time.  Could it be:

Quote-spawn 5 Knights
So we can use the formal name?? I thinkit could be easier
13 years
No, I want id Unit ID
I had listed the Unit on the quest :D :D
10 years
Well, you're going to need to figure out where to put them. Do you have a designated location for each player or do you want to base it off of the current location of a unit, or what? You didn't provide much information. Also, are you trying to make it so units will revive even though they aren't heroes, like in a more traditional RPG, or are you simply trying to create units once?
10 years
sorry bump, it's been a long time, i've been quite busy, anyway, please help me with these :3 :3
-spawn (Unit ID, not formal name!) (amounts, if not entered, just spawn it one)
10 years
Have you tried the S2I function?
10 years
edited 10 years
You have still to convert the string in a valid integer.
It can be done of course, but it's not straight forward, for example 'hpea' (human peasant) is not a decimal base.
Hopefully there is already a resource for that :
http://www.hiveworkshop.com/forums/jass-resources-412/snippet-ascii-190746/

Ok, here we go :

Code (jass) Select
library SpawnUnitByChat requires Ascii initializer init

   globals
      private constant string KEY = "-spawn"
      private constant player PLAYER = Player(0) // first player (red)
      private constant real X = 0
      private constant real Y = 0
   endglobals

   private function Actions takes nothing returns nothing
      local string chat = GetEventPlayerChatString()
      local integer i = -1
      local string s = ""
      local string unitid = ""
      local string count = ""
     
      chat = SubString(chat,StringLength(KEY)+1,StringLength(chat))
      loop
      set i = i+1
      set s = SubString(chat,i,i+1)
      exitwhen s == " " or s == null
         set unitid = unitid+s
      endloop

      loop
      set i = i+1
      set s = SubString(chat,i,i+1)
      exitwhen s == " " or s == null
         set count= count+s
      endloop
     
      set i = S2I(count)
      loop
      set i = i-1
         call CreateUnit(PLAYER,S2A(unitid),X,Y,0)
      exitwhen i <= 0
      endloop
     
   endfunction

   private function init takes nothing returns nothing
       local trigger trig = CreateTrigger()

       call TriggerRegisterPlayerChatEvent(trig,PLAYER,KEY,false)
       call TriggerAddAction(trig,function Actions)
   endfunction

endlibrary


Note that i've written the code directly here, not tested, it could have errors, also you need to follow exactly this pattern :
-spawn unitid X

Where X is the numbers of units (optional).
We could improve the script and allow or not more spaces between the arguments, and add safety, but oh well i have not found an interesting string parsing library, so meh ...
10 years
Now since i've the JNGP again, i've tried and fixed the two syntax errors.
The code seems to work as it should, after a short test with these commands :

-spawn hfoo 3
-spawn hpea

Code (jass) Select
    library SpawnUnitByChat initializer init requires Ascii
     
       globals
          private constant string KEY = "-spawn"
          private constant player PLAYER = Player(0) // first player (red)
          private constant real X = 0
          private constant real Y = 0
       endglobals
     
       private function Actions takes nothing returns nothing
          local string chat = GetEventPlayerChatString()
          local integer i = -1
          local string s = ""
          local string unitid = ""
          local string count = ""
         
          set chat = SubString(chat,StringLength(KEY)+1,StringLength(chat))
          loop
          set i = i+1
          set s = SubString(chat,i,i+1)
          exitwhen s == " " or s == null
             set unitid = unitid+s
          endloop
     
          loop
          set i = i+1
          set s = SubString(chat,i,i+1)
          exitwhen s == " " or s == null
             set count= count+s
          endloop
         
          set i = S2I(count)
          loop
          set i = i-1
             call CreateUnit(PLAYER,S2A(unitid),X,Y,0)
          exitwhen i <= 0
          endloop
         
       endfunction
     
       private function init takes nothing returns nothing
           local trigger trig = CreateTrigger()
     
           call TriggerRegisterPlayerChatEvent(trig,PLAYER,KEY,false)
           call TriggerAddAction(trig,function Actions)
       endfunction
     
    endlibrary


Enjoy :)
10 years
edited 10 years
Quote from: Troll-Brain on March 11, 2016, 06:46:24 PM
Now since i've the JNGP again, i've tried and fixed the two syntax errors.
The code seems to work as it should, after a short test with these commands :

-spawn hfoo 3
-spawn hpea

Code (jass) Select
    library SpawnUnitByChat initializer init requires Ascii
     
       globals
          private constant string KEY = "-spawn"
          private constant player PLAYER = Player(0) // first player (red)
          private constant real X = 0
          private constant real Y = 0
       endglobals
     
       private function Actions takes nothing returns nothing
          local string chat = GetEventPlayerChatString()
          local integer i = -1
          local string s = ""
          local string unitid = ""
          local string count = ""
         
          set chat = SubString(chat,StringLength(KEY)+1,StringLength(chat))
          loop
          set i = i+1
          set s = SubString(chat,i,i+1)
          exitwhen s == " " or s == null
             set unitid = unitid+s
          endloop
     
          loop
          set i = i+1
          set s = SubString(chat,i,i+1)
          exitwhen s == " " or s == null
             set count= count+s
          endloop
         
          set i = S2I(count)
          loop
          set i = i-1
             call CreateUnit(PLAYER,S2A(unitid),X,Y,0)
          exitwhen i <= 0
          endloop
         
       endfunction
     
       private function init takes nothing returns nothing
           local trigger trig = CreateTrigger()
     
           call TriggerRegisterPlayerChatEvent(trig,PLAYER,KEY,false)
           call TriggerAddAction(trig,function Actions)
       endfunction
     
    endlibrary


Enjoy :)
Woah!! Thank you so much <3 <3
Trying.....

EDIT:
WOW IT WORKS LIKE A CHARM!!
THANK YOU SO MUCH MUCH WE DO!!
Thread Closed :3 :3
10 years
um.. One last question, sorry damn.
How to make spawned units patrol to random points?