Can we use PulpScript to redirect variable access?

I'm trying to structure my variables and code in a manner that PulpScript doesn't seem to expect. Basic structures and arrays would be AWESOME but may be out of scope.

I'm iterating on plans to work around this by using tiles as data structure containers. For example, i've created a TOOL tile, which holds information about the various tools my player might use in the game (for me that's a camera, sonar, radar, GPR).

I plan to use one frame in the tile to represent each of the various tools i'm creating. This way I can create more tools in the future by adding a frame instead of rewriting a mass of spaghetti code.

For this I figure i'd start my room with one tool tile in it somewhere then call its "init" handler:

on init do
      // a bad hack to simulate an array of tools

	_tool_cnt = 4 // number of tools i've defined
	_tool_0 = "camera" // looks forward, sees objects
	_tool_1 = "sonar" // looks around, sees objects
	_tool_2 = "radar" // looks in cone, sees walls
	_tool_3 = "GPR" // looks under ground, sees hazards
	// are the tools VALID (available)?
	// default to yes, but maybe one breaks?
	_v_tool_0 = 1
	_v_tool_1 = 1
	_v_tool_2 = 1
	_v_tool_3 = 1
	// costs in nondescript units
	_c_tool_0 = 1 // camera costs 1
	_c_tool_1 = 2 // sonar costs 2
	_c_tool_2 = 4 // radar costs 4
	_c_tool_3 = 8 // gpr costs 8

now i have all the data setup. later on if i want to leave a tool in the room for my player, all i need to do is drop a TOOL tile there and set its frame to the proper index for the specific tool in question. all good so far.

when a player picks up the tool i'll read the frame number from that tile and set the appropriate valid flag for that tool. i'll write code in the tile script itself for this.

later on i'll use a menu to let a player select from a list of tools the possess - and i'll need to update some variables in the main loop when that happens. i could use a bunch of if/then logic, but that's brittle and i already have one place where my code must be brittle (the init handler above). I'd like to avoid that.

so i'm trying to do something better. when the player selects an item i plan to take note of the tile they are standing on (or some other dedicate spot), swap a TOOL item tile onto the same coordinates, set the tile frame to the index of their selection from the menu (to turn that generic tool tile into the exact tool selected), call the tile's getParams handler (below) then swap back to the original tile to clean up. Doing all that should let me set some global variables so when the user activates their selected tool I know what it is supposed to do...

on getParams do
  t_idx = frame
  // TODO: figure out how to read from variables named as above!
  // that is, use t_nam, t_val, and t_cst as virtual pointers to the variables i actually want to read

  // trying to avoid this:
  if t_idx==0 then
        //...... repeat for every tool
        // ..... maintain this code anytime i update the set of tools supported in-game

Anyone know if this is possible?
If not I suppose I'll post a feature request... but it feels really similar to existing PulpScript mechanisms already - we already switch between strings and identifiers elsewhere like when we call a handler or refer to a tilename... so i'm hoping it can already be done


Secondary note: I'll likely use a dedicated tile location (maybe 0,0) as the place where i drop a tool tile and set its frame, etc. this way there is a specific tile i can call on to perform the tool action - with the actions defined in the tool tile's script.

otherwise i think i'd have to do some work each time the player wants to use their already selected tool - i'd have to swap tiles around, set frame, call the action, then clean up... and that's messy.

so i'll put some label over the location where i place the tool tile and with that i'll have less cleanup to do and a simpler way to say "use whatever tool is currently selected" by just telling the tile at 0,0 to "useTool".

From what I understand you're trying to build a variable name from variables, which was discussed here: Reference variables within variable names? and currently isn't possible.

What you could do instead which might reduce your code a little, is have a different event for each tool that sets up it's variables, something like so

on getParams do
  t_idx = frame
  setupTool = "setup_tool_{t_idx}"
  call setupTool

on setup_tool_0 do
  t_name = "camera"
  t_cost = 1
  t_valid = _c_tool_0
1 Like

and yes, you understood my intent; the older answer definitely applies here.
I think your alternative is still better than nothing, so i'll happily adopt that for now :slight_smile:

Happy making,

1 Like

Happy to help, I think it would be useful to be able to build variable names though, especially because all variables are pre-initialised to 0 so there wouldn't be any issues if it caused a variable name that didn't exist. It would require a new function in Pulpscript though, but maybe if this feature is important enough we could have value = var "values_{i}", and as I write this, I realise this could also provide a rudimentary way of implementing arrays.

1 Like