CLEO-Redux icon indicating copy to clipboard operation
CLEO-Redux copied to clipboard

Add a method for retrieving global game/mission var

Open OrhanNasuf opened this issue 4 years ago • 7 comments

OrhanNasuf avatar Nov 24 '21 16:11 OrhanNasuf

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?

x87 avatar Nov 24 '21 17:11 x87

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?

OrhanNasuf avatar Nov 26 '21 19:11 OrhanNasuf

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.

x87 avatar Nov 26 '21 20:11 x87

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.

OrhanNasuf avatar Nov 27 '21 22:11 OrhanNasuf

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 avatar Mar 21 '22 00:03 x87

ONMISSION = 0 image

ONMISSION = 1 image

x87 avatar Mar 21 '22 00:03 x87

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.

JuniorDjjr avatar Mar 31 '22 13:03 JuniorDjjr

Available with scm.ts

x87 avatar Dec 17 '22 23:12 x87