Jump to content
  • 0

Variable into variable name


DanTheMan

Question


  • Group:  Members
  • Topic Count:  5
  • Topics Per Day:  0.00
  • Content Count:  13
  • Reputation:   0
  • Joined:  01/28/20
  • Last Seen:  

Hi everyone!

 

I'm trying to do something at the moment and I'm wondering if it it's possible to pass the string of a variable into a new variable.

Here's an example:

set @monstername$,getmonsterinfo(killedrid,MOB_NAME);

//Use @monstername$ as variable name
#@monstername$ +=1;

//Use @monstername$ in combination with text as variable name
#@monstername$"kills" += 1;

 

Is any of those two possible?

 

Thanks in advance,

Dan

Edited by DanTheMan1988
Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

  • Group:  Forum Moderator
  • Topic Count:  93
  • Topics Per Day:  0.02
  • Content Count:  10013
  • Reputation:   2345
  • Joined:  10/28/11
  • Last Seen:  

On 2/18/2020 at 8:34 PM, DanTheMan said:

What I want to create is a list of the last 5 killed mobs. First I thought of an array but I ran into a wall trying it. 

try this.

-	script	Sample	-1,{
	OnInit:
		.last_kill_amount = 5;
		bindatcmd "lastkill",strnpcinfo(3) + "::OnAtcommand";
		end;
		
	OnAtcommand:
		for (.@i = 0; .@i < .last_kill_amount; .@i++)
			if (last_killed_mobid[.@i]) 
				mes (.@i+1)+". "+getmonsterinfo(last_killed_mobid[.@i]. MOB_NAME);
		end;
		
	OnNPCKillEvent:
		copyarray last_killed_mobid[1], last_killed_mobid[0], (.last_kill_amount - 1);
		last_killed_mobid[0] = killedrid;
		end;
}

 

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  25
  • Topics Per Day:  0.01
  • Content Count:  283
  • Reputation:   76
  • Joined:  06/13/13
  • Last Seen:  

first you need to read script doc to see how the variable type is composed... there is no #@ as valid prefix for a variable...

then, to use integer in string type variable you need to "stringify" it idk if that was the correct term..

examples

.@int = 1;
.@string_int$ = .@int+"";

mes .@int; // warning ? error?? forgot what will pop up by this
mes .@string_int$; // ok!
mes .@int+""; // ok!

OnNPCKilledEvent:
	.@mobid = killedrid;
	
	// using combination?
	// the original variable should be like ".mob1002_killcount" for Poring.
	.@data_int = getd(".mob"+.@mobid+"_killcount");
	
	// setting the value into it
	set getd(".mob"+.@mobid+"_killcount"), .@data_int +1;
	end;

 

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  5
  • Topics Per Day:  0.00
  • Content Count:  13
  • Reputation:   0
  • Joined:  01/28/20
  • Last Seen:  

Quote

first you need to read script doc to see how the variable type is composed... there is no #@ as valid prefix for a variable...

Yeah, I knew that. Just dumped that like this in here. Thanks anyway.

 

I think I got that part and I'm gonna try it out now. Thanks a bunch!

 

Got another question and instead of creating a new topic, I'll write here. 

What I want to create is a list of the last 5 killed mobs. First I thought of an array but I ran into a wall trying it. 

I use killedrid as ID and the idea is that the most recent kill is added to the list and the last one (0) gets deleted. 

A thought I had was doing a for loop which moves 1 to 0, 2 to 1, 3 to 2 and 4 to 3. 5 is then added as most recent. 

Is there an easier way?

 

EDIT:

		.@j = 0;
		.@k = 1;
		for (.@i = 1; .@i < getarraysize ( kList$ ); .@i++){
			kList$[.@j] = kList$[.@k];
			.@j++;
			.@k++;
		}

		kList$[4] = getmonsterinfo(killedrid,MOB_NAME);

That does it for now. But I guess there's a more elegant way?

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  25
  • Topics Per Day:  0.01
  • Content Count:  283
  • Reputation:   76
  • Joined:  06/13/13
  • Last Seen:  

for now this type of variable storing is good enough if just only 5, but in my case is not god enough, don't store any in char, accout, map registry if you use it for quest, use custom sql db entry and go query it, well for now this is enough just to get the grasp how it worked...

OnNPCKillEvent:
	.@mobid = killedrid;
	
	// get the size of array list
	.@size = getarraysize(kList$);
	
	// get the index of killed mob, exist or not?
	.@idx = inarray(kList$, getmonsterinfo(.@mobid, MOB_NAME));
	
	// .@idx retrun -1 if the mon is not exist in the list
	// .@idx return index of mob in array list, array index started from 0
	if (.@idx > -1)
	{
		// let's try to increase the mob kill count
		killmobcount[.@idx]++;
	}
	else
	{
		// insert new mobid into the list
		// use .@size variable to set it as last entry in array
		kList$[.@size] = getmonsterinfo(.@mobid, MOB_NAME);
		
		// set the kill count to 1
		killmobcount[.@size] = 1;
	}
	end;

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...