deadcast2 / UltraEd

A WIP level editor/game engine for the Nintendo 64.
MIT License
132 stars 13 forks source link

Sharing variables through multiple functions #135

Closed EGAMatsu closed 2 years ago

EGAMatsu commented 2 years ago

Not sure if im just dumb or not, but if i make a float in start, it cant be used in Update.

deadcast2 commented 2 years ago

The current way to pass things around is to utilize the dynamic property of the actor. Since actors can be cloned during runtime there needs to be a way to give each actor the ability to customize itself as needed. You could also define a global variable in the top of your script file like: int $globalValue = 0;. The dollar signs will make sure there's no naming collisions.

typedef struct $_MyVars
{
    int someValue;
        // Put more variables in here if you like.
} $MyVars;

void $Start(Actor *self)
{
        // Have to allocate space for this struct on the heap.
        $MyVars *myVars = ($MyVars *)malloc(sizeof($MyVars));

        myVars->someValue = 2;

        self->dynamic = myVars;
}

void $Update(Actor *self)
{
     $MyVars *myVars = ($MyVars *)self->dynamic;

     if (myVars != NULL)
     {
                int test = 1 + myVars->someValue;
     }
}

If the actor get destroyed the dynamic data will be released. I may find better ways in the future to allow actors to "talk" to each other and share variables within themselves. There is a notify method that lets you send messages to other actors.