Skip to content
User Avatar
#3675
Auto-translated
Martha

Added 4 hours 8 minutes ago

How do I write the code so that the hero who activated the object is teleported, if that hero belongs to the first player?
So, a little bit of theory. Inside the parentheses, after the name of your function, is the magic word "heroname." This is the function's argument. Like in school, f(x) = x + 5. That is, you can pass different values to it, and the result is expected to be different. You need to pass the argument to the function manually if you call it yourself, but since visiting the object is already a defined event by the developers, the name of the hero and the name of the object will be passed to any function that you specify in the trigger. That is, by adding something like "object" after "heroname," you will also get the name of the object with which the interaction occurred.
So, following the logic I described, the "heroname" variable ALWAYS contains the name of the hero who approached the object. Therefore, all that remains is to write a check to see if this hero is a hero of the first player.
This can be done, if I'm not mistaken, with "GetObjectOwner(heroname) == 1." Well, of course, you should put this check in an "if" statement, and so on, but I hope you understand the logic.
What you wrote there will also work, for the heroes that you entered in the table, but in your check, you have "heroName," and in the argument, you have "heroname." Scripts are case-sensitive, like almost any programming language.
I understand why you made that mistake. The documentation states that "SetObjectPosition" first takes some "heroName," and that's why you put the same thing there, but this is just a "placeholder," it's the same as having "heroname" in the teleportation function. They could have written "arg1," or any other nonsense. It would just be even less clear to you. Therefore, when you pass an argument to any function, it does not have to be named the same as it is written in the function header. You can write any alphanumeric characters there if you need to.

Postscript:
It's better not to copy scripts from forums, but to take a good map, preferably one that you have played, which has scripts, and look at them. For example, the "Cursed" scenario contains many good scripts. It is also worth looking at the works of RedHeavenHero. And read the manual, because without it, it is very difficult to write scripts like yours.)

Added 1 hour 26 minutes ago
Trigger(OBJECT_TOUCH_TRIGGER,"portal","Portal_HaladF")
function Portal_HaladF(heroname)
if(GetObjectOwner(heroname) == 1) then
SetObjectPosition(heroname,56,90,0)
end
end

Here, instead of "portal" in the trigger, you give your own name, and everything will work.
And so that you understand, perhaps, what the trick is with naming arguments:

Trigger(OBJECT_TOUCH_TRIGGER,"portal","Portal_HaladF")
function Portal_Halad(red_flower)
if(GetObjectOwner(red_flower) == 1) then
SetObjectPosition(red_flower,56,90,0)
end
end

Instead of "heroname," "red_flower" appears. Do you agree that flowers have nothing to do with a hero? Of course, no one would name it that way because then you wouldn't be able to understand what the red flower is, but you need to understand the essence - the name itself is not important, the main thing is that it is inserted in the right place. If the trigger gives you the hero's name as the first argument, then it will give it to you regardless of how you name the variable.



Не уходи безропотно во тьму,
Будь яростней пред ночью всех ночей,
Не дай погаснуть свету своему!

Хоть мудрый знает – не осилишь тьму
Во мгле словами не зажжёшь лучей –
Не уходи безропотно во тьму.


                                                                                       

Statistics

Welcome our newest member: wayal