The International Simutrans Forum

Development => Scripting Scenarios and AI => Topic started by: Yona-TYT on April 11, 2018, 08:17:47 PM

Title: Problem with objects passed aroung by reference
Post by: Yona-TYT on April 11, 2018, 08:17:47 PM
I have noticed that when the label is used, the function is executed running indefinitely, should not it be executed only once?  ???
Title: Re: Problem with objects passed aroung by reference
Post by: Dwachs on April 11, 2018, 09:38:39 PM
So one click triggers many calls to the function? Can you post the code of the function?
Title: Re: Problem with objects passed aroung by reference
Post by: Yona-TYT on April 11, 2018, 09:47:16 PM
One click makes the function run permanently.  :o
Title: Re: Problem with objects passed aroung by reference
Post by: Yona-TYT on April 11, 2018, 09:53:17 PM
function get_goal_text(pl)
{
    local goal = ttextfile("goal.txt")
    goal.start = "<em>--></em> <a href='script:script_text()'>"+ translate("Select")+"</a>"
    goal.c1 = "<a href=\"("+our_coord.x+","+our_coord.y+")\">("+our_coord.x +","+""+our_coord.y+")</a>"
    goal.c2 = "<a href=\"("+gl_pos.x+","+gl_pos.y+")\">("+gl_pos.x +","+""+gl_pos.y+")</a>"
    goal.miss = ""+miss_list[our_coord.x]+""
    return goal.tostring()
}

function script_text()
{
       our_coord = check_coord()
    our_nr = 1//miss_list[our_coord.x]
    complete = false
    return 0
}
Title: Re: Problem with objects passed aroung by reference
Post by: Yona-TYT on April 13, 2018, 06:21:44 PM
It seems that the script tags are working well, the problem is another.

function script_text()
{
       our_coord = check_coord()
    our_nr = 1//miss_list[our_coord.x]
    complete = false
    reset()
   
    return null
}


the function "check_coord ()" returns a value of random coordinates, for the whole map, the logic must be that a coordinate is chosen and saved in the variable "our_coord", the problem is that the variable "our_coord" changes its value every time the function "check_coord ()" is called, is this normal?

Edit.
In the image you can see the value of "our_coord" when I click the link only once ... I think all the results should be the same, since "our_coord = check_coord ()" is only executed once with the tag script
(http://files.simutrans.com/index.php/apps/gallery/preview/1082?width=1100&height=1100&c=a7c8f367903dd85b6b01fe9cc0e4b7b8&requesttoken=Uo%2FK%2BojPiRI9mAVxlplpfA8aozAaNjxcA8BpAXDL%2B9c%3D%3AO9vzsumfzlMK9mo3%2BNY9SklqwglqBG8LMesFSki7soY%3D)
Title: Re: Problem with objects passed aroung by reference
Post by: Dwachs on April 13, 2018, 06:56:28 PM
You fell into the trap that objects are passed around as references. check_coord returns count_c, then our_coord is the same coordinate as count_c, if count_c is changed, so is our_coord. I suggest to create a new coord in check_coord and return this