• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[JASS] Help with TriggerRegisterPlayerChatEvent

Status
Not open for further replies.
Level 8
Joined
Oct 31, 2010
Messages
238
Hi all. I am new to JASS. (Started learning yesterday) . And I am a new member of Hiveworkshop.

I have a problem with that event.
JASS:
TriggerRegisterPlayerChatEvent( gg_trg_TRIGGER, Player(0), udg_STRING, true)
I wanted to use a global string variable (udg_STRING) but failed.
I typed anything and it just executed the actions.

Here's my script:

Global variables:
udg_RandomString[1] = .......
udg_RandomString[2] = .......
udg_RandomInteger = Random Integer between 1 and 2

JASS:
function Trig_TRIGGER_Actions takes nothing returns nothing
        ------ALL MY CODE HERE
endfunction

//===========================================================================
function InitTrig_TRIGGER takes nothing returns nothing
    set gg_trg_TRIGGER = CreateTrigger(  )
    set udg_STRING = udg_RandomString[udg_RandomInteger]

    call TriggerRegisterPlayerChatEvent( gg_trg_TRIGGER, Player(0), udg_STRING, true )
    call TriggerAddAction( gg_trg_TRIGGER, function Trig_TRIGGER_Actions )
endfunction

Help would be very much appreciated:p
 
Level 8
Joined
Oct 31, 2010
Messages
238
Are you getting any compile errors? Or does the map save, but does it just not do what you expect it to do?

In that perspective, what do you expect this trigger to do?

It compiled correctly. It is that I doesn't do what I wanted it to do.
It is supposed to generate a random string (udg_STRING) and the player is suppose to type that to continue on.

However, i typed anything and the trigger ignores the event and continues running the actions.
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
The problem happens when the string parameter for this function is "" or null. You use
JASS:
TriggerRegisterPlayerChatEvent
in an init function there but the array you are extracting your string from seems to be set at another point, so probably this init function here runs first and the array is not filled yet.

String arrays' members are predefined to null.
 
Level 8
Joined
Oct 31, 2010
Messages
238
The problem happens when the string parameter for this function is "" or null. You use
JASS:
TriggerRegisterPlayerChatEvent
in an init function there but the array you are extracting your string from seems to be set at another point, so probably this init function here runs first and the array is not filled yet.

String arrays' members are predefined to null.

So is there anything I can do to solve the problem?
 
Level 5
Joined
Oct 14, 2010
Messages
100
JASS:
function Trig_TRIGGER_Actions takes nothing returns nothing
        ------ALL MY CODE HERE
endfunction

//===========================================================================
function InitTrig_TRIGGER takes nothing returns nothing
    set gg_trg_TRIGGER = CreateTrigger()

    set udg_RandomString[0] = "Code 1" // where Code 1 is the string you must enter in game
    set udg_RandomString[1] = "Code 2"
    // for all strings

    set udg_RandomInteger = GetRandomInt(0, 9) // random integer between 0 and 9, assuming there are 10 RandomStrings.
    
    call TriggerRegisterPlayerChatEvent( gg_trg_TRIGGER, Player(0), udg_RandomString[udg_RandomInteger], true )
    call TriggerAddAction( gg_trg_TRIGGER, function Trig_TRIGGER_Actions )
endfunction

Basically, you initialize the random strings first, then you register the chat event.

String arrays' members are predefined to null.
Unless string arrays are an exception to the rule, only the indices you've assigned in the variable editor are initialized to null. So if you have an array of size 10, only the 10 first indices are null. Using other indices before assigning a value will make your code halt.
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
Unless string arrays are an exception to the rule, only the indices you've assigned in the variable editor are initialized to null. So if you have an array of size 10, only the 10 first indices are null. Using other indices before assigning a value will make your code halt.

No, I actually tested this. It's right that the preset value (may be null) in the variable window is assigned to it from 0 to given size of the variable there, but I did not create it via the variable window. Using undefined non-array variables will halt the code, arrays won't in War3 since they are preset upon declaration. Anyway, it's not a clean programming style, define your variables before using them.
 
Level 8
Joined
Oct 31, 2010
Messages
238
Okay I feel like giving up. Anyone would be kind enough to help me make a map that shows the triggers?

It goes like that:


TRIGGER ONE

A specified unit (I defined it as udg_UNIT) enters this region.

A random string is generated from a array of strings.
udg_RANDOMSTRING[0] = lol
udg_RANDOMSTRING[1] = lolol

A random integer is generated to get the index of the string array.
udg_RANDOMINTEGER = GetRandomInt(0, 1)

Then another string will be:
udg_PASSWORD = udg_RANDOMSTRING[udg_RANDOMINTEGER]

Then a quest will be created, with the description as udg_PASSWORD
After that, TRIGGER TWO will be initiated


TRIGGER TWO

The player is supposed to type udg_PASSWORD

After that, he can continue on with the game.
(e.g. advance to the next level)
The quest is removed.



That's the thing I want to do but I can't.. Someone help me make a example map with this triggers please.. I need a guide. I am very new to JASS (started yesterday)..

I will +rep to whoever that helps me later on! :)

Thanks in advance.
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
Okay I feel like giving up. Anyone would be kind enough to help me make a map that shows the triggers?

We are helping you by providing hints. You shall consider them and spend own thoughts into it to solve your problem on your own.

When you use
JASS:
udg_RandomString[udg_RandomInteger]
in
JASS:
TriggerRegisterPlayerChatEvent
, the string your variable currently contains (when the event is created through the code) is the one that the trigger reacts to. It's not like you could state a variable and when the variable changes the event automatically changes accordingly. So you have to create another event when you newly set the variable.

Now, you cannot destroy the old event without destroying the trigger, which again is kind of bugged. So rather make all chat messages firing the trigger and filter the input by comparing
JASS:
constant native GetEventPlayerChatString takes nothing returns string
with your variable's content. Then again, if you have to filter it by conditions anyway, one event of this type will be enough.
 
Level 8
Joined
Oct 31, 2010
Messages
238
Now, you cannot destroy the old event without destroying the trigger, which again is kind of bugged. So rather make all chat messages firing the trigger and filter the input by comparing
JASS:
constant native GetEventPlayerChatString takes nothing returns string
with your variable's content. Then again, if you have to filter it by conditions anyway, one event of this type will be enough.

How do I compare

JASS:
call TriggerRegisterPlayerChatEvent( gg_trg_TRIGGER, Player(0), "", false )
with
JASS:
native TriggerRegisterPlayerChatEvent takes trigger gg_trg_TRIGGER, player 0, string udg_PASSWORD, boolean exactMatchOnly returns nothing
? Or is this not what you're trying to say? Sorry!
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
JASS:
call TriggerRegisterPlayerChatEvent( gg_trg_TRIGGER, Player(0), "", false )

makes the trigger fire on any chat message sent.

JASS:
GetEventPlayerChatString

returns the entered chat message in the fired trigger's conditions/actions.

So you make a selection

JASS:
if (GetEventPlayerChatString() == udg_PASSWORD) then
    //Do your actions
endif

The code will only enter the then-path if the condition is fulfilled, if the entered chat message that made the trigger fire is the same as that one in your udg_PASSWORD variable.

You may also cancel the function's run by using the "return" directive. If the function does return nothing (see function's declaration headline)

JASS:
return

is enough. Else you have to return a value of the specified type. If you put the selection in the trigger's condition function, simply

JASS:
return false

This way, if the condition function returns false, the trigger won't start its action function.
 
Status
Not open for further replies.
Top