Embed variable values into npc creation?

Antares

New member
Messages
26
Points
0
Location
Hungary
Github
MrAntares
Emulator
Hercules
Hi!

Is it possible to embed variable values into npc creation scripts?

For example this is -the- script:

prtg_cas02,88,215,0 warp prtg-2-02_prtg-2-22 1,1,prtg_cas02,206,41 

and I want it to use values from the variable .@map$. Is it posible to do something like this:?

.@map$,88,215,0 warp prtg-2-02_prtg-2-22 1,1,prtg_cas02,206,41 


or to go further:

.@map_with_pos$,0 warp prtg-2-02_prtg-2-22 1,1,prtg_cas02,206,41 

I would like to avoid switches, so is there a way to make this work?
default_smile.png


 
The short answer to your question is "no".

The somewhat longer answer would be that you wouldn't be able to initialize a temporary NPC variable (.@var/.@var$) if it hasn't been defined yet (where would the NPC get a temporary variable if it has not even been loaded?). I'm not sure what exactly you're trying to do, but you could probably do something really similar with enablenpc/disablenpc.

 
Last edited by a moderator:
Basicall I want a warp to send ppl into random locations. The location is determined in let's say every day. The possible locations are stored in an array. The array might have hundreds of items, so I would really like to aviod using a switch for every item. I don't want to hardcode the locations anywhere but only in the array.

 
I might have found my answer
default_smile.png


If anyone else is curious about the same matter, this makes something similar. I only need to alter it a bit to make it work the way I want
default_smile.png


aldeg_cas04,132,231,0 script aldeg-4-15_aldeg-4- 45,1,1,{
 
OnTouch:
switch (rand(1,5)) {
case 1: warp "aldeg_cas04",152,210; end;
case 2: warp "aldeg_cas04",111,210; end;
case 3: warp "aldeg_cas04",129,212; end;
case 4: warp "aldeg_cas04",129,212; end;
case 5: warp "aldeg_cas04",14,196; end;
}
end;
}

PS-

My example was wrong I used the npc's position and not the destination, sorry baut that
default_smile.png


 
Last edited by a moderator:
As of today that's not possible without changing almost entirely the scripting engine. You can't use any variables when defining any of the following:

  • A mapflag
  • A monster spawn
  • A warp point
  • Any NPC object

When you are defining any of these you're using what in the documentation (/doc/script_commands.txt) is called a top-level command, and as it's said there (pay special attention to the bolded text):

Upon loading all the files, the server will execute all the top-level commands in them. No variables exist yet at this point, no commands can be called other than those given in this section. These commands set up the basic server script structure - create NPC objects, spawn monster objects, set map flags, etc. No code is actually executed at this point except them. The top-level commands the scripting are pretty confusing, since they aren't structured like you would expect commands, command name first, but rather, normally start with a map name.
So, sadly, I think that won't work.

Edit: That other way you just proposed will work for sure.

 
Last edited by a moderator:
Thanks for your answer! I was thinking a bit reversed when I posted my original question, but knowing this is pretty useful
default_smile.png


 
This mockup script can help you do what you want. Just remember it's not an actual script but a mockup:

<map>,<x>,<y> script randomwarp 45,1,1{OnTouch: if (!.init) { donpcevent strnpcinfo(3)+"OnInit"; sleep2 10; // This way the OnInit event has time to load completely } /* HERE COMES YOUR DECISION ALGORITHM set .@pos, M; // .@pos is the decided position on the array, let's say you decided position M */ warp .maps$[.@pos],.x[.@pos],.y[.@pos]; end;OnInit: // Fill your possible destinations on this arrays. Warning: arrays have up to 128 positions, don't use more than 128 places // Arrays have positions ranged from 0 to 127. This means 0 is the first position of an array. // Change "mapN" to actual map names, xN to actual X positions on mapN, yN to actual Y positions on mapN setarray .maps$[0],"map1","map2","map3"; setarray .x[0],x1,x2,x3; setarray .y[0],y1,y2,y3; end;}
Hope this helps you making your script
default_wink.png


 
@Jabote

you dont need this

if (!.init) { donpcevent strnpcinfo(3)+"OnInit"; sleep2 10; // This way the OnInit event has time to load completely }

all npc will now execute OnInit label when the npc is loaded into the server.

beside...you are missing this if you want to run above statement otherwise the statment above will run everytime player touch the npc

Code:
set .init,1;
 
Last edited by a moderator:
Whops, that habit was a bit old, didn't know that. For the .init var, I forgot to add it on the quick mockup, my bad.

 
Back
Top