Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sugarcube and variable from text

Hello i'm making a game that uses url variables to locate img that should be displayed.

The one problem i have is number of images contained in folder.

<<set $imgmax to 15>>
<<set $imgnmb to random(1,$imgmax)>>

I need $imgmax to be replaced with variable from 'n.txt' placed in $txturl.

It doesn't sounds complicated but i can't get trough jquery to complete this.

Thanks for any help

Comments

  • I need $imgmax to be replaced with variable from 'n.txt' placed in $txturl.

    Why that step?
    Can't you just use <img src=$imgnmb>
  • There is too many folders that can be updated in same time and i need content from txt to var :/
  • If I understand your question correctly you want your story to access and read the contents of a remote text file 'n.txt', and then assign the values within that contents to $variables.

    If that is correct then my question is, will your story HTML file be hosted/located on the same web-server as the text file?
  • Yes for now i don't have time for learning databases simultaneously. All is gonna be gathered in main folder but there are sooooo many folders nested in nested in nested folders that i need to access that .txt's.
    One more time:
    vars:
    pc: dog;
    place: park;
    cond: fog;
    action: peeing;

    Some trigger;

    get number from txt to 'maxrand'
    var imgnmb = get random number(1, $maxrand)

    Display image from "$place/$cond/$pc/$action/$imgnmb" + ".jpg"

    So it will be img from park/fog/dog/43.jpg for example and it works fine but for so many folders and couple of admins that will want to update img content its pointles to CREATE every folder number of contained imgs.


    I think i can't explain it more clear.

    Sorry for my english.
  • I would suggest using something like jquery's get method and then to update the $variables using the state.active.variables assessor from SugarCube's Story API (v1's Story API)

    So something like the following could be added to a custom macro (v1 custom macro):
    $.get( "location/of/n.txt", function(data) {
    
    	var values = null; /* Code to extract the key/value pairs from the retrieved data goes here... */
    	
    	state.active.variables.pc = values.pc;
    	state.active.variables.place = values.place;
    	state.active.variables.cond = values.cond;
    });
    
  • greyelf's suggestion on how to access the file is likely on the right track.

    Beyond that, ideally, instead of trying to parse some custom format in your loading code, it would be better if whatever is creating the n.txt file would generate a JSON file (e.g. n.json) instead, so that JSON.parse() could be used. If it's an automated script, then it shouldn't be an issue, or if it's being created by hand, then it still shouldn't be an issue.
Sign In or Register to comment.