Forum

> > CS2D > Scripts > Super Hero save system
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Super Hero save system

10 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Super Hero save system

_Yank
User Off Offline

Zitieren
Hello people. In this post i came to ask for your help. Im still learning lua and now my lua knowledge is [7.2/10]

Im making saving feature the the super hero mod but im getting errors. Is not that errors with lua, the result of the script is not exactly what i wanted.
Here is the script :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
function sh_saveheroes(id)
	local ida = 0
	local i
	for ida = 0,sheroescount[id] do -- I made an array for each time the player selects a hero it adds 1 
		-- example, player has 2 heros (flash and superman) it will stop at 2 (for ida,2 do)
		for i = 1,36 do
			if sh_id[(id - 1) * 36 + i] > 0 then -- then it checks all heroes and checks if the player have it (example : sh_id[12] = 1) -- Means the player has the hero with id 12
				file = assert(io.open(sh_dir..'sh_data/'..player(id,"usgn")..'lol.txt','a')) -- writes the player heroes
				file:write('sh_id[(id - 1) * 36] = '..i..'\n') -- continuation 
				file:close()
			end
		end
	end
end

The problem is that cs2d repeats the heroes of the player in the file. Example of the file usgnlol.txt:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sh_id[(id - 1) * 36] = 1
sh_id[(id - 1) * 36] = 2
sh_id[(id - 1) * 36] = 3
sh_id[(id - 1) * 36] = 6
sh_id[(id - 1) * 36] = 7
sh_id[(id - 1) * 36] = 10
sh_id[(id - 1) * 36] = 1
sh_id[(id - 1) * 36] = 2
sh_id[(id - 1) * 36] = 3
sh_id[(id - 1) * 36] = 6
sh_id[(id - 1) * 36] = 7
sh_id[(id - 1) * 36] = 10
sh_id[(id - 1) * 36] = 1
sh_id[(id - 1) * 36] = 2
sh_id[(id - 1) * 36] = 3
sh_id[(id - 1) * 36] = 6
sh_id[(id - 1) * 36] = 7
sh_id[(id - 1) * 36] = 10
1× editiert, zuletzt 21.07.13 13:19:50

alt Re: Super Hero save system

EngiN33R
Moderator Off Offline

Zitieren
user _Yank hat geschrieben
Im still learning lua and now my lua knowledge is [7.2/10]

A daring assumption, and a surprisingly precise one at that.

The code is fairly confusing. You check if a player has selected a hero and if they did you write it to the file, but you do that once for each time a player has selected a hero - and then once more (because you begin at 0).

What I don't understand is why you do that - why you do it as many times as a player has selected a hero. If you remove the outer for loop it should work correctly.

Also, for loops in Lua don't need their variables declared. So in the end, your code should look like this:

1
2
3
4
5
6
7
8
9
function sh_saveheroes(id)
	for i = 1,36 do
		if sh_id[(id - 1) * 36 + i] > 0 then
			file = assert(io.open(sh_dir..'sh_data/'..player(id,"usgn")..'lol.txt','a'))
			file:write('sh_id[(id - 1) * 36] = '..i..'\n') -- continuation 
			file:close()
		end
	end
end

alt Re: Super Hero save system

_Yank
User Off Offline

Zitieren
That was what i did first but i used write mode to io.open, im now testing it anyway thanks.

EDIT:Didnt worked, or, it worked but i writes does the same usgnlol.txt and if i try to use io.open with write mode it only writes the first hero i selected.
2× editiert, zuletzt 21.07.13 15:31:31

alt Re: Super Hero save system

_Yank
User Off Offline

Zitieren
I will try to explain better.

There is a function called sh_pl_save (or something like that) which saves the player exp when the player kill someone. Then i added "sh_saveheroes(id)" to it and wrote the function "sh_saveheroes(id)". I do what you said in last comment and i checked the file. I only saw "sh_id[(id - 1) * 36] = 1". It only writes one and it deletes the previous. Then i try to use append mode and that happened. I thought that was because for loop but didnt make much sense.

alt Re: Super Hero save system

EngiN33R
Moderator Off Offline

Zitieren
So basically right now it still duplicates the data when you use it with append mode?

If it is, you could either make one big string to concatenate everything with and then write it using write mode, or you could remove the contents of the file and then write the data using append mode.

alt Re: Super Hero save system

oxytamine
User Off Offline

Zitieren
Your logic is flawed, I mean your code reminds me of this.
1
2
3
4
5
6
7
<?php
for($i=0;$i<10;$i++) {
	for($i=0;$i<5;$i++) {
		echo 'wtf it's deadlock';
	}
}
?>
No surprise it's not working as it should.

alt Re: Super Hero save system

_Yank
User Off Offline

Zitieren
Hm.. Sorry im not very good explaining this things. You problably will need to read and understand super hero 1.2 script. Sorry to make you waste your time in this.
But now i got a better idea of what append mode to io.open is. Now i want to set the end of the file with file:seek. But i need to use lines.

I found a way to use append mode:
The append mode stops writing on the end of file. We need to specify the end with "file:seek" or it goes forever. Then i want to specify the end of file with lines. Will use sheroescount to set the end of file in lines.
Please help
1× editiert, zuletzt 22.07.13 15:06:32

alt Re: Super Hero save system

Tobey
User Off Offline

Zitieren
user oxytamine hat geschrieben
Your logic is flawed, I mean your code reminds me of this.
1
2
3
4
5
6
7
<?php
for($i=0;$i<10;$i++) {
	for($i=0;$i<5;$i++) {
		echo 'wtf it's deadlock';
	}
}
?>
No surprise it's not working as it should.


lmao.

This pretty much sums it up at all.

alt Re: Super Hero save system

_Yank
User Off Offline

Zitieren
Then guys, if you dont understand what i wrote up i will try to explain us from zero. What i want to make is a script that checks hero players :

Info : sh_id is an array
1
2
3
4
5
6
7
8
9
10
11
Example:sh_id[(id - 1) * 36 + 2] is Super Man Hero. If player has that hero the value of it is 1. Else if player hasn't that hero the value is "0". Then the code is :

addhook("kill","saveheroes")
function saveheroes(killer)
for i = 1,36 do
	if sh_id[(killer - 1) * 36 + i] > 0 then
		-- write sh_id[(killer - 1) * 36 + i] = 1
	end
end

Then if player has the Super Man Hero "sh_id[(id - 1) * 36 + 2]" it will write sh_id[(killer - 1) * 36 + 2] = 1.

But the problem is, it deletes the previous text and writes the new instead of write the new text in next line.
1× editiert, zuletzt 24.07.13 17:33:36
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht