Scripts / global flags for multiple objects without using separate code for each one

Author
Message
Bolt Action Gaming
GameGuru Tool Maker
11
Years of Service
User Offline
Joined: 24th Oct 2013
Location: Harrisburg, PA (USA)
Posted: 6th Jan 2018 16:36
I'm trying to use a global flag on the lines of:

runonce = {}

function teststuff_init(e)
runonce[e] = 0
end

function teststuff_main(e)
if runonce[e] == 0 then
-- do stuff
runonce[e] = 1
end
end

Instead it doesn't seem to work. I don't want to have to save a piece of code for each object.
Anyone have any ideas?
Thanks
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 6th Jan 2018 16:51
If you want a flag use a flag, i.e. g_runonce[e] = true.

Then the code simply becomes if g_runonce[e] then .... g_runonce[e] = false

Be warned though that I find the 'init' function is not guaranteed to be run every time for some reason.
Been there, done that, got all the T-Shirts!
PM
Bolt Action Gaming
GameGuru Tool Maker
11
Years of Service
User Offline
Joined: 24th Oct 2013
Location: Harrisburg, PA (USA)
Posted: 6th Jan 2018 17:50
fair enough but it's still not functioning.

It doesn't appear to ever set the value.
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 6th Jan 2018 18:09
Try rebooting GG to see if it works then. It should at least work the first time you test it.
Been there, done that, got all the T-Shirts!
PM
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 6th Jan 2018 18:52
are you trying to have 1 variable toggle between multiple scripts?
you have the array set per entity like that, you want it to just be a normal variable if it will be affected by all of them
i.e.

otherwise they will all get their own version of the variable
like runonce[1] = 0
runonce[2] = 0
runonce[3] = 1
etc
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
Bolt Action Gaming
GameGuru Tool Maker
11
Years of Service
User Offline
Joined: 24th Oct 2013
Location: Harrisburg, PA (USA)
Posted: 6th Jan 2018 20:09
Their own version of the variable is what I want.

Otherwise what happens is one of the items sets runonce to 1 and the rest of them never do their initial run.
Bolt Action Gaming
GameGuru Tool Maker
11
Years of Service
User Offline
Joined: 24th Oct 2013
Location: Harrisburg, PA (USA)
Posted: 6th Jan 2018 20:16
Also so what I'm trying to do is have one variable, on one script, used by multiple objects.
So each individual object will all run this same script, but then toggle their own flag.
I don't want to have to make a separate script for each object, which is just asinine.
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 6th Jan 2018 20:21
well then your initial code is correct, most likely you are just making a mistake somewhere - double check all spelling and that you aren't toggling the variable in any wrong places
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
Bolt Action Gaming
GameGuru Tool Maker
11
Years of Service
User Offline
Joined: 24th Oct 2013
Location: Harrisburg, PA (USA)
Posted: 6th Jan 2018 20:28
So I think I got it. I did have to use 0 and 1, btw. Using true/false wouldn't set.
I don't know why?

Final code:
objname={}
g_runonce={}
function teststuff_init(e)
g_runonce[e] = 0
end

function teststuff_init_name(e, name)
objname[e] = name

end

function teststuff_main(e)
if g_runonce[e] == 0 or g_runonce[e] == nil then
customfunction(objname[e])
g_runonce[e] = 1
end
end

AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 6th Jan 2018 21:11
Aha, you have two 'init' functions, I don't think that will work only one or the other but not both!

It works now because you added an explicit check for 'nil' which is what it will always give when not initialised!
Been there, done that, got all the T-Shirts!
PM
Belidos
3D Media Maker
9
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 6th Jan 2018 22:04
Quote: "Be warned though that I find the 'init' function is not guaranteed to be run every time for some reason. "


I thought the init part was only supposed to run once, when the script is first spawned, hence the name init (short for initialize).

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 7th Jan 2018 10:24
It is, sometimes it doesn't or at least it may not get called before the first 'main' call.
Been there, done that, got all the T-Shirts!
PM
Belidos
3D Media Maker
9
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 7th Jan 2018 10:32
ah I see, not noticed that happening yet, but I don't do much in the way of scripting anyway.

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
Bolt Action Gaming
GameGuru Tool Maker
11
Years of Service
User Offline
Joined: 24th Oct 2013
Location: Harrisburg, PA (USA)
Posted: 8th Jan 2018 00:25
I don't think the double init functions matter or it'd affect 90% of the programs made where someone is referencing smallg's code

the nil was actually a holdover from a previous attempt with the false/true settings. I took it out and it functioned just fine without it. For whatever reason, setting a table to have a value of true/false was breaking things.

smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 8th Jan 2018 11:13
Pretty sure I have never used a double init, there's no reason to, they both would do the same thing except one can return the name property, I also had no idea multiple would work but I guess it makes sense, they are only functions after all
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
Bolt Action Gaming
GameGuru Tool Maker
11
Years of Service
User Offline
Joined: 24th Oct 2013
Location: Harrisburg, PA (USA)
Posted: 8th Jan 2018 14:37
Could have sworn I got it from your blood code. Maybe not? I'd have to check but whatever. It is what it is. Thanks for the help either way guys.

Login to post a reply

Server time is: 2024-12-23 00:22:39
Your offset time is: 2024-12-23 00:22:39