Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / [SOLVED] Variables for variables?

Years of Service
User Offline
Joined: 21st Nov 2010
Posted: 27th Jun 2018 21:36
Apologies if this has been answered in Tutorial Guide 2, as I'm still going through it, but I've been through OTG1 and it wasn't in there and it's really bugging me.

Is there a way to store the name of a variable in a variable? Maybe as a string, but I want to be able to use the string to identify the variable in question.

For example, what if I wanted some kind of generic ApplyDamage() function which took the name of a target, an amount to apply, and the name of a variable because maybe I wanted to apply the damage to something other than health? So the creature type has health, strength, coins, and so on. How do I tell ApplyDamage which variable I want it to apply to?

The author of this post has marked a post as an answer.

Go to answer

DBPro Master
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 27th Jun 2018 23:50
Not really, best thing is pass a value to select case against for this kindof thing

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
AGK Developer
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 28th Jun 2018 04:19 Edited at: 28th Jun 2018 04:22
You could handle in different ways. If you used some kind of "master" array for all objects then you could give each an id (which may simply be their index in the array) and then just pass that ID in to your ApplyDamage function.
If you use dedicated arrays for each type then you could pass in a predefined constant value specifying the object type (i.e. OBJECT_TYPE_ENEMY) and as Ortu said use a select structure to sort out which array (the enemies array in this case) the ID applies to.

Personally, I don't like super generic stuff and think it is one of the "bad" modern practices. In my own projects I create manager modules for each specific class of object (EnemyManager, CollectiblesManager, etc) and to apply a damaging force do something along these lines EnemyApplyDamageForce(index as integer, damageAmount as integer, xKnockbackForce as float, yKnockbackForce as float) and that would apply the damageAmount to enemy[index].health as well as apply the forces to its x and y velocities.

Actually here is a snippet from a little project I mess around on in those rare bits of summer development time....

In this case, I only apply the forces when the enemy is killed typically just knocking them back and up into the air a bit. But of course that is just a choice made for this particular game and normally I'd apply the forces period if they are passed in having additional variables in the EnemyType named ForceX and ForceY that are applied and move toward 0 a each update.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 28th Jun 2018 07:12
I would handle it with types and type arrays.

I do it this way because it's easier to read for me and I can also just save that code in it's own file and include it in any project.
AGK Tool Maker
Years of Service
Recently Online
Joined: 13th May 2007
Location: Hellas
Posted: 28th Jun 2018 17:36 Edited at: 28th Jun 2018 17:37
You can create a new type named tDamage. This type will encapsulate all type of damages:

Then, using this type you can pass to a function any type of damage:
Years of Service
User Offline
Joined: 21st Nov 2010
Posted: 30th Jun 2018 21:31
So many good ideas! Thanks for the input, guys.

@GarBenjamin: The reason I'm trying to make this work in a "generic" way is because I'm trying to make the game data as user-editable as possible, with as little "hard coding" as possible. Other than the main gameplay, the central feature is editable levels. In Mario Maker, for example, you can do a lot with placing things and certain things can be changed such as item blocks can contain powerups or monsters and so on. You can't really completely change anything, though. My game is going to be as customisable as i can make it.
Years of Service
User Offline
Joined: 6th Jan 2018
Posted: 1st Jul 2018 12:01 Edited at: 1st Jul 2018 12:02
This post has been marked by the post author as the answer.
Problem with accessing variable names in AppGameKit is that variable names are replaced with integer identifiers when you compile the program, making access to those variables by the engine faster and more efficient. Most programming languages are like that. Usually when someone wants the functions you are talking about, a scripting language is used such as LUA. Has anyone created a scripting language module for AppGameKit? It would be a handy thing to have.

If your requirements are simple enough, you might be able to roll your own, or even use some of the built in functions of AppGameKit like toJSON and fromJSON. Here is george++ example redone using JSON string.

You could have the JSON strings supplied by the player and read from a file, or the strings could be created by the editor using parameters supplied by the player.
Years of Service
User Offline
Joined: 21st Nov 2010
Posted: 2nd Jul 2018 21:30 Edited at: 2nd Jul 2018 21:41
What I was doing in GM:S2 was using the script_execute() and variable_instance_set() functions. Now, although I was using the script_execute() function as intended, technically I wasn't using the variable_instance_set() function as intended, and needed to use accesors (considered an advanced feature of GML) to make it work. In a nutshell, I'd like to be able to have as close to those functions as possible, but, of course, AppGameKit does things fundamentally differently to GM:S and it's not that straightforward.

TomToad, that is a fantastic idea. I was just starting to use toJSON and fromJSON to implement game settings (just music and sound volume so far), and I hadn't thought of using it that way. That's probably the closest example so far of what I was thinking of. In fact, it's actually better than variable_instance_set(), because, according to the official documentation:
Quote: "If a JSON object contains fields that are not in the AppGameKit type being used to load it, then those fields will be ignored. If the AppGameKit type contains fields that are not specified in the JSON object then those fields will be set to zero."
I just realized that's super useful for my purposes. Now I can have damage attempt to apply to literally anything and it will just be ignored if the thing in question doesn't have the correct stats. I could have projectiles that attempt to apply damage to what they collide with, and if it's a damageable object or creature it will work, and if it's an undamageable object the damage will just be ignored and won't hard-crash AppGameKit like incorrectly using variable_damage_set() crashes GM:S.

I feel like there must be a good substitute for script_execute() that doesn't involve hard-coding a switch case, but I am using that for now.

Login to post a reply

Server time is: 2020-07-14 07:38:33
Your offset time is: 2020-07-14 07:38:33