Creation Code Not Rerolling Variables on Repeat

W

Wardog

Guest
Morning All,

I'm currently attempting to use creation code to run a script that will assign a randomized variable to instances of a specific object, based on weather.

Now, it appears to mostly work as intended, except it is assigning the same value to the target variable to all instances of that object, instead of randomizing the variable between each instance.

obj_node_small Creation Code: (This is attached to each instance of the object in the room).
instance_node_spawning(room);

Script (instance_node_spawning):
Code:
if(instance_exists(obj_node_small)){
    repeat(instance_number(obj_node_small)){
        if(argument[0] == room 1){
                if(obj_weather.current_weather = 1){ // Rain
                        if(random(1) < 0.75){ var nodeassign = 0; } // Foliage 1
                        else { nodeassign = 1; } // Foliage 2
                }
        }
        var inst = obj_node_small;
        with(inst){ nodetype = nodeassign }; // Assign the Nodetype to Each Instance
    }
}
Now, I could just run the script in the creation code of the object itself, and get the desired result, but it will ultimately be so large and expansive, that it made more sense to run it as a script. Any thoughts on how to resolve this? Thanks in advance.
 

Alexx

Member
This line:
Code:
with(inst){ nodetype = nodeassign }
Will set
Code:
nodetype
to the same value for all instances, as although you are looping through all the instances, you're not setting a distinct value for each.

You need to do your processing for each instance. You can iterate through them all using the structure below.

Try starting with:
Code:
if(instance_exists(obj_node_small)
{
    with(obj_node_small)
    {
       put your processing code here
    }
}
 
Last edited:

TheouAegis

Member
Code:
if argument[0] == room_1 and obj_weather.current_weather == 1
with obj_node_small
nodetype = random(1) > 0.75
But i would guess you don't need argument[0] even, as i'm guessing it's just room
 
W

Wardog

Guest
Thank you both for your responses, I was able to resolve the issue and get it working as intended using your advice.

Code Creation:
instance_node_spawning();

Script:
Code:
if(instance_exists(obj_node_small)){
    with(obj_node_small){
        if(room == rm_room1){
            if(obj_weather.current_weather = 1){ // Rain
                if(random(1) < 0.75){ nodetype = 0; } // Foliage 1
                else { nodetype = 1; } // Foliage 2
            }
        }
    }
}
 
Top