[JASS] Strings and in-game values

Status
Not open for further replies.
Level 3
Joined
May 19, 2010
Messages
34
Hey guys, I'm just starting to learn JASS and whilst there are lots of good tutorials out there for beginners there are still many things I don't understand. THe main principle is how can you use JASS to edit specific values that aren't easily accessed or accessable at all via GUI triggers.

More specifically, I was trying to make a simple in-game addition calculator as practice. That is, when a player typed a message containing the addition of two integers (so a+b, where a,b are integers) a message would be displayed showing the result. However the only in-game 'Action' in terms of chat messages that players type were things like "if they type String 1 + String2" and it had to be an exact match or a substring. So how would I make it so that they can type ANY value there and then I can set those values to a local variable or the like. So if they typed 4+8 is it possible to set 4=a, 8=b then add them together and display the result through JASS? I can manage to add them, it's just converting them to variables to start off with that I can't work out. And also, is that a good way to go about what I'm trying to do?

And secondly, how can I access other in-game values? For example, in a function which involved say dealing damage based on a unit's intelligence (or whatever, there are plenty of different examples) how can I set a local variable to equal a hero's intelligence. Like:

function Trig_IntAttack_Actions takes nothing returns nothing
local integer a = ???

Firstly, CAN I do it that way, and if so how? I mean I'm generally wondering how you type out those kinds of values in JASS, like unit's movement speed, movement height, stats, life/mana etc..

Sorry if my questions seem stupid or completely wrong, I'm brand new to JASS and any kind of programming :confused: and starting off is the hardest of all. I'm just trying to get a hold of some basic concepts so I can build on them.

Thanks for anyone who helps, it's greatly appreciated :grin:!!!
 
Dinipants said:
And secondly, how can I access other in-game values? For example, in a function which involved say dealing damage based on a unit's intelligence (or whatever, there are plenty of different examples) how can I set a local variable to equal a hero's intelligence.
JASS:
local unit u = GetTriggerUnit()
local integer i = GetHeroInt(u, true)

Others:
JASS:
local unit u = GetTriggerUnit()
local real r = GetUnitMoveSpeed (u)

JASS:
local unit u = GetTriggerUnit()
local real r = GetUnitState (u, UNIT_STATE_LIFE)

For the calculator thing, you will need a carefully structured system. It will work with substrings and conversions S2I.

Good luck by the way with learning the ropes.
 
Okay, so if you were to make a calculator, you first need to layout what you need to do.

- First, you want to allow for two integers to be input.
- It will be a+b.
- "a" and "b" will be numbers.

So now, you can start the coding. So first, you want this trigger to be executed when someone types a chat message. So we will start off the trigger:
JASS:
function InitTrig_Calculator takes nothing returns nothing
    //This is the function that is called on map initialization, when the game loads. 
    //The prefix is InitTrig_ and the next part is "YourTrigger'sName"
    //In this case, the trigger should be named "Calculator".
    local trigger t = CreateTrigger() //This will create the trigger to register the event.
endfunction

Okay, now we will look for the proper event:
JASS:
native TriggerRegisterPlayerChatEvent takes trigger whichTrigger, player whichPlayer, string chatMessageToDetect, boolean exactMatchOnly returns event

Well, that is the event to use. So, we input a trigger, which player to register the event from, the string message to detect, and the boolean to see whether it has to be exact or just a part of it. Since we want it for all players, we will need to make a loop through for each player.
JASS:
function InitTrig_Calculator takes nothing returns nothing
    local trigger t = CreateTrigger()
    local integer i = 0 //Loop integer.
    loop //Begin the loop.
        exitwhen i==12 //We will end when "i" is equal to 12.
        call TriggerRegisterPlayerChatEvent(t,Player(i),"+",false) //register the event
        //When someone types "+" in their chat message, this trigger will be fired.
        //Player(i). It will loop through, increasing "i" by one. So it will register for players 0-11.
        set i = i+1 //The integer "i" is increased by 1.
    endloop //End the loop.
endfunction

Okay, now we need to tell where the trigger will lead to once the event is registered. Thus, we use TriggerAddAction[icode=jass]. [code=jass]function Calculations takes nothing returns nothing endfunction //Functions are compiled from top to bottom. Thus, this must be ABOVE the // initialization for it to be considered a valid function input. function InitTrig_Calculator takes nothing returns nothing local trigger t = CreateTrigger() local integer i = 0 loop exitwhen i==12 call TriggerRegisterPlayerChatEvent(t,Player(i),"+",false) set i = i+1 endloop call TriggerAddAction(t,function Calculations) //When the event is registered, it will execute the function "calculations" endfunction[/code] Now we need to consider some things. - There will be a number before the "+" and after the plus. - We can break the string into two parts that way. - After it is broken, we can formally add them. So now let's do that. [code=jass]function Calculations takes nothing returns nothing local string entered = GetEventPlayerChatString() //the entered chat string local string sub = "" //this will be the looping string local integer length = StringLength(entered) //length of the entered chat string local integer character = 0 //this will be for looping through the string local real addition1 = 0 //This will be the first number to add. local real addition2 = 0 //This will be the second number. loop exitwhen sub=="+" or character>length //We will loop until we find the "+" sign. //Else, exit when we've looped through the entire string. set sub = SubString(entered,character,character+1) //This will loop //through the string. It will loop through each character and retrieve it. set character = character+1 endloop set addition1 = S2R(SubString(entered,0,character-1)) //We'll retrieve the number from the start to behind the "+". set addition2 = S2R(SubString(entered,character,length)) //We'll retrieve the number from after the "+" to the end. call DisplayTextToPlayer(GetTriggerPlayer(),0,0,"Result: "+R2S(addition1+addition2)) //Display the result. endfunction function InitTrig_Calculator takes nothing returns nothing local trigger t = CreateTrigger() local integer i = 0 loop exitwhen i==12 call TriggerRegisterPlayerChatEvent(t,Player(i),"+",false) set i = i+1 endloop call TriggerAddAction(t,function Calculations) endfunction[/code] This is difficult to understand. [icode=jass]SubString cuts a string into a part, from one point to another. What we are doing, is chopping up each character. So say someone input "534+325"

We'll loop through:

"5" is not "+", keep looping
"3" is not "+", keep looping
"4" is not "+", keep looping
"+" IS "+", end the loop.

Then it will cut before the "+" and after it.
"534"
"325"

Then it will add them.
"534+325" = "859"

That is what it will do. Some notes though:
- The a+b must not be separated by spaces. Thus, it can't be "a + b", it must be "a+b". Else you would need to search for spaces. It would be a bit longer of a process.
- It will be precise to the third decimal. You can always change this with R2SW, but we won't go into that.

Now you should test it out, see if it works. It worked for me, hopefully the same for you. =)

--------------

Let's recap:
- First, find out what you are trying to do.
- Create a basic layout.
- Find the proper functions to use.
- Perform it.

How to debug? Just utilize this:
JASS:
function BJDebugMsg takes string msg returns nothing

This will just show a message, so add them in random parts in the trigger. That way you can show parts of the trigger, using things like R2S and I2S to turn numbers into strings, and to see the results of things in game.

That way, you can keep your trigger in check. :)

Good luck.
 
Level 3
Joined
May 19, 2010
Messages
34
Ok another question about the unit values, how do I find them out? I mean if I want to know a unit's X location, it's Y location, it's animation speed it's buffs etc. like all the different values of a unit how can I find them out? Like is there a way I can find them in-game? Rather than just asking people every time I need to know a specific one? Or is there a list somewhere which tells you how to type out each different value in JASS? Say for example, Pharoah 's post had:

local integer i = GetHeroInt

how did you know that GetHeroInt was the line used to retrieve a hero's intelligence? Where can you find that out? It'll save me asking every time I don't know the specific line for a specific value.

For example, I was thinking about making a trigger which causes a unit to slow down over 3 seconds until finally it freezes (like a time-lock sort of thing) and then obviously work it in reverse. So how would I found out how to change a unit's animation speed and the code for that specific value myself? Do the codes for all the values follow a certain pattern?

Thanks again for any help :).
 
Certain things (like animation speed, damage, vertex colors, attack speed, etc.) are not easily obtainable without some sort of system.

However, things like X/Y, strength, agility, intelligence, current order, etc. are findable.

You can either:
  • Find the appropriate function in GUI, convert to custom text, and then try to inline the BJ.
  • Download Jass NewGen Pack v5d, which will highlight your JASS and give you a neat function list. (Or well, the TESH aspect will) It will also give you some nice features that can simplify coding, once you've learned the basics. This tool also has some other cool features, eg: more tiles, nice object editor sorting, unlimited doodads, etc.
  • Download a tool like JassCraft, which will do the same as above, for the JASS part. However, it doesn't have its own world editor, it is a standalone program, but it loads quickly and is a nice coding interface. It also has a nice interface.

I prefer to get both Jass NewGen Pack and JassCraft. But if you are stuck trying to find a function, don't hesitate to use the GUI method as well. But afterward, look up the BJ in JASSCraft/NewGen and see how to inline it. (if it is too much work to inline though, just leave it as is because there is no large efficiency difference. It is just a good habit to inline simple BJ's that just have swapped parameters for the sake of GUI)
 
Status
Not open for further replies.
Top