Forum

> > CS2D > Scripts > C stack overflow
Forums overviewCS2D overview Scripts overviewLog in to reply

English C stack overflow

13 replies
To the start Previous 1 Next To the start

old C stack overflow

pbeloto
User Off Offline

Quote
Help me , remove the error "C stack overflow" , i shot the npc (id = 1) = "Zumbie" , there appears this error

Plz help, Thanks you


Script :

1
2
3
4
5
6
7
8
9
10
11
addhook("objectdamage","_objectdamage")
function _objectdamage(id_o,dmg,id_pl)
	if id_o > 0 then
		if object(id_o,"type") == 30 then
			if object(id_o,"player") == 1 then	
			parse("damageobject "..id_o.." 1 "..id_pl)
			return 1
			end
		end
	end
end

old Re: C stack overflow

KimKat
GAME BANNED Off Offline

Quote
I believe it might be due to this part...
if id_o > 0 then -- = If <player ID> is greater than (>) zero perform following action.
That might include the NPC/dynamic object's ID perhaps causing a somewhat strange error. Because it might check for both yours and other types ID and perform action based on the ID's found. That's just my assumption I don't know if it's correct though.

Maybe it's also due to line 5, 6. That's the possible problems I can spot out anyhow.

old Re: C stack overflow

Starkkz
Moderator Off Offline

Quote
@user KimKat: It isn't quite hard to discover the error.

Your hook is called when an object gets damaged, however on the sixth line you are using a command that damages objects. As you added that command on the hook, the game will start to call that function an infinite amount of times (Stack overflow) and it will not let the game continue processing correctly if you don't add conditionals for that.

old Re: C stack overflow

MikuAuahDark
User Off Offline

Quote
And also you can't check a NPC Type(like zombie,headcrab,etc.)

So, on line 6, that operation only return true if that object is spawned by player with ID 1.

old Re: C stack overflow

DC
Admin Off Offline

Quote
@user MikuAuahDark: This is not well documented but user pbeloto is actually doing it right. For NPCs the "player" field is used to store the subtype of the NPCs. This field is used for the subtype by the game because NPCs don't belong to players like buildings. So that part of the code is correct.

The overflow is caused by an infinite loop like user Starkkz already explained. Calling a function in a hook which triggers the same hook again is always critical. Make sure to add proper conditions in such cases to avoid infinite loops.

old Re: C stack overflow

KimKat
GAME BANNED Off Offline

Quote
It's kind of recursion due to the parsing of cs2d cmd damageobject which is interconnected to the hooked function _objectdamage (cs2d lua hook objectdamage). To fix this would be to revise line 6 in a way, so that it doesn't happen. But I currently have no solution since I personally have never really touched that hook.

As a on the fly solution try by not hooking the function and simply calling it when needed and with proper parameters. In theory it might work much better for you. Also thanks to everyone for clarifying this.

old Re: C stack overflow

Infinite Rain
Reviewer Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
allowPass = true
addhook("objectdamage","_objectdamage")
function _objectdamage(id_o,dmg,id_pl)
     if allowPass then
          if id_o > 0 then
               if object(id_o,"type") == 30 then
                    if object(id_o,"player") == 1 then     
                         allowPass = false
                         parse("damageobject "..id_o.." 1 "..id_pl)
                         allowPass = true
                         return 1
                    end
               end
          end
     end
end

Should work now, I used the most basic method.

old Re: C stack overflow

pbeloto
User Off Offline

Quote
and if I call this command only once in the game?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
npc_force = true

addhook("objectdamage","_objectdamage")
function _objectdamage(id_o,dmg,id_pl)
	if npc_force ==  true then
		if id_o > 0 then
			if object(id_o,"type") == 30 then
				if object(id_o,"player") == 1 then     
					parse("damageobject "..id_o.." 1 "..id_pl)
					npc_force == false
				return 1
				end
			end
		end
	end
end

Error: active and desactive for command '-' , 1 shot = cmd 1 , 2 shot = shot weapon
edited 1×, last 11.10.13 05:54:20 pm

old Re: C stack overflow

pbeloto
User Off Offline

Quote
user Infinite Rain has written
Use my script, your version is wrong. My version will work perfectly.



OMG , thanks ... , go test script me server ??

Name : Rp server (BETA)
IP :189.54.117.71:36963

passworld : 157 or none passworld

'-'

old Re: C stack overflow

omg
User Off Offline

Quote
im not sure what else u implemented but the simplest solution would be to put parse("damageobject "..id_o.." 1 0") or something similar
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview