Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script API: implemented ScriptName and Type.GetByName() for all applicable types. #2011

Conversation

ivan-mogilko
Copy link
Contributor

@ivan-mogilko ivan-mogilko commented May 30, 2023

This implements readonly ScriptName property and a static GetByName() method for all the object types that have script names. The method looks up for the object of corresponding type with the given script name, and returns its pointer, or null if such object is not found.

NOTE: passing other type's script name will also return null, for example calling Character.GetByName() with a room object's name.

scriptName argument is case-sensitive, obviously.

static AudioClip* AudioClip.GetByName(const string scriptName);
static Character* Character.GetByName(const string scriptName);
static Dialog* Dialog.GetByName(const string scriptName);
static GUIControl* GUIControl.GetByName(const string scriptName);
static GUI* GUI.GetByName(const string scriptName);
static Hotspot* Hotspot.GetByName(const string scriptName);
static InventoryItem* InventoryItem.GetByName(const string scriptName);
static Object* Object.GetByName(const string scriptName);

@ivan-mogilko ivan-mogilko added type: enhancement a suggestion or necessity to have something improved context: script api labels May 30, 2023
@ivan-mogilko
Copy link
Contributor Author

Something for a distant future, but in a conversation we had with @AlanDrake , I realized this or similar methods may be used to initialize global variables in case we completely move to dynamic creation of everything.

Imagine the game data was loaded, and objects defined at design-time created, but how do you attach them to the variables to keep things consistent with objects created at runtime? Possibly that's how:

function game_start() { // or another "service init" function
    cEgo = Character.FindByName("cEgo");
    cNPC = Character.FindByName("cNPC");
}

@ericoporto
Copy link
Member

ericoporto commented May 30, 2023

I think GetByName is a better name than FindByName for this behavior.

(More similar to the GetAtRoomXY/GetAtScreenXY methods)

@ivan-mogilko ivan-mogilko force-pushed the ags4--scapi-findbyscriptname branch from 143e2ff to fea2cfd Compare May 31, 2023 06:25
@ivan-mogilko ivan-mogilko changed the title Script API: implemented Type.FindByName() for all applicable types. Script API: implemented Type.GetByName() for all applicable types. May 31, 2023
@ivan-mogilko
Copy link
Contributor Author

Renamed FindByName -> GetByName.

@ivan-mogilko
Copy link
Contributor Author

ivan-mogilko commented May 31, 2023

In regards to getting script names from these objects; I've been thinking of quickly adding a readable ScriptName property to all of them, but then thought to instead delay that, and experiment with creating the most basic parent class which would hold this property, thus avoiding duplicate API.

Something like

builtin managed struct Entity {
     import attribute String ScriptName;
};

(which will be the first tiny step to #1228)

EDIT:
Hmm, looking at the code though, this might take a while to do this properly (in a more or less clean way), and I would not want to rush this; so perhaps we could have a ScriptName added in a duplicate way for the time being...
Then after the parent class(es) are added, numerous properties (getters/setters) will have to be merged anyway.

@ivan-mogilko ivan-mogilko force-pushed the ags4--scapi-findbyscriptname branch from fea2cfd to c6bd7eb Compare May 31, 2023 09:09
@ivan-mogilko ivan-mogilko changed the title Script API: implemented Type.GetByName() for all applicable types. Script API: implemented ScriptName and Type.GetByName() for all applicable types. May 31, 2023
@ivan-mogilko
Copy link
Contributor Author

Note: removed Region.GetByName, realizing that regions don't have script names for some reason...

@ivan-mogilko ivan-mogilko merged commit 2abac21 into adventuregamestudio:ags4 Jun 1, 2023
@ivan-mogilko ivan-mogilko deleted the ags4--scapi-findbyscriptname branch June 1, 2023 09:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
context: script api type: enhancement a suggestion or necessity to have something improved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants