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

Add a method for retrieving global game/mission var #10

Closed
OrhanNasuf opened this issue Nov 24, 2021 · 8 comments
Closed

Add a method for retrieving global game/mission var #10

OrhanNasuf opened this issue Nov 24, 2021 · 8 comments

Comments

@OrhanNasuf
Copy link

No description provided.

@OrhanNasuf OrhanNasuf changed the title Add a method for retrieving global game/mission vars Add a method for retrieving global game/mission var Nov 24, 2021
@x87
Copy link
Contributor

x87 commented Nov 24, 2021

Using global variables in CLEO scripts is a bad practice and source of many issues. How can you guarantee correct results on a modified main.scm where global variables have different ids?

@OrhanNasuf
Copy link
Author

OrhanNasuf commented Nov 26, 2021

Using global variables in CLEO scripts is a bad practice and source of many issues. How can you guarantee correct results on a modified main.scm where global variables have different ids?

While I understand that dealing with global variables is a bad practice in any context, it is what Rockstar decided to do, and it is the only way to check whether a specific mission has been passed or not.

Some use cases include rewarding the player for passing a specific mission, activating a feature only after a certain amount of progress or displaying achievements.

Based on your concern, I made a test with CLEO 2.0.0.6 on GTA 3:

{$CLEO .cs}
{$USE ini}

wait 500
write_int_to_ini_file $FLAG_JOEY_MISSION1_PASSED "CLEO\test.ini" "MISSIONS_PASSED" "JOEY_1" // Exists
write_int_to_ini_file $THIS_DOES_NOT_EXIST "CLEO\test.ini" "TEST_SECTION" "NOT_EXIST" // Does not exist
terminate_this_script

This was the result:

test-write-gvar-to-ini

So it seems that either 0 is returned when accessing a global var that doesn't exist, or IniFile.WriteInt() is doing some magic.
Either way it wouldn't cause a conflict with a modified main script if handled correctly.
Perhaps a function in CLEO-Redux could do something similar, but return undefined or -1 for non-existent vars.

I tried to work around this limit by writing the values to an ini file with CLEO, which I was then going to access in my javascript with CLEO-Redux; but the File and ini plugins break when CLEO-Redux is loaded, and they cannot be used.
Is this a known issue?

@x87
Copy link
Contributor

x87 commented Nov 26, 2021

File and ini plugins break when CLEO-Redux is loaded

those plugins will only work if original CLEO 2.0.0.6 is present in the same directory
https://github.com/cleolibrary/CLEO-Redux#running-cleo-redux-as-an-addon-to-cleo-library

So it seems that either 0 is returned when accessing a global var that doesn't exist, or IniFile.WriteInt() is doing some magic.

most likely $THIS_DOES_NOT_EXIST was compiled to the first free id (one that is not declared in CustomVariables.ini) and all free variables get initialized to 0 by the default.

Some use cases include rewarding the player for passing a specific mission, activating a feature only after a certain amount of progress or displaying achievements.

Right, also someone mentioned on Discord that usage of timer/counter opcodes require a global variable. I will look into what we can do here.

@OrhanNasuf
Copy link
Author

those plugins will only work if original CLEO 2.0.0.6 is present in the same directory https://github.com/cleolibrary/CLEO-Redux#running-cleo-redux-as-an-addon-to-cleo-library

Thank you!
The .cs script is now able to write the values without issue, but the game still crashes when attempting to read in javascript.

@x87
Copy link
Contributor

x87 commented Mar 21, 2022

Script that reads from and writes to the global SCM variable from JS code:
https://github.com/cleolibrary/CLEO-Redux/blob/master/examples/scm.js

Download scm.js and place it to CLEO_PLUGINS folder. Then import readScmVariable or writeScmVariable in your script.

Example script that displays $ONMISSION variable on screen. To change the var value use writeScmVariable(id, value)

import { readScmVariable, writeScmVariable } from "./cleo_plugins/scm";

// get variable offset for the current game
const ONMISSION_ID = getOnMissionId();

// display the variable value on screen
while (true) {
  wait(250);
  FxtStore.insert("scm_var", readScmVariable(ONMISSION_ID).toString());
  Text.PrintNow("scm_var", 500, 1);
}

// $ONMISSION var has different id in each game
function getOnMissionId() {
  switch (GAME) {
    case "re3":
    case "gta3":
    case "gta3_unreal":
      return 207;
    case "reVC":
    case "vc":
    case "vc_unreal":
      return 313;
    case "sa":
    case "sa_unreal":
      return 409;
  }
  throw new Error("Unsupported game");
}

@x87
Copy link
Contributor

x87 commented Mar 21, 2022

ONMISSION = 0
image

ONMISSION = 1
image

@JuniorDjjr
Copy link

JuniorDjjr commented Mar 31, 2022

Right, also someone mentioned on Discord that usage of timer/counter opcodes require a global variable. I will look into what we can do here.

In CLEO+ I created the opcodes for the local variable, it was pretty easy btw (internally, the game uses a pointer). I think this is important to add it natively in CLEO Redux.
https://github.com/JuniorDjjr/CLEOPlus/blob/main/CLEOPlus/Timer.cpp

Global variables have always been bad practice, and now it's worse as a game patch can change their id (rare, but still), plus this problem that every game is an id, you have to search for ids etc. There are also more common cases, like using an available global variable, and in a patch it is no longer available, or another script uses too etc...

Some use cases include rewarding the player for passing a specific mission, activating a feature only after a certain amount of progress or displaying achievements.

Remember that there are some stats that help with this, and it's a much better practice, some of them count how many missions of certain character has passed, or a certain mission has been completed, or how many cities have been unlocked etc. It doesn't solve all cases, but most of it (some more important). That is, before using global variable, make sure you need to use.

@x87
Copy link
Contributor

x87 commented Dec 17, 2022

Available with scm.ts

@x87 x87 closed this as completed Dec 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants