Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Hi and thanks for using my library. Glad it's being useful.

It would be very helpful to check out your examples in more detail to understand what you are trying to do, but I'd anticipate most cases are totally doable.

The scope for setCallback is object where the panel and widget is defined. For example, the following code, in the Create event of the obj_Player object, will rotate said object's sprite when held:


If the panel is created in another object, and there's only one instance, you can refer to the calling object and/or instance directly:


If you want to do something slightly more complex, you can bind the function to a specific instance via a method. For example, if there are three obj_Player instances, you can dynamically create three buttons in a panel, each of which rotates a specific obj_Player instance:



For the last example, I have attached a very small project here.

Please, share your code and or detail your use cases and I will happily guide you.

(1 edit)

Thank you for the reply. I just cleaned up the main section of code I am having problems with and tried implementing what you sent, but I still can't get it to work. The code above works, but only because of the hard-coded fix I put after the for loop. 

I apologize if my code is hard to read because I am entirely self-taught and don't know any standard variable conventions. I want to use a loop so I can dynamically add new buttons. 

The ".setImageAlpha" sections are only there for testing purposes so I can see results faster, so my main concern is the array updating portion. This basically just changes a variable that holds information on what is equipped to the player. I am trying to set up a system that will allow me to add more options quickly in the future while modifying as little code as possible. If this isn't enough for you to help me, I can share more of the project, but fair warning, my code is very cluttered right now.

there was one other thing I was trying to do that is less important, but I thought I would still bring it up. I first ran into troubles when I tried setting up the .setCallback in a script so I could reduce the size of my code, but I had similar issues to the above problem where the function couldn't find the variable I wanted to access. my current hard-coded solution is sufficient for my project so no worries if we can't figure this one out.

The problem you have is indeed with variable scoping, not gooey. The basic idea is that local variables are not passed to functions/methods in the scope automatically, like in other programming languages (this automatic behavior, which is not present in GML right now, is called "closure"). So in order to be able to access those variables from inside those functions/methods, you need to "bind" the method to a struct which contains the variables you need, using the `method()` call (this is what I did in the example above). 

More info about the method function and its uses in the manual.

In your example, I'm not really understanding what `obj_cybterm` is (I think you have a terminal which the player can access and you are displaying one button per each upgrade the player can choose for its left and right robotic arm, or something), but the first issue I see is that, even though you are using `method()` to bind the `_id` variable (which holds the reference to the specific `obj_cybterm` object you found with the `instance_find` function), then on the callback code you are not referencing that variable. Also, you are using the local variable `_i` inside the function, which has not been declared in the struct.

Without seeing the project I cannot recommend the exact code to use, but I'm sure this is the issue. If you are willing to share your project I will happily review and let you know what options you have. The other option is I can try to quickly whip up a project with the functionality I described so you can take it from there.

Finally for the second part you can of course use a script, but again, you'll have to be careful to bind it to the appropriate instance in order to reference its variables correctly.

I have created a small sample project here. Let me know.

Thank you. This is exactly what I needed. I have never used 'method()'  before, so your code helped me realize what I was missing. This also allowed me to set that script up properly. I'll be sure to share the game when it's in a more functional state.