Storing and reading data from XML files.

Discuss, ask for help, share ideas, give suggestions, read tutorials, and tell us about bugs you have found with MicroMacro in here.

Do not post RoM-Bot stuff here. There is a subforum for that.
Forum rules
This is a sub-forum for things specific to MicroMacro.

This is not the place to ask questions about the RoM bot, which uses MicroMacro. There is a difference.
Post Reply
Message
Author
Exempt
Posts: 197
Joined: Wed Jan 20, 2010 9:55 am

Storing and reading data from XML files.

#1 Post by Exempt » Sun Feb 07, 2010 6:11 pm

I'm having ton of issues with this. I'm trying to make the code simple to read so my goal is to do something similar to your rom bot. I want to make a createpath.lua file to make all my paths. Some of the stuff i want to add is that it will be able to call function like yours does with harvesting and stuff.

I guess my real question is in a simple form (i tried to learn from you createpath file but there is to much). How can I setup my createpath.lua to write to a XML file and then would would i read that information. The XML will hold coords (x, y) and i need to to check if i want it to use my attack function at x,y or use my portal function at x,y.

No need to write it out for me or anything but a simple break down will work. The wiki just further confused me this time around.

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Storing and reading data from XML files.

#2 Post by Administrator » Sun Feb 07, 2010 9:35 pm

XML examples can be found on the XML module page:
http://www.solarstrike.net/wiki/index.p ... Xml_Module

To run the code from the XML files, it is read and stored as a string, and executed with the pcall function.

Exempt
Posts: 197
Joined: Wed Jan 20, 2010 9:55 am

Re: Storing and reading data from XML files.

#3 Post by Exempt » Sun Feb 07, 2010 10:12 pm

I'm slowly learning this but I'm still not getting the info I need on reading and writing. I'm going to reread this a few times till i get it and after i have a real error i'll post it. thanks

Edit: This is my goal atm.

I need to make one single list of numbers inside my xml file to fill my table. Inside the file should look something like this..

1234, 4321, 1, 1, 3333, 2222, 2, 2

When my make my code to read this it will pair it into coords and any pair that shows up as 1, 1 will call the attack() function and anything with 2, 2 will call the teleport function. This is the most simple way i could think of to do this.

EDIT: How can i convert a table to a string?

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Storing and reading data from XML files.

#4 Post by Administrator » Mon Feb 08, 2010 7:32 am

Wait, what? It should only return a string in the case of "1,1" being the value.

Second of all, you're going about this all the wrong way. The string "1,1" is going to be more difficult to work with. Instead, you should be using two separate sets of integers, like so:

Code: Select all

<someTag page="1" index="1" />
This way, you can instantly get the information as needed without any extra work. If you want to use a skill through the API, this is the format you'll need the information in; not strings.

Exempt
Posts: 197
Joined: Wed Jan 20, 2010 9:55 am

Re: Storing and reading data from XML files.

#5 Post by Exempt » Mon Feb 08, 2010 11:16 am

All i want to do is store a list of number inside the xml file.

1111, 2222, 1, 1, 2222, 3333, 2222, 3333, 2, 2

All i'd have to do from here is take that entire list of number and put it inside a table and i'm done. I don't need all the tags and stuff just the numbers. I tried to write a list to my xml file and it returned an error to me saying I pass a table where a string should be. I need to turn my table into a string to pass it to my xml file then turn the string back into a table when i read it with the bot script.

Edit: This might help if you see my code and what i plan to do.

Code: Select all

printf("createpath.lua\n");
 
setStartKey(key.VK_DELETE);
setStopKey(key.VK_END); 

tWp = key.VK_NUMPAD7;
tAtt = key.VK_NUMPAD8;
tPort = key.VK_NUMPAD9;
tSave = key.VK_NUMPAD6;

curX = memoryReadInt(myProc, 0x02C85944); --My current X position
curY = memoryReadInt(myProc, 0x0157D78C); --My current Y position

function saveWaypoint(list)
	keyboardBufferClear();
	io.stdin:flush();
	printf("What do you want to name your path (without .xml)?\nName> ");	-- What do you want to name your path
	filename = getExecutionPath() .. "/waypoints/" .. io.stdin:read() .. ".xml";
	
	file, err = io.open(filename, "w");
	if( not file ) then
		error(err, 0);
	end
	
	file:write(txtList);
end

 
function main()

	--window = findWindow("Xenimus");
	--hdc = openDC(window);

	printf("\n\n\n\n\n\n\n\nXenimus Path Creation Script"
	.."\n\n (7) Waypoint"
	.."\n (8) Attack"
	.."\n (9) Port"
	.."\n (6) Save");
		
    running = true;
    while(running) do		
		--Checking if the key has been pressed.
		
		local keyDown, keyType;
		while(true) do
			keyDown = false;
		
			if(keyPressed(tWp)) then
				keyDown = true;
				keyType = "WP";
			end
			if(keyPressed(tAtt)) then
				keyDown = true;
				keyType = "ATTACK";
			end
			if(keyPressed(tPort)) then
				keyDown = true;
				keyType = "PORT"
			end
			if(keyPressed(tSave)) then
				keyDown = true;
				keyType = "SAVE";
			end
			
			local tmp = {};
			--Key has been pressed and is now released.
			if(keyDown == false and keyType) then
				if(keyType == "SAVE") then
					saveWaypoint(wpList);
					keyType = "";
					running = false;
					break;
				end
				if(keyType == "WP") then
					updatePlayer();
					tmp = { memoryReadInt(myProc, 0x02C85944),
						memoryReadInt(myProc, 0x0157D78C) } 
					keyType = "";
				end
				if(keyType == "tAtt") then
					tmp = { 1, 1 };
					keyType = "";
				end
				if(keyType == "tPort") then
					tmp = { 2, 2 };
					keyTpye = "";
				end
			end
			table.insert(wpList, tmp);
		end --End while true
    end --End while running
end
startMacro(main);
In my save waypoint function i need to turn the argument list which will be a table into a txt format to feed to my xml file.

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Storing and reading data from XML files.

#6 Post by Administrator » Mon Feb 08, 2010 1:18 pm

Code: Select all

local str = tab[1] .. "," .. tab[2]; -- str is now something like "1,1"

Exempt
Posts: 197
Joined: Wed Jan 20, 2010 9:55 am

Re: Storing and reading data from XML files.

#7 Post by Exempt » Mon Feb 08, 2010 2:20 pm

This is my code now but i'm getting an error i cannot seem to figure out.

Code: Select all

scripts\xen/createpath.lua:103: attempt to concatenate global 'wpList' (a table value)

Code: Select all

printf("createpath.lua\n");
 
setStartKey(key.VK_DELETE);
setStopKey(key.VK_END); 

function saveWaypoint(list)
	keyboardBufferClear();
	io.stdin:flush();
	printf("What do you want to name your path (without .xml)?\nName> ");	-- What do you want to name your path
	filename = getExecutionPath() .. "/waypoints/" .. io.stdin:read() .. ".xml";
	
	file, err = io.open(filename, "w");
	if( not file ) then
		error(err, 0);
	end
	local txtList;
	
	for i = 1, #list, 1 do
		txtList = list[i] .. ",";
	end
	
	file:write(txtList);
end

 
function main()

	tWp = key.VK_NUMPAD7;
	tAtt = key.VK_NUMPAD8;
	tPort = key.VK_NUMPAD9;
	tSave = key.VK_NUMPAD6;
	wpList = {};
	
	myProc = openProcess( findProcess("Xenimus") );

	--window = findWindow("Xenimus");
	--hdc = openDC(window);

	printf("\n\n\n\n\n\n\n\nXenimus Path Creation Script"
	.."\n\n (7) Waypoint"
	.."\n (8) Attack"
	.."\n (9) Port"
	.."\n (6) Save\n\n\n");
		
    running = true;
    while(running) do		
		--Checking if the key has been pressed.
		local keyDown, keyType;
		while(true) do
			keyDown = false;
		
			if(keyPressed(tWp)) then
				keyDown = true;
				keyType = "WP";
			end
			if(keyPressed(tAtt)) then
				keyDown = true;
				keyType = "ATTACK";
			end
			if(keyPressed(tPort)) then
				keyDown = true;
				keyType = "PORT"
			end
			if(keyPressed(tSave)) then
				keyDown = true;
				keyType = "SAVE";
			end
			
			local tmp = {}
			--Key has been pressed and is now released.
			if(keyDown == false and keyType) then
				if(keyType == "SAVE") then
					saveWaypoint(wpList);
					keyType = "";
					running = false;
					break;
				end
				if(keyType == "WP") then
					tmp = { memoryReadInt(myProc, 0x02C85944),
						memoryReadInt(myProc, 0x0157D78C) } 
					printf("Waypoint: "..tmp[1]..", "..tmp[2].."\n");
					keyType = "";
				end
				if(keyType == "tAtt") then
					tmp = { 1, 1 };
					if(tmp[1] == 1 and tmp[2] == 1) then
						printf("Waypoint: Attack\n");
					else
						printf("Waypoint: Attack Failed\n");
					end
					keyType = "";
				end
				if(keyType == "tPort") then
					tmp = { 2, 2 };
					if(tmp[1] == 2 and tmp[2] == 2) then
						printf("Waypoint: Port\n");
					else
						printf("Waypoint: Port Failed\n");
					end
					keyTpye = "";
				end
				table.insert(wpList, tmp);
				printf("List: "..wpList.."\n");
			end
		end --End while true
    end --End while running
end
startMacro(main);

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Storing and reading data from XML files.

#8 Post by Administrator » Mon Feb 08, 2010 4:14 pm

Code: Select all

printf("List: "..wpList.."\n");
wpList is a table, not a string. You can't concatenate it like that. If you wanted to, you could do this instead:

Code: Select all

printf("List: "..tostring(wpList).."\n");

Exempt
Posts: 197
Joined: Wed Jan 20, 2010 9:55 am

Re: Storing and reading data from XML files.

#9 Post by Exempt » Mon Feb 08, 2010 8:53 pm

When i use that, I don't get an error but what i do get is confusing me even more...

This just spams constantly after i press one of my keys.

Code: Select all

List: table: 006C1CD0
List: table: 006C1CD0
List: table: 006C1CD0
List: table: 006C1CD0
List: table: 006C1CD0
List: table: 006C1CD0
List: table: 006C1CD0
List: table: 006C1CD0
List: table: 006C1CD0
List: table: 006C1CD0
List: table: 006C1CD0
List: table: 006C1CD0
I did a few more tests and tostring() seems to just return table: (memory address). I was wanting to just print out what the table had in it so i could check it for errors.

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Storing and reading data from XML files.

#10 Post by Administrator » Mon Feb 08, 2010 9:31 pm

Then you would need to iterate through the table and print it out.

Code: Select all

for i,v in pairs(tab) do
  printf("Tab[i]: %s\n", i, tostring(v));
end

Exempt
Posts: 197
Joined: Wed Jan 20, 2010 9:55 am

Re: Storing and reading data from XML files.

#11 Post by Exempt » Tue Feb 09, 2010 2:13 pm

tostring() doesn't return the right values though.

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Storing and reading data from XML files.

#12 Post by Administrator » Tue Feb 09, 2010 7:57 pm

Exempt wrote:tostring() doesn't return the right values though.
That makes no sense. All tostring() does is convert it to a string.

Exempt
Posts: 197
Joined: Wed Jan 20, 2010 9:55 am

Re: Storing and reading data from XML files.

#13 Post by Exempt » Tue Feb 09, 2010 7:59 pm

It's not just converting it to a string though. It returns crazy values. Like my table was list = {53, 52, 53, 52, 53, 52}... the return was 10.

Edit: this code here is suppost to insert a value into my table right?

Code: Select all

tmp = { 2, 2 };
table.insert(wpList, tmp);
Say wpList was wpList = { 4, 4, 5, 5 } before using that function. After it should be wpList = { 4, 4, 5, 5, 2, 2 } right?

My goal is to fill wpList with all the waypoints I add through tmp.

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Storing and reading data from XML files.

#14 Post by Administrator » Tue Feb 09, 2010 8:02 pm

You're doing it wrong then. You are going to have to provide more information if you want help.

Exempt
Posts: 197
Joined: Wed Jan 20, 2010 9:55 am

Re: Storing and reading data from XML files.

#15 Post by Exempt » Tue Feb 09, 2010 8:14 pm

What info do you need exactly?

Code: Select all

printf("createpath.lua\n");
 
setStartKey(key.VK_DELETE);
setStopKey(key.VK_END); 

function saveWaypoint(list)
	keyboardBufferClear();
	io.stdin:flush();
	printf("What do you want to name your path (without .xml)?\nName> ");	-- What do you want to name your path
	filename = getExecutionPath() .. "/waypoints/" .. io.stdin:read() .. ".xml";
	
	file, err = io.open(filename, "w");
	if( not file ) then
		error(err, 0);
	end
	
	local txtList;
	
	for i,v in pairs(list) do
		txtList = sprintf( tostring(i)..","..tostring(v));
	end

	file:write(txtList);
end

 
function main()

	tWp = key.VK_NUMPAD7;
	tAtt = key.VK_NUMPAD8;
	tPort = key.VK_NUMPAD9;
	tSave = key.VK_NUMPAD6;
	wpList = {};
	
	myProc = openProcess( findProcess("Xenimus") );

	--window = findWindow("Xenimus");
	--hdc = openDC(window);

	printf("\n\n\n\n\n\n\n\nXenimus Path Creation Script"
	.."\n\n (7) Waypoint"
	.."\n (8) Attack"
	.."\n (9) Port"
	.."\n (6) Save\n\n\n");
		
    running = true;
    while(running) do		
		--Checking if the key has been pressed.
		local keyDown, keyType;
		while(true) do
			keyDown = false;
		
			if(keyPressed(tWp)) then
				keyDown = true;
				keyType = "WP";
			end
			if(keyPressed(tAtt)) then
				keyDown = true;
				keyType = "ATTACK";
			end
			if(keyPressed(tPort)) then
				keyDown = true;
				keyType = "PORT"
			end
			if(keyPressed(tSave)) then
				keyDown = true;
				keyType = "SAVE";
			end
			
			local tmp = {}
			--Key has been pressed and is now released.
			if(keyDown == false and keyType) then
				if(keyType == "SAVE") then
					saveWaypoint(wpList);
					keyType = "";
					running = false;
					break;
				elseif(keyType == "WP") then
					tmp = { memoryReadInt(myProc, 0x02C85944),
						memoryReadInt(myProc, 0x0157D78C) }
					table.insert(wpList, tmp);
					printf("Waypoint: "..tmp[1]..", "..tmp[2].."\n");
					keyType = "";
					for i,v in pairs(list) do
						print("Coords: "..tostring(i)..","..tostring(v).."\n";
					end
				elseif(keyType == "ATTACK") then
					tmp = { 1, 1 };
					table.insert(wpList, tmp);
					if(tmp[1] == 1 and tmp[2] == 1) then
						printf("Waypoint: Attack\n");
					else
						printf("Waypoint: Attack Failed\n");
					end
					keyType = "";
				elseif(keyType == "PORT") then
					tmp = { 2, 2 };
					table.insert(wpList, tmp);
					if(tmp[1] == 2 and tmp[2] == 2) then
						printf("Waypoint: Port\n");
					else
						printf("Waypoint: Port Failed\n");
					end
					keyType = "";
				end
			end
		end --End while true
    end --End while running
end
startMacro(main);

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Storing and reading data from XML files.

#16 Post by Administrator » Tue Feb 09, 2010 8:32 pm

Code: Select all

Say wpList was wpList = { 4, 4, 5, 5 } before using that function. After it should be wpList = { 4, 4, 5, 5, 2, 2 } right?
Stop that. Treat tables like tables. You are making this way more complicated than it needs to be. If you insert a table into another table, it's going to remain a table. No wonder you're having so many problems.

Code: Select all

local tmp = {1, 2};
table.insert(list, tmp);

for i = 1,#list do
  local t = list[i];
  local x, y = t[1], t[2];

 -- just in case there was an error
  x = x or -1;
  y = y or -1;

  printf("x: %d, y: %d\n", x, y);
end

Exempt
Posts: 197
Joined: Wed Jan 20, 2010 9:55 am

Re: Storing and reading data from XML files.

#17 Post by Exempt » Tue Feb 09, 2010 8:53 pm

Hm, I don't know why i can't grasp this. All i want to do it make a simple script to write one single list to a xml file. I'm going to try what you said there and see what i can get going. thanks again

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest