Jump to content
anaximandross

Local Variable in Global Space

Recommended Posts

4 hours ago, Harzach said:

Wait, does CBA no longer wrap init/onact field code in a call? Just tested with CBA and it's throwing the local in global error as well.

 

Maybe my brain is just turning to oatmeal.

 

I think it'd only wrap it AFTER either hitting "OK" on the trigger or saving the mission (or some other event, been a little while). Either way, code works, but doesn't like going into a trigger. So maybe it is a scripted trigger like you mentioned (which means we need the trigger code.....)

Share this post


Link to post
Share on other sites
4 hours ago, beno_83au said:

I think it'd only wrap it AFTER either hitting "OK" on the trigger

 

That's just it, it's throwing the error on "OK." Recent CBA updates have broken some things, maybe this is another one. Or, again, I'm losing my faculties.

Share this post


Link to post
Share on other sites
2 minutes ago, Harzach said:

Or, again, I'm losing my faculties.

It's allowed from time to time 🤪

 

But CBA has had an error related to XEHs - 

 

Share this post


Link to post
Share on other sites

ok I loaded up ArmA 3 this afternoon and I was greeted by a few error messages

 

unknown.png

 

 

Share this post


Link to post
Share on other sites

If you use ace together with rhs then you need those compatibility mods mentioned in the warnings.

If this is the case then just search the workshop for those mods, subscribe and load them.

Share this post


Link to post
Share on other sites

OK so I downloaded this mod:

https://steamcommunity.com/sharedfiles/filedetails/?id=2216362075&searchtext=ace+compatibility

Hopefully it works!

 

EDIT: so ArmA 3 loaded fine, no errors, went into the editor and loaded the mission fine, no errors, went to paste in the script... local variable in global space... FFS

 

Share this post


Link to post
Share on other sites

It's a valid error. CBA used to automatically wrap it, but now it seems it doesn't. There's nothing stopping you from doing it correctly. 

Share this post


Link to post
Share on other sites

Yes. As Harzach said, just spawn/call the code yourself from within the trigger's On Activation.

Share this post


Link to post
Share on other sites
14 hours ago, Harzach said:

It's a valid error. CBA used to automatically wrap it, but now it seems it doesn't. There's nothing stopping you from doing it correctly. 


Apologies in advance but I'm not very good at writing my own scripts, can you explain to me what you mean by "wrapping" the text?

 

I'm wondering if I created a .sqf and in the trigger use the ExecVM command to activate the script would that work or is there an easier/simplier solution.

 

Again apologies if I'm saying somerthing stupid, I'm not very good with scripts, I usually just get ideas for missions and look for scripts pre-written and get them to work for me.

Share this post


Link to post
Share on other sites
2 hours ago, BikerJoe said:

I'm wondering if I created a .sqf and in the trigger use the ExecVM command to activate the script would that work or is there an easier/simplier solution.

 

Yes, and it'll be easier to work with your script if it starts getting bigger/more complex.

  • Like 2

Share this post


Link to post
Share on other sites
2 hours ago, BikerJoe said:

Apologies in advance but I'm not very good at writing my own scripts, can you explain to me what you mean by "wrapping" the text?

 

not wrapped:

_ammo = getArtilleryAmmo [gun1] select 0;
_tgt = getMarkerPos "target1";
gun1 doArtilleryFire[_tgt,_ammo,10];

 

wrapped in a spawn:

private _dummy = [] spawn
{
 _ammo = getArtilleryAmmo [gun1] select 0;
 _tgt = getMarkerPos "target1";
 gun1 doArtilleryFire[_tgt,_ammo,10];
};

 

Idk why this should help but eden triggers are not my subject area...

  • Like 1

Share this post


Link to post
Share on other sites
53 minutes ago, sarogahtyp said:

 

not wrapped:


_ammo = getArtilleryAmmo [gun1] select 0;
_tgt = getMarkerPos "target1";
gun1 doArtilleryFire[_tgt,_ammo,10];

 

wrapped in a spawn:


private _dummy = [] spawn
{
 _ammo = getArtilleryAmmo [gun1] select 0;
 _tgt = getMarkerPos "target1";
 gun1 doArtilleryFire[_tgt,_ammo,10];
};

 

Idk why this should help but eden triggers are not my subject area...

 So would I be correct in thinking that by "wrapping" you literally just put { before the start and } at the end?

Share this post


Link to post
Share on other sites

 

On 7/13/2021 at 11:22 PM, Harzach said:

Is this trigger scripted? If not, are you wrapping your code in a call or using CBA? Pasting that code into an OnAct or init field yields the "local in global" error unless you are using CBA.

 

all posts 'bout wrapping are related to this.

wrapping is just wrapping. ask your favorite translation software for it...

 

what harzach means was to wrap your code with a call scope. that would look like what I posted but with the call instead of a spawn.

 

Edit: huh, Ireland ... translation software should not be necessary 😉

Share this post


Link to post
Share on other sites
1 hour ago, beno_83au said:

 

Yes, and it'll be easier to work with your script if it starts getting bigger/more complex.

 

I ended up doing this and instead of 1 artillery unit I had 3, because why not!

Thank you all for your help and patience. The issue is now resolved (for now - I hope!)

 

  • Like 2

Share this post


Link to post
Share on other sites
On 1/30/2019 at 2:37 PM, opusfmspol said:

Clear the .rpt log and run the mission once.  What does .rpt log report as the issue?

Sorry to pull up old stuff but this is getting me so close to what I need that I was hoping you could help. I want a task to complete when a player opens a crate (gearCrate).

I have this in the crates init

crateOpen = false; this addEventHandler ["ContainerOpened", {params ["gearCrate", "player"]; crateOpen = true;}];

and when the player opens the crate, i get "error local variable in global space". This is my first time working with event handlers so I'm sure I'm just missing something simple here.

 

The rpt. gives me this but didn't really enlighten me.

15:09:55 Error in expression <is addEventHandler ["ContainerOpened", {params ["gearCrate", "player"]; crateOpe>
15:09:55   Error position: <params ["gearCrate", "player"]; crateOpe>
15:09:55   Error Local variable in global space

thanks for any help, ive been stuck for a bit now

Share this post


Link to post
Share on other sites
10 minutes ago, SupremeTDM said:

15:09:55 Error position: <params ["gearCrate", "player"]; crateOpe>

 

Params "parses input argument into array of private variables." Global variables can not by definition be private. Also, you are trying to redefine these variables into what that already were.

 

The variables shown in the Biki example are not placeholders:

this addEventHandler ["ContainerOpened", {
	params ["_container", "_unit"];
}];
//  container: Object - cargo container
//  player: Object - unit who accessed the container

So, the EH is attached to the container in question, and the unit is always the player opening the container.

crateOpen = false; this addEventHandler ["ContainerOpened", {params ["_container", "_unit"]; crateOpen = true;}];

 

  • Like 2

Share this post


Link to post
Share on other sites
6 minutes ago, Harzach said:

Params "parses input argument into array of private variables."

this flys over my head so I'm going to read as much as I can on params and variables. Thanks for this!

 

7 minutes ago, Harzach said:

The variables shown in the Biki example are not placeholders

again, thanks! I couldn't find a solid answer on this when googling if i needed to replace them or not.

 

I just tested it again and 

crateOpen = false; this addEventHandler ["ContainerOpened", {params ["_container", "_unit"]; crateOpen = true;}];

this works perfect 🙂

Share this post


Link to post
Share on other sites
9 minutes ago, SupremeTDM said:

this flys over my head

 

Params parses input argument into array of private variables.

this addEventHandler ["ContainerOpened", {params ["_container", "_unit"]; crateOpen = true;}];

The input argument here is "this" - as this command is run in the init of an object, "this" is the object itself. The EH sees the object as an array containing the object and the unit opening the object. The command "params" then declares those as private variables. 

 

So, this:

params ["_container", "_unit"];

is essentially the same as this:

private _container = _this select 0;
private _unit = _this select 1;

Have a look through the first few links in my sig for some more basic info if you are still confused, and feel free to ask questions!

  • Like 2

Share this post


Link to post
Share on other sites
7 minutes ago, Harzach said:

parses

had to google this word lol. 

 

7 minutes ago, Harzach said:

and the unit opening the object

Im confused how it knows the unit I want to open it is the player? hence why I filled in "_unit" thinking it was a place holder. This is a SP mission so probably not necessary to specify player anyway but im still confused

 

7 minutes ago, Harzach said:

private _container = _this select 0; private _unit = _this select 1;

I don't understand the select 0 and select 1 but am reading into it now. I have never used select so like every other command, I have some experimenting to do 🙂

 

again, thanks for taking the time! It is most appreciated and your sig is quite helpful

Share this post


Link to post
Share on other sites
21 minutes ago, SupremeTDM said:

Im confused how it knows the unit I want to open it is the player

 

It doesn't. It knows what player is currently opening the container.

 

In MP, you could then filter for a more specific player, but as you understand, in SP it doesn't matter as there is only one player.

  • Like 1

Share this post


Link to post
Share on other sites
18 minutes ago, Harzach said:

 

It doesn't. It knows what player is currently opening the container.

 

In MP, you could then filter for a more specific player, but as you understand, in SP it doesn't matter as there is only one player.

thank you again for explaining, I understand somewhat better the script you wrote but it will take me a long time to get to that line of thinking on my own (params / variables) when solving a problem.

My mindset right now seems to be limited (even tho im just making a SP), such as I try to keep it as basic as possible with a + b = c which I thought for this situation would be as simple as player + crate open = task complete. I get focused on the player aspect instead of just making the crate react how I want (which is how I understand the script you wrote for now)

Share this post


Link to post
Share on other sites
2 minutes ago, SupremeTDM said:

I get focused on the player aspect instead of just making the crate react how I want

 

That's one of the fun/difficult things about coding, and about many things in life in general. Looking at it from different angles/approaches leads to different solutions, some of which are "better" than others.

 

And in this case, it's not even the "crate reacting," it's the game detecting the crate being opened. The Event being Handled.

 

It's a deep pool. Have fun!

Share this post


Link to post
Share on other sites
13 minutes ago, Harzach said:

The Event being Handled.

this is my first EH in my mission so no wonder I didn't understand it. This is very helpful to keep in mind!

 

So that's why I put crateOpen in the cond. of my task complete trigger? I couldnt figure that out at first.

 

Explained to myself: my event is the crate opening which makes crateOpen true which then satisfies the trigger cond. crateOpen; (by returning true) or hopefully I understand it right haha, I never know for sure.

 

13 minutes ago, Harzach said:

It's a deep pool. Have fun!

It sure is! I just hit 300+ hours and will keep going thanks to people like you

  • Like 1

Share this post


Link to post
Share on other sites
43 minutes ago, SupremeTDM said:

hopefully I understand it right

 

You got it 👍

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×