The Consistency of Hedgewars Gear References

5 replies [Last post]
UltiMaxKom
UltiMaxKom's picture
User offline. Last seen 4 years 44 weeks ago. Offline
Joined: 2016-06-26
Posts: 381

Hello my dear reader!
Here I am again, requesting any help from anybody that could help me (huh... isn't that obvious?)
Welp, let's sink into the issue in this very insta:

Suppose we test this Hedgewars Lua Script, let's call it as "test.lua".
Which I designed to work this way: Every vgtHealthTag and vgtSmallDamageTag visual gear will be deleted upon creation so there's no message created when a hedgehog is damaged/etc BUT ONLY IF they're not 'approved' [local = GApprovedVG]. Then I try to make one 'approved' vgtHealthTag visual gear at onNewTurn() event. I expect this last created vgtHealthTag showed up, because I mark it as 'approved' by inserting a value into GApprovedVG[vgUid] = 100.
A better look at the code should help you wrap what I'm trying/how I try to do the things.
Please take look at the code below:

local GApprovedVG = {}

function onVisualGearAdd(vg)
	if GetVisualGearType(vg) == vgtHealthTag or GetVisualGearType(vg) == vgtSmallDamageTag then
		if vg and GApprovedVG[vg] == nil then
			DeleteVisualGear(vg)
		end
	end
end

function onNewTurn()
	local x,y = GetGearPosition(CurrentHedgehog)
	local vg = AddVisualGear(x, y, vgtHealthTag, 100, true)
	SetVisualGearValues(vg, nil, nil, nil, nil, nil, nil, nil, nil, nil, 0xffffffff)
	GApprovedVG[vg] = 100
end

And as you can see; I expect that vg created at onNewTurn() should be NOT deleted because the expression "vg and GApprovedVG[vg] == nil" clearly supposed to be false, thus there should be no deletion for any 'approved' vg. But yet, when I debug the expression boolean value, it returned true, but why? Well, I think the question why can be answered by my little hypothesis: "The vgUid reference value over a gear/visual gear isn't consistent through Hedgewars Lua system, or at least there's a problem with the vgUid referencing system itself". But well, that's doesn't make sense, right? Why would the vgUid reference system inconsistent? Or because there's something messed up inside? A problem, OR ISN'T IT? Well, tbh idk for sure, and that's why I bring this personal-little-trivial-issue here, to make sure my thought and to get the right answer...
Code Note: If you remove 'DeleteVisualGear(vg)' from the code above, you will find that the vgtHealthTag created at onNewTurn() showed up perfectly.
But for now, as my best guess and bet; I just simply messed things up myself here, thus rendering the said test.lua work improperly like it should be. I just wanted to know what's my mistake here, so yeah, don't hesitate to tell my mistake (if I had any) okay =) ?

I hope you could understand the issue of me in this post, especially I hope a dev could lend me a little hand on this trivial question, hehe.
Thanks ^^

PS: To gain the best clarity, please to consider testing on your own machine by copying the provided code above then run it in Hedgewars.

[Licensed as: CC-O]

╟───NW──────┼──────N╢

╓──────────────────╖ ╓──────────────────╖
⠀HP: ██████████ 1E9/1E91E9/1E9 ██████████ :MP
╙──────────────────╜ ╙──────────────────╜

nemo
nemo's picture
User offline. Last seen 19 weeks 3 days ago. Offline
Joined: 2009-01-28
Posts: 1861

14:03 <@nemo_admin> UltiMaxKom: I'm reading your forum post
14:03 <@nemo_admin> UltiMaxKom: local vg = AddVisualGear(x, y, vgtHealthTag, 100, true)
14:03 <@nemo_admin> happens before
14:03 <@nemo_admin> GApprovedVG[vg] = 100
14:03 <@nemo_admin> UltiMaxKom: you call Add
14:03 <+UltiMaxKom> OH MY FFFFFUMES
14:03 <@nemo_admin> then immediately delete it
14:04 <+UltiMaxKom> nemo_admin: DERPFACE
14:04 <@nemo_admin> so the other thing [the insert] never happens [in time]
14:04 <+UltiMaxKom> nemo_admin: I see now, lmao, damnit Ulti! Good job xD
14:04 <+UltiMaxKom> what a big mistake!
14:04 <+UltiMaxKom> ...
14:04 <+UltiMaxKom> nemo_admin: then how I supposed to do it right?
14:05 <@nemo_admin> UltiMaxKom: how about defering the delete?
14:05 <+UltiMaxKom> okay, what is 'defering' anyway?
14:06 <@nemo_admin> another option would be to guard that one particular add
14:06 <@nemo_admin> either one
14:06 <@nemo_admin> set protection flag before the add, remove it after.
14:06 <@nemo_admin> or else stuff the ones for deletion into an array and clean them up later in onGameTick20 or something
14:06 * nemo_admin shrugs
14:06 <+UltiMaxKom> hmmm...
14:07 <+UltiMaxKom> nemo_admin: what do you think for the best?
14:07 <@nemo_admin> protection flag would ensure the delete happened promptly

--
Oh, what the heck. 1PLXzL1CBUD1kdEWqMrwNUfGrGiirV1WpH <= tip a hedgewars dev

UltiMaxKom
UltiMaxKom's picture
User offline. Last seen 4 years 44 weeks ago. Offline
Joined: 2016-06-26
Posts: 381

nemo allegedly wrote:

14:03 <@nemo_admin> UltiMaxKom: I'm reading your forum post
14:03 <@nemo_admin> UltiMaxKom: local vg = AddVisualGear(x, y, vgtHealthTag, 100, true)
14:03 <@nemo_admin> happens before
14:03 <@nemo_admin> GApprovedVG[vg] = 100
14:03 <@nemo_admin> UltiMaxKom: you call Add
14:03 <+UltiMaxKom> OH MY FFFFFUMES
14:03 <@nemo_admin> then immediately delete it
14:04 <+UltiMaxKom> nemo_admin: DERPFACE
14:04 <@nemo_admin> so the other thing [the insert] never happens [in time]
14:04 <+UltiMaxKom> nemo_admin: I see now, lmao, damnit Ulti! Good job xD
14:04 <+UltiMaxKom> what a big mistake!
14:04 <+UltiMaxKom> ...
14:04 <+UltiMaxKom> nemo_admin: then how I supposed to do it right?
14:05 <@nemo_admin> UltiMaxKom: how about defering the delete?
14:05 <+UltiMaxKom> okay, what is 'defering' anyway?
14:06 <@nemo_admin> another option would be to guard that one particular add
14:06 <@nemo_admin> either one
14:06 <@nemo_admin> set protection flag before the add, remove it after.
14:06 <@nemo_admin> or else stuff the ones for deletion into an array and clean them up later in onGameTick20 or something
14:06 * nemo_admin shrugs
14:06 <+UltiMaxKom> hmmm...
14:07 <+UltiMaxKom> nemo_admin: what do you think for the best?
14:07 <@nemo_admin> protection flag would ensure the delete happened promptly


LMAO!
Oh man, nemo forbid me from deleting this post lol
Although, this is kinda hilarious xD

I'd just hope there's no-one read this post...
EVER...
AGAIN...

╟───NW──────┼──────N╢

╓──────────────────╖ ╓──────────────────╖
⠀HP: ██████████ 1E9/1E91E9/1E9 ██████████ :MP
╙──────────────────╜ ╙──────────────────╜

mikade
mikade's picture
User offline. Last seen 45 weeks 1 day ago. Offline
Joined: 2010-10-22
Posts: 355

UltiMaxKom allegedly wrote:

what a big mistake!
I'd just hope there's no-one read this post...
EVER...
AGAIN...

mikade
Hedgewars Developer

UltiMaxKom
UltiMaxKom's picture
User offline. Last seen 4 years 44 weeks ago. Offline
Joined: 2016-06-26
Posts: 381

mikade allegedly wrote:

UltiMaxKom allegedly wrote:

what a big mistake!
I'd just hope there's no-one read this post...
EVER...
AGAIN...



Okay, this leads me to my ultimate mental breakdown where it can be over very soon, or else to ask nemo the forbidden ritual itself where I can kill every character in this threadverse including em' beloved lowercase children in an insta; thus ending this cursed, selfblowing-post xD (rofl + sitting in the corner thinking "What have I done") #chokeme #hi #dankface

╟───NW──────┼──────N╢

╓──────────────────╖ ╓──────────────────╖
⠀HP: ██████████ 1E9/1E91E9/1E9 ██████████ :MP
╙──────────────────╜ ╙──────────────────╜

nemo
nemo's picture
User offline. Last seen 19 weeks 3 days ago. Offline
Joined: 2009-01-28
Posts: 1861

Heh. BTW. One more (possibly silly) way to protect a VG before it was actually issued would be to create *another* VG immediately before, then "protect" that VG ID+1. Since the IDs are issued sequentially.

But I like the flag better since it doesn't rely on unguaranteed engine behaviour Smile

--
Oh, what the heck. 1PLXzL1CBUD1kdEWqMrwNUfGrGiirV1WpH <= tip a hedgewars dev

User login

Copyright © 2004-2024 Hedgewars Project. All rights reserved. [ contact ]